question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

authorizationCodeGrant not working.

See original GitHub issue

Hey, I have the following code on my firebase server for authenticating a user:

var SpotifyWebApi = require('spotify-web-api-node');

async function proxySpotifyToken(req, res){
    const spotifyApi = new SpotifyWebApi({
        clientId: "myId",
        clientSecret: "mySecret",
        redirectUri: "com.myRNapp:/callback",
    });

    const code = req.body.code;

    // console shows code
    console.log(code);

    if(code){
        spotifyApi.authorizationCodeGrant(code).then(
            data => {
                return res.json(data.body)
            },
            err => {
                console.log('Something went wrong!', err)
            }
        ).catch((oError) => {
            return res.json(oError)
        })
    }
}

module.exports = {
    authentication: proxySpotifyToken
};

I’m using react-native-app-auth to make the call to the server and pass on the code. As you can see, I’m logging the code in the Firebase console and it does shows a string, so that seems to be working.

Whatever I try, It always catches the following error: Something went wrong! { [WebapiError: Bad Request] name: ‘WebapiError’, message: ‘Bad Request’, statusCode: 400 }

Am i doing something wrong here?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9

github_iconTop GitHub Comments

1reaction
oliveroneillcommented, Jun 17, 2020

Thanks @AquaWolf! Got it working. For those interested, here’s a discussion including Spotify engineers regarding this problem: https://community.spotify.com/t5/Spotify-for-Developers/Authentication-API-failing-in-production-right-now/td-p/4959662

1reaction
AquaWolfcommented, Jun 10, 2020

Ok i dived a bit deeper and found that somehow i needed a codeverifier for the authorizationCodeGrant method. Here is the new method:

authorizationCodeGrant: function(code, code_verifier, callback) {
    return AuthenticationRequest.builder()
      .withPath('/api/token')
      .withBodyParameters({
        grant_type: 'authorization_code',
        redirect_uri: this.getRedirectURI(),
        code: code,
        code_verifier: code_verifier
      })
      .withHeaders({
        Authorization:
          'Basic ' +
          new Buffer(
            this.getClientId() + ':' + this.getClientSecret()
          ).toString('base64')
      })
      .build()
      .execute(HttpManager.post, callback);
  }
Read more comments on GitHub >

github_iconTop Results From Across the Web

spotifyApi.authorizationCodeGrant is not a function · Issue ...
Expected: authorizationCodeGrant should be a function available for access using the library. Actual: spotifyApiInstanceWithCredentials.
Read more >
spotify-web-api-node: authorizationCodeGrant() give a 400
Then, I use this code to get an Access Token and a Refresh Token from Spotify and perform all the actions I want....
Read more >
Passport's Authorization Code Grant not working out of the box!
So when we run the /redirect in the browser, and the user is not logged in to our auth server, the login page...
Read more >
Troubleshooting Guide for Cisco Unity Connection Release 12.x
This section explains various problems that may occur while using Authorization Code Grant Flow along with the resolution.
Read more >
Authorization Code Grant Flow Error Messages in the Login ...
The following table lists errors that are visible in the Detail column of the Login Audit Trail Results. Problem. Authorization Code Grant Flow...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found