I've been working on a Spotify player using React, and so far I've been able to get the player to properly connect to the API and receive a device_id.
My next step is change the player to the newly "made" one, but I keep on receiving 400: Required field device_ids missing, even though I have it included in the PUT request:
const requestOptions = {
method: 'PUT',
headers: {
'Authorization' : 'Bearer ' + spotifyAccessToken
},
data: {
"device_ids": [device_id],
"play": 'true'
}
};
// Change player
fetch('https://api.spotify.com/v1/me/player', requestOptions)
Am I missing something/formatting it incorrectly? I've tried changing the data to body and I instead get a 400: Malformed JSON.
Thanks in advance!
I was able to get this working using the following format:
const requestOptions = {
method: 'PUT',
body: JSON.stringify({
device_ids: [southORadioDevId],
play: 'true'
}),
headers: {
Authorization: `Bearer ${spotifyAccessToken}`
},
};
// Change player
fetch('https://api.spotify.com/v1/me/player', requestOptions)
Related
I am using spotify api for a project. Every request I made work fine except this one. I want to be able to create a new playlist. The scope was already checked, it's not the source of the problem. For this I made the following request
axios('https://api.spotify.com/v1/users/' + ID + '/playlists', {
method: 'POST',
headers: {
'Authorization' : 'Bearer ' + data.data.access_token,
"Content-Type" : "application/json"
},
data: {
name: Name,
Description: Description
}
})
when I tried it, I've got 400 Bad Request - The request could not be understood by the server due to malformed syntax. The message body will contain more information
Here's the link for the spotify api references
https://developer.spotify.com/documentation/web-api/reference/playlists/create-playlist/
assuming your ID is defined. You need to stringify your json.
Been a while since I used axios,
axios('https://api.spotify.com/v1/users/' + ID + '/playlists', {
method: 'POST',
headers: {
'Authorization' : 'Bearer ' + data.data.access_token,
'Content-Type': 'application/json'
},
data: JSON.stringify({
name: Name,
description: Description
})
})
I'm trying to be understand authorization mechanism in power bi API
I would embed a report in my web app.
I have done the steps as mentioned in docs
Actually I would get report embedded url then use power bi JS API to embed the report.
Getting access_token is successful
var options = {
'method': 'POST',
'url': `https://login.microsoftonline.com/${process.env.TENANT_ID}/oauth2/token`,
'headers': {
'Content-Type': 'multipart/form-data'
},
formData: {
'grant_type': process.env.GRANT_TYPE,
'client_id': process.env.CLIENT_ID,
'client_secret': process.env.CLIENT_SECRET,
'resource': "https://analysis.windows.net/powerbi/api",
'Scope': "https://analysis.windows.net/powerbi/api/.default"
}
};
Now I try to get embedded token for report in group
var data = { accessLevel: "View", datasetId: "5b11d62a-803e-46c9-83f3-*****" };
var config = {
method: 'post',
url: `https://api.powerbi.com/v1.0/myorg/groups/${process.env.GROUP_ID}/reports/${process.env.Report_ID}/GenerateToken`,
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${JSON.parse(response).access_token}`
},
data: data
};
let embedtoken
try {
embedtoken = await axios(config)
}
catch (e) {
console.log(e)
}
context.res = {
// status: 200, /* Defaults to 200 */
body: JSON.parse(response).access_token
};
I get error 400 response
But When I generate an embed token for dashboard I get a valid token. But of course that's not working with get report API
My goal is to get report infos. For information I get get that using the access token but it's not safe
For POST API requests, data should be passed in string format. This can be done by using for example, JSON.stringify(data).
Refer below code snippet which should resolve the error.
var config = {
method: 'post',
url: `https://api.powerbi.com/v1.0/myorg/groups/${process.env.GROUP_ID}/reports/${process.env.Report_ID}/GenerateToken`,
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${JSON.parse(response).access_token}`
},
data: JSON.stringify(data) };
I acccessed WEBAPI using React. I successfully received data (Status code 200) but in http format.
I want to extract information from this. Could you please help on this.
IHttpActionResult is the return type in ASP.net
Output in Postman looks like following : [{Name: "Ravi", City:"HYD"},{Name: "Raj", City:"ABC"},{Name: "Rakesh", City:"ZZZ"}] but in react it shows totally different as shown in black screenshot.
ASP.net sample code:
public IHttpActionResult itemList(PropertyModel pObj)
{
// some code.
// returning data in list like [{Name:'a',city:'HYD'},{Name:'b',city:'HYD'}]
}
Output in react native when access WebAPI:
Following is the piece of code I written.
fetch('http://IPADDRESS/Inventory/api/ItemList', {
method: 'POST',
mode: 'no-cors',
credentials: 'same-origin', // include, *same-origin, omit
redirect: 'follow', // manual, *follow, error
referrerPolicy: 'no-referrer', // no-referrer, *client
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: JSON.stringify({
Organisatio:'ABC',
Type:'Item'
})
}).then((response) => {
console.log(JSON.stringify(response));
})
.then((responseJson) => {
})
.catch((error) => {
console.error(error);
});
I'm using this code to create payment(Stripe) in the tab Pre-request script
pm.sendRequest({
url: 'https://api.stripe.com/v1/tokens',
method: 'POST',
headers: {
'Authorization': 'Bearer pk_test_...',
'Content-Type': 'application/x-www-form-urlencoded'
},
data: {
'card[number]': 2223003122003222,
'card[exp_month]': 12,
'card[exp_year]': 2020,
'card[cvc]': 123
}
And getting this error:
You did not provide an API key. You need to provide your API key in the Authorization header, using Bearer auth (e.g. 'Authorization: Bearer YOUR_SECRET_KEY').
everything works as a separate request
In my case, I had my dotenv import come after I was calling my secret key.
const stripe = require("stripe")(process.env.STRIPE_SECRET);
require ("dotenv").config();
Reversing those two lines of code was what I needed to do.
require ("dotenv").config();
const stripe = require("stripe")(process.env.STRIPE_SECRET);
I think you're after something like this:
pm.sendRequest({
url: 'https://api.stripe.com/v1/tokens',
method: 'POST',
header: {
'Authorization': 'Bearer pk_test_......',
'Content-Type': 'application/x-www-form-urlencoded'
},
body: {
mode: 'urlencoded',
urlencoded: [
{ key: "card[number]", value: 2223003122003222 },
{ key: "card[exp_month]", value: 12 },
{ key: "card[exp_year]", value: 2020 },
{ key: "card[cvc]", value: 123 },
]
}
})
Your issue was around the headers key as it should be header. Also, the way the payload was written seemed like it would error(?). I tried this code locally and saw the response back from Stripe.
You might want to change the token credentials now that you've posted it here.
I'm quite new with ANGULAR and web development in general and I'm currently working on a web tool. Now I want this tool to send a POST request to a web service but Im encountering a weird bug. Now I have below code in my javascript:
var data_info = { test_id: 'TEST', model_id: 'TEST:TEST_ID' };
//data_info = JSON.stringify(data_info);
var request_json = {
method: 'POST',
url: url,
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
data: data_info,
cache: false,
};
console.log(request_json);
$http(request_json).then(function successCallback(response) {
// response code here
}
Now this code currently doesn't pass the preflight request check and is always returning a 405. But if I change the line data: data_info in the request JSON into a new key let's say body: data_info it now successfully sends the request and I can confirm that the service is receiving it. I'm not sure what's the issue here and can't figure it out.
change your header to :
headers : {
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;'
}
Please try