gpt4 book ai didi

javascript - Spotify API - 在 Google Apps 脚本中检索有效的访问 token

转载 作者:行者123 更新时间:2023-12-04 02:04:54 25 4
gpt4 key购买 nike

这是 Spotify API 的文档(我使用的是隐式授权流程):https://beta.developer.spotify.com/documentation/general/guides/authorization-guide/#implicit-grant-flow

我正在尝试在 Google 表格中编写脚本。我专注于基本设置,但似乎无法让访问 token 正常工作。

SOLVED:

I'm currently receiving the following error (it seems like my parameters for my fetch method aren't set properly):

"error":"unsupported_grant_type","error_description":"grant_type must be client_credentials, authorization_code or refresh_token"

SOLUTION:

The grant_type must be listed as a payload according to the Google Scripts method UrlFetchApp.fetch (see updated code below)



--

SOLVED:

As you can see below I'm using the 'get' method when trying to get the token (despite the documentation specifying 'post') because the post method consistently returns a 405 error. I think this is the step I'm screwing up on. I'm assuming that I'm not supposed to be using the csrf_token as the access token.

SOLUTION:

https://accounts.spotify.com/token should have been https://accounts.spotify.com/api/token



更新了以下工作代码:
  var fetchParams = {
'method':'post',
'payload':{'grant_type':'client_credentials'},
'headers':{'Authorization':authorization},
'muteHttpExceptions':true
}

var replaceResponse = UrlFetchApp.fetch('https://accounts.spotify.com/api/token', fetchParams);

var regExp = /access_token(.*?):/;

var contentText = replaceResponse.getContentText();
var access_token = contentText.slice(contentText.search('access_token')+15,contentText.search(',')-1);

var requestOptions = {
'headers':{'Authorization':'Bearer '+access_token},
'muteHttpExceptions':true
}

var finalResponse = UrlFetchApp.fetch('https://api.spotify.com/v1/tracks/4dhARBZ8YLvm8oRDnCIeXr', requestOptions);

最佳答案

我按照 curl -X "POST" -H "Authorization: Basic ZjM4ZjAw...WY0MzE=" -d grant_type=client_credentials https://accounts.spotify.com/api/token 修改了用于检索访问 token 的脚本的文件。你能试试这个修改过的脚本吗?

var authorization = "Basic "+ Utilities.base64Encode('<client_id>:<client_secret>');
var fetchParams = {
method: 'post', // Modified
payload: {'grant_type': 'client_credentials'}, // Modified
headers: {'Authorization': authorization},
muteHttpExceptions: true
}
var replaceResponse = UrlFetchApp.fetch("https://accounts.spotify.com/api/token", fetchParams); // Modified
Logger.log(replaceResponse.getContentText())

引用 :
  • Client Credentials Flow

  • 如果响应消息已更改,请告诉我。

    关于javascript - Spotify API - 在 Google Apps 脚本中检索有效的访问 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50459496/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com