Announcements

Help Wizard

Step 1

NEXT STEP

FAQs

Please see below the most popular frequently asked questions.

Loading article...

Loading faqs...

VIEW ALL

Ongoing Issues

Please see below the current ongoing issues which are under investigation.

Loading issue...

Loading ongoing issues...

VIEW ALL

Cannot authenticate to Spotify API with Node.JS app

Cannot authenticate to Spotify API with Node.JS app

Plan

Free

Country

CH

Programming Language

Node.JS 12.13.0

 

My Question or Issue

Hi,

I'm creating a bot using the Spotify API. This bot should update one of my playlist by adding a specific song.

My problem is that I cannot obtain an access token with the /authorize endpoint.

Here's my code :

 

 

 

 

 

 

 

function getToken() {
    let generateRandomString = function (length) {
        let text = '';
        let possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';

        for (let i = 0; i < length; i++) {
            text += possible.charAt(Math.floor(Math.random() * possible.length));
        }
        return text;
    };

    let spotifyApi = new SpotifyWebApi({
        clientId: data.spotify.client_id,
        redirectUri: data.spotify.redirect_uri
    });

    /**
     * Authenticating the app to Spotify Web API
     * @type {string[]}
     */
    let scopes = ['user-read-private', 'user-read-email', 'playlist-modify-public'],
        state = generateRandomString(16);

    // Create the authorization URL
    let authorizeURL = spotifyApi.createAuthorizeURL(scopes, state, false);

//Obtained URL -> https://accounts.spotify.com/authorize?client_id=516af068412341e69675da2c0f364846&response_type=code&redirect_uri=http://localhost:8888/callback&scope=user-read-private%20user-read-email%20playlist-modify-public&state=SEdtlgsP8aECVurs&show_dialog=false

    // Create request to the callback Express route
    https.get(authorizeURL, response => {
        if (response) {
            return console.log(response);
        }
    }).on('error', err => {
        console.error(err);
    });
}

 

 

 

 

 

 

 

When I process the GET request to authenticate my app (see the https.get part above) and when I check what is given in my console (output given by the return console.log(response)), I get that :

 

 

 

 

 

 

 

...
responseUrl: 'https://accounts.spotify.com/login?continue=https%3A%2F%2Faccounts.spotify.com%2Fauthorize%3Fscope%3Duser-read-private%2Buser-read-email%2Bplaylist-modify-public%26response_type%3Dcode%26redirect_uri%3Dhttp%253A%252F%252Flocalhost%253A8888%252Fcallback%26state%3DlD3Ijyz5ucTZ9L5B%26client_id%3D516af068412341e69675da2c0f364846%26show_dialog%3Dfalse',
...

 

 

 

 

 

 

This is the reponse URL I have. It seems that I'm not logged, thus I cannot access the Spotify Web API.

My callback route is working since I can copy/paste the authorizeUrl in a browser, and I'll get the token written in a text file. It's only when I try to process a request with https.get that it doesn't work.

 

If the API prompt me for login, then why is there an CLIENT_ID and CLIENT_SECRET since there not used ?

 

Any idea ?

 

Thanks in advance

 

EDIT : fixed obtained URL since it wasn't what I obtained ^^

Reply
1 Reply

Hey man, how did you get your RedirectURI, i have been getting INVALID RedirectURI on my ReactNative expo app

Suggested posts

Staff
Let's introduce ourselves!

Hey there you,   Yeah, you! 😁   Welcome - we're glad you joined the Spotify Community!   While you here, let's have a fun game and get…

Staff
ModeratorStaff / Moderator/ 4 years ago  in Social & Random

Type a product name