为了账号安全,请及时绑定邮箱和手机立即绑定

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 关注
  • 75 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信