簡體   English   中英

如何使用 Web API 節點獲取 Spotify 訪問令牌

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM