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

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

Spotify api 檢索到的訪問令牌不起作用

Spotify api 檢索到的訪問令牌不起作用

C#
MM們 2022-11-22 10:21:31
我正在使用 spotify API 創建一個 React/C# 應用程序,我收到了一個訪問令牌,但它不起作用。嘗試調用 API 時出現錯誤(如下所示)(不知道為什么顯示“無效用戶名”,因為它從未被使用過):錯誤:消息:“無效的用戶名”狀態:404code來自身份驗證過程的較早步驟(在我使用節點時工作,所以我猜那部分是正確的)并且我使用了所有范圍只是為了確保這不是問題playlist-modify-public playlist-modify-private user-read-private user-read-email user-read-playback-state user-read-recently-played我的 C# 代碼檢索訪問令牌:var spotifyClientId = "id";var spotifySecret = "secret";var webClient = new WebClient();var postparams = new NameValueCollection();postparams.Add("grant_type", "client_credentials");postparams.Add("code", code);postparams.Add("redirect_uri", "http://localhost:8000/");//api documentation says you can post clientid/secret in either body or headerpostparams.Add("client_id", spotifyClientId);postparams.Add("client_secret", spotifySecret);//var authHeader = Convert.ToBase64String(Encoding.Default.GetBytes($"{spotifyClientId}:{spotifySecret}"));//webClient.Headers.Add(HttpRequestHeader.Authorization, "Basic " + authHeader);var tokenResponse = webClient.UploadValues("https://accounts.spotify.com/api/token", postparams);var textResponse = Encoding.UTF8.GetString(tokenResponse);return Json(textResponse);所以我收到這是響應(注意訪問令牌很短)"\"access_token\":\"BQAOYJZnfS0LPEwwZ_06Be76mx59bpoPsVPD0uTvvrwvDtSNi9flgeZQK8kDPSRyTFdE70iBk5PtEAvqnIQ\",\"token_type\":\"Bearer\",\"expires_in\":3600,\"scope\":\"\"}"` 當我嘗試使用此令牌調用 API 時,出現如上所示的錯誤。如果我手動獲取令牌,我可以從前端進行 API 調用
查看完整描述

1 回答

?
慕尼黑5688855

TA貢獻1848條經驗 獲得超2個贊

我發現郵遞員在調用授權后有一個代碼示例。在成功完成 oauth 驗證后,轉到右上角的代碼并編寫代碼示例,安裝 restsharp 并刪除一些參數,這樣你就會得到這樣的結果。


        var client = new RestClient("https://accounts.spotify.com/api/token");

        var request = new RestRequest(Method.POST);

        request.AddHeader("Authorization", "Basic ODAzNTkzMDMxNzIwNGViNzlhMf54f45f5454fNTJhMTg=");

        request.AddParameter("grant_type", "authorization_code");

        request.AddParameter("code", code);

        request.AddParameter("redirect_uri", "http://localhost:8000/");


        IRestResponse response = client.Execute(request);


        return Json(response.Content);


查看完整回答
反對 回復 2022-11-22
  • 1 回答
  • 0 關注
  • 121 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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