1
votes

I am trying to build a web app that integrates with the spotify API. For this, I am using the Authorization grant flow.

I managed to get an authorization code, but on the back end when I am testing the endpoint that should exchange the auth code with an access token, I keep getting a 415 response status.

Here is the service that the endpoint is using:

export async function getAccessAndRefresh(code: string): Promise<any> {
return axios.post(ACCESS_URL, {
    data: {
        "grant_type": "authorization_code",
        "code": code,
        "redirect_uri": REDIRECT_URI
    },
    headers: {
        "Authorization": " Basic " + Buffer.from(CLIENT_ID + ":" + CLIENT_SECRET).toString("base64"),
        "Content-Type": "application/x-www-form-urlencoded",
    },
    method: "POST",
    json:true
})

}

Also, I wrote this unit test in order to test the service(I got the 415 while running this unit test):

describe("Request tests", () => {

let server: Server;

function initServer() {


    server = createServer(App);

    server.listen(5000);
}

function destroyServer() {
    server.close();
}

test("Test refresh and access token returned by spotify api", () => {

    return getAccessAndRefresh(AUTH_CODE).then((value)=>{
        expect(value).toHaveProperty("access_token");
    })

})

beforeAll(() => {
    initServer();
});

afterAll(()=>{
    destroyServer();
})

})

In the test, AUTH_CODE is a code that I obtained manually in a browser by accessing the https://accounts.spotify.com/authorize endpoint with my API Key.

Can anyone help me figure this one out please? Thanks!

1
That is odd, everything looks good here, have you tried running this call in Postman to make sure it works there, I've compared your call with mine in Postman and it appears similar that I can't spot anything initially wrongRoguePlanetoid
I've tried running a curl with the same parameters, and the curl works just fine. I haven't tried Postman thoughAdrian Pascu
@RoguePlanetoid I've tried with Postman now. If I encode the body as form-data I get the same 415 status. I've also tried changing to x-www-form-urlencoded and got a 400 status with this body : { "error": "invalid_grant", "error_description": "Invalid redirect URI" }Adrian Pascu
I think I know the problem, you need to log into the Dashboard where you got your Client Id and Secret and go to the settings and set the Redirect URI there to the one you're using e.g. example.com/postback that should fix the issue, whatever you're doing to get the 400 error that's all you need to do, that's the "correct" error for that situationRoguePlanetoid

1 Answers

1
votes

In the Spotify Dashboard you might need to set the Redirect URI to the URL you're using in your code, these need to match if getting the following error:

{ "error": "invalid_grant", "error_description": "Invalid redirect URI" }

That's all you need to do, just go to the Dashboard where you get the Client ID and Client Secret and then go to the Edit Settings and you'll see the option to set the Redirect URI