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

标签 javascript json google-apps-script spotify

这是 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/

    相关文章:

    javascript - Angular js Controller 只打印大括号

    javascript - 带有 assetic 的 symfony 内联 Assets

    java - 使用层次结构数据动态创建 json 文件

    javascript - 全日历 JSON/AJAX

    google-apps-script - 访问 Google Sheets 的 "Locale"

    arrays - 转换数组数据的问题

    javascript - 在 2D 矩阵数组中移动 Sprite - JavaScript

    javascript - 2 秒后离开循环

    javascript - 将数据绑定(bind)到 d3 中的新元素

    google-apps-script - 无法获得授权