亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

查詢 Spotify 的 Web API 客戶端憑證流

查詢 Spotify 的 Web API 客戶端憑證流

阿波羅的戰車 2023-03-24 14:41:51
我正在嘗試根據https://developer.spotify.com/documentation/general/guides/authorization-guide/#client-credentials-flow Client Credentials Flow上的文檔發出 http 請求。我寫過const BASE_URL = 'https://accounts.spotify.com/api/token';fetch(BASE_URL, {            method: 'POST',            headers: {                'Content-Type': 'application/x-www-form-urlencoded',                'Authorization': 'Basic ' + base64(clientID) + ':' + base64(clientSecret)            },            body: JSON.stringify({'grant_type:client_credentials'})        })這是否按照它所說的去做?我很困惑如何編寫發布請求的正文。
查看完整描述

3 回答

?
莫回無

TA貢獻1865條經驗 獲得超7個贊

我最終做了哪些工作:


async authorize(){

        let myHeaders = new Headers();

        myHeaders.append("Authorization", `Basic ${my_clientID:clientSecret}`);

        myHeaders.append("Content-Type", "application/x-www-form-urlencoded");


        var urlencoded = new URLSearchParams();

        urlencoded.append("grant_type", "client_credentials");


        const requestOptions = {

        method: 'POST',

        headers: myHeaders,

        body: urlencoded,

        redirect: 'follow'

        }

        

        let res = await fetch("https://accounts.spotify.com/api/token", requestOptions);

        res = await res.json();

        return res.access_token; 

    }


async search(){

        const access_token = await this.authorize();

        this.setState({access_token});

        const BASE_URL = 'https://api.spotify.com/v1/search';

        let FETCH_URL = `${BASE_URL}?q=${this.state.query}&type=artist&limit=1`; 

        const ALBUM_URL = 'https://api.spotify.com/v1/artists';


        let myHeaders = new Headers();

        myHeaders.append("Authorization", `Bearer ${access_token}`);

        

        const requestOptions = {

            method: 'GET',

            headers: myHeaders

        }


        let res = await fetch(FETCH_URL, requestOptions);

        res = await res.json();

        console.log("ARTIST", res);

}


查看完整回答
反對 回復 2023-03-24
?
明月笑刀無情

TA貢獻1828條經驗 獲得超4個贊

從您共享的鏈接中,客戶端憑證流是向 spotify API 服務器發出請求的客戶端(服務器端)。因此,這是一個服務器到服務器的身份驗證流程(不是授權)。您正在使用客戶端的 fecth API,這意味著您的實現應該是服務器端的。如果您使用的是 node.js 運行時服務器端框架,只需查找即可在http.request API服務器端發出請求。

例如,這將是一個純 node.js 實現:

const options = {

   hostname: 'https://accounts.spotify.com/api/token',

   method: 'POST',

   headers: {

     'Content-Type': 'application/x-www-form-urlencoded',

     'Authorization': 'Basic ' + base64(clientID) + ':' + base64(clientSecret)

   }

 };


 const req = http.request(options, (res) => {

   res.setEncoding('utf8');

   // process the data bit by bit or in chunks...

   res.on('data', (chunk) => {});

   // ...and do something with it when there is no more data in response

   res.on('end', () => {

    console.log('No more data in response.');

   });

 });

 

 // handle the error explicitly

 req.on('error', (e) => {

   console.error(`problem with request: ${e.message}`);

 });


 req.end();


查看完整回答
反對 回復 2023-03-24
?
江戶川亂折騰

TA貢獻1851條經驗 獲得超5個贊

對我來說,我不確定其他人是否也是這種情況,但 spotify api 拒絕base64(clientID) + ":" + base64(clientKey),但接受base64(clientID + ":" + clientKey)



查看完整回答
反對 回復 2023-03-24
  • 3 回答
  • 0 關注
  • 170 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號