[英]Getting a Spotify access token using only client credentials (Spotify Web API Node)
[英]How to get Spotify access token using Web API Node
我參考了鏈接https://www.npmjs.com/package/spotify-web-api-node
代碼示例:
var SpotifyWebApi = require('spotify-web-api-node');
// credentials are optional
var spotifyApi = new SpotifyWebApi({
clientId: 'fcecfc72172e4cd267473117a17cbd4d',
clientSecret: 'a6338157c9bb5ac9c71924cb2940e1a7',
redirectUri: 'http://www.example.com/callback'
});
在這種情況下,我如何獲得訪問令牌?
上面的特定代碼不會返回訪問令牌。 您需要參考該包的“授權”部分,您可以在其中決定是授權代碼流還是客戶端憑據流。 兩者都將返回一個訪問令牌,但根據您的用例,您必須決定哪一個最適合您。
對於授權代碼流,它看起來像這樣:
spotifyApi.authorizationCodeGrant(code).then(
function(data) {
console.log('The token expires in ' + data.body['expires_in']);
console.log('The access token is ' + data.body['access_token']);
console.log('The refresh token is ' + data.body['refresh_token']);
...more code
}
對於客戶端憑據流,它看起來像這樣:
spotifyApi.clientCredentialsGrant().then(
function(data) {
console.log('The access token expires in ' + data.body['expires_in']);
console.log('The access token is ' + data.body['access_token']);
...more code
})
Spotify 授權指南將是一個很好的資源,可以幫助您決定要遵循的路徑。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.