This 包含 Spotify 的所有三个授权流程的示例。
你想看看Client Credentials flow。此流程仅在客户端工作,不需要任何后端部分。使用Client Credentials flow,您将无法检索特定用户的特定数据,这在您的场景中是不必要的。
我假设您创建的 project 是一个 Spotify Web API 应用程序,它有一个客户端 ID 和一个客户端密码。需要这两个值才能使Client Credentials flow 正常工作。
以下 sn-p 显示了 Spotify 的 Client Credentials flow 示例。
/**
* This is an example of a basic node.js script that performs
* the Client Credentials oAuth2 flow to authenticate against
* the Spotify Accounts.
*
* For more information, read
* https://developer.spotify.com/web-api/authorization-guide/#client_credentials_flow
*/
var request = require('request'); // "Request" library
var client_id = 'CLIENT_ID'; // Your client id
var client_secret = 'CLIENT_SECRET'; // Your secret
// your application requests authorization
var authOptions = {
url: 'https://accounts.spotify.com/api/token',
headers: {
'Authorization': 'Basic ' + (new Buffer(client_id + ':' + client_secret).toString('base64'))
},
form: {
grant_type: 'client_credentials'
},
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/users/jmperezperez',
headers: {
'Authorization': 'Bearer ' + token
},
json: true
};
request.get(options, function(error, response, body) {
console.log(body);
});
}
});