I am using the following code to make a POST request to https://accounts.spotify.com/api/token in order to receive a refreshed access token although every time I've tried I get an Error 400 (Bad Request)
$.ajax({
url: 'https://accounts.spotify.com/api/token',
type: 'post',
Headers: {
'Authorization': 'Basic ' + btoa(ClientID ':' ClientSecret),
'Content-Type': 'application/x-www-form-urlencoded'},
body: {
grant_type: 'refreshtoken',
refresh_token: RefreshToken
},
json: true
});
Related
Sorry for this title but i really need hlp. I don't know why that's not working and i searched a lot.
So I'm working with Spotify Api and I want to access to the Access_Token. The documentation says you have to do like that: Spotify Documentation
And I m requesting like this :
fetch ('https://accounts.spotify.com/api/token', {
method: 'post',
body: {
code: code,
redirect_uri: redirectUri,
grant_type: 'authorization_code'
},
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
Authorization: 'Basic ' + btoa(clientId+':'+clientSecret)
},
json: true
})
But that's answering this:
Error
I checked and error 400 means "Bad Request".
Have u got an idea? Thanks for helping !
Looking at your error, no body was received. You have to send it as a json string:
let body = {
code: code,
redirect_uri: redirectUri,
grant_type: 'authorization_code'
}
fetch ('https://accounts.spotify.com/api/token', {
method: 'post',
body: JSON.stringify(body),
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
Authorization: 'Basic ' + btoa(clientId+':'+clientSecret)
},
json: true
})
I use the following code using request which works as expected, got http response 200
var request = require('request');
var auth
var options = {
'method': 'POST',
'url': 'https://oauth2.k.de.com/oauth2/token',
'headers': {
'Accept': 'application/json',
'Authorization': 'Basic NGViMTE2ODctZTNjNi00NDUyLTgwNjgtMzhiOjJDR2lJd0hxOFFx==',
'Content-Type': 'application/x-www-form-urlencoded'
},
form: {
'grant_type': 'client_credentials',
'scope': 'app:read'
}
};
request(options, function (error, response) {
if (error) throw new Error(error);
let body = JSON.parse(response.body);
….
Now I need to convert it to axios as request been deprecated but it’s not working for me ( I got http 400 response )
const axios = require('axios').default;
axios({
method: 'post',
'url': 'https://oauth2.k.de.com/oauth2/token',
data: {
'grant_type': 'client_credentials',
'scope': 'app:read'
},
headers: {
'Accept': 'application/json',
'Authorization': 'Basic NGViMTE2ODctZTNjNi00NDUyLTgwNjgtMzhiOjJDR2lJd0hxOFFx==',
'Content-Type': 'application/x-www-form-urlencoded'
}
}).then(function (response: any) {
console.log("Head With Authentication :" + response);
}).catch(function (error: any) {
console.log("Post Error : " + error);
});
Any idea why with request library with the exact same data it works (http response 200) and in axios I got 400 ?
In request I put the grant_type etc in form and in axios in data, this is the only diffrencace I see, any idea?
This is the error I got
Request failed with status code 400
Should I use other rest libary if it cannot be done via axios ?
This is a bug, you might want to check this: https://github.com/axios/axios/issues/362
The problem is, because of axios interceptors, your Content-Type header is disappearing. If you have access and can change the backend, you can make it work with another header and set it on your client code. Otherwise if your code is working in a browser, you can try using URLSearchParams as suggested here.
Passing Authentication param properly in headers but it is get converted into WWW-Authenticate:Basic realm="Realm" in the response headers.
Below is my code snippet of get api call.
doGetAuthRaceReplay (url) {
var param = this._doGetParam({ 'credentials': 'include',
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': "Basic " + window.btoa('uname+':'+password')});
return fetch(url, param)
}
_doGetParam (headers) {
var params = {
method: 'GET',
dataType: 'JSON',
headers: headers
};
return params;
}
I am getting error 401 after calling api. It is working fine from POSTMAN.
I have also tried it with normal jquery ajax call but still not able to get it.
var username = "uname";
var password = "pass";
var url = 'My url';
$.ajax({
url: url,
success: function(json) {
alert("Success", json);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert(textStatus, errorThrown);
},
headers: {
'Access-Control-Allow-Origin' : '*',
'Authorization' : "Basic " + btoa(username+':'+password)}
'accept':'application/json'
},
type: 'GET',
contentType: 'json',
});
Still getting 401 error.
I'm trying to receive a token from the Spotify api to allow me to search the api and such for songs and other information.
On my sever application index.js I start by requiring the package:
var request = require('ajax-request');
Later on I go to request my token:
request.post({
method: 'POST',
url: 'https://accounts.spotify.com/api/token',
'Content-Type' : 'application/x-www-form-urlencoded',
header: {
Authorization: 'Basic' + <urlEncodedClientIdAndClientSecret>,
},
data: {
grant_type: 'client_credentials'
}
}, function(err, res, body) {
});
However if I try and console log this it always returns undefined, is there an issue with how I've laid out my post request or is there something else I'm missing?
Oh....I just looked at your request again....are you missing a space after the "Basic"? Try this:
Authorization: 'Basic ' + <urlEncodedClientIdAndClientSecret>,
I have a small Spotify app that I am trying to convert to use the axios http library. I am having an issue with the callback when logging in. Up to this point I have been using request like is in all of the Spotify documentation. Everything works fine with request, but even though everything looks the same with axios, I get a 500 Internal Server Error. Here is my code to make the http request:
var authOptions = {
method: 'POST',
url: 'https://accounts.spotify.com/api/token',
form: {
code: code,
redirect_uri: REDIRECT_URI,
grant_type: 'authorization_code'
},
headers: {
'Authorization': 'Basic ' + (new Buffer(CLIENT_ID + ':' + CLIENT_SECRET).toString('base64'))
},
json: true
};
axios(authOptions).then(res => {
console.log(res)
})
I can pass the same authOptions object to the request library everything works fine. Here is my request from axios logged out to the console.
{
method: 'POST',
url: 'https://accounts.spotify.com/api/token',
form:
{ code: 'changedthecode',
redirect_uri: 'http://localhost:8888/callback',
grant_type: 'authorization_code' },
headers: { Authorization: 'Basic changedthecode=' },
json: true,
timeout: 0,
transformRequest: [ [Function] ],
transformResponse: [ [Function] ],
withCredentials: undefined
}
And here is my response with the axios library:
{ data: { error: 'server_error' },
status: 500,
statusText: 'Internal Server Error',
headers:
{ server: 'nginx',
date: 'Fri, 04 Dec 2015 14:48:06 GMT',
'content-type': 'application/json',
'content-length': '24',
connection: 'close' },
config:
{ method: 'POST',
headers: { Authorization: 'Basic changedthecode' },
timeout: 0,
transformRequest: [ [Function] ],
transformResponse: [ [Function] ],
url: 'https://accounts.spotify.com/api/token',
form:
{ code: 'changedthecode',
redirect_uri: 'http://localhost:8888/callback',
grant_type: 'authorization_code' },
json: true,
withCredentials: undefined }
}
The only option that I didn't know about from axios was withCredentials, and it didn't work when it was set to false or true. What else am I missing?
The problem is that I was posting a form and was not encoding it when going across the wire and I was not setting the Content-Type. I changed my authOptions to:
var authOptions = {
method: 'POST',
url: 'https://accounts.spotify.com/api/token',
data: querystring.stringify({
grant_type: 'refresh_token',
refresh_token: refreshToken
}),
headers: {
'Authorization': 'Basic ' + (new Buffer(CLIENT_ID + ':' + CLIENT_SECRET).toString('base64')),
'Content-Type': 'application/x-www-form-urlencoded'
},
json: true
};
and everything worked fine.