Spotify API-检索Google Apps脚本中的有效访问令牌 [英] Spotify API - Retrieving Valid Access Token in Google Apps Scripts

查看:153
本文介绍了Spotify API-检索Google Apps脚本中的有效访问令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Here is the documentation for the Spotify API (I'm using the Implicit Grant Flow): https://beta.developer.spotify.com/documentation/general/guides/authorization-guide/#implicit-grant-flow

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

I'm trying to write a script in Google Sheets. I'm focussing on the basic setup, but I cannot seem to get the access token working.

已解决:

我目前收到以下错误(似乎我的 我的提取方法的参数设置不正确):

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

错误":"unsupported_grant_type",错误描述":"grant_type必须是client_credentials,authorization_code或refresh_token"

解决方案:

grant_type必须根据Google列出为有效负载 脚本方法UrlFetchApp.fetch(请参见下面的更新代码)

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

-

已解决:

正如您在下面看到的那样,当我尝试获取 令牌(尽管文档中指定了"post"),因为该帖子 方法始终返回405错误.我认为这是我迈出的一步 搞砸了.我假设我不应该使用 csrf_token作为访问令牌.

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.

解决方案:

https://accounts.spotify.com/token 应该是 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修改了用于检索访问令牌的脚本.您可以尝试这个修改后的脚本吗?

I modified your script for retrieving access token by following curl -X "POST" -H "Authorization: Basic ZjM4ZjAw...WY0MzE=" -d grant_type=client_credentials https://accounts.spotify.com/api/token of the document. Can you please try this modified script?

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())

参考:

  • 客户端凭据流
  • Reference :

    • Client Credentials Flow
    • 如果响应消息已更改,请告诉我.

      If the response message was changed, please tell me.

      这篇关于Spotify API-检索Google Apps脚本中的有效访问令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆