Spotify API使当前用户的数据返回错误未经授权

时间:2016-07-25 22:27:08

标签: javascript node.js spotify

目前正在尝试获取当前登录用户的数据,但我使用https://api.spotify.com/v1/me请求的任何内容都会出现401 Unauthorized错误。使用https://api.spotify.com/v1/users/{id}工作正常。我在这里做错了什么?

app.get('/playlist', function(req, res) {
var authOptions = {
    url: 'https://accounts.spotify.com/api/token',
    form: {
        code: access_code,
        grant_type: 'client_credentials',
        redirect_uri: redirect_uri
    },
    headers: {
        'Authorization': 'Basic ' + (new Buffer(client_id + ':' + client_secret).toString('base64'))
    },
    json: true
};

request.post(authOptions, function(error, response, body) {
    if (!error && response.statusCode === 200) {

        // use the access token to access the Spotify Web API
        var token = body.access_token;

        var options = {
            url: 'https://api.spotify.com/v1/me/playlists',
            headers: {
                'Authorization': 'Bearer ' + token
            },
            json: true
        };

        request.get(options, function(error, response, body) {
            if (!error && response.statusCode === 200) {
                res.send(body);
            } else {
                console.log('Error retrieving playlists');
            }
        });
    } else {
        console.log('Error authenticating playlist: ' + response.statusCode);
    }
});
});

0 个答案:

没有答案