【问题标题】:Failed to retrieve clubhouse.io api using UrlFetchApp.fetch使用 UrlFetchApp.fetch 检索 clubhouse.io api 失败
【发布时间】:2020-07-07 04:48:43
【问题描述】:

问题说明:无法使用clubhouse.io api检索数据 在 Google 表格中 > 脚本编辑器

Per developers.google.com 某些 HTTP 方法(例如,GET)不接受有效负载。 然而,俱乐部 v3 api 期望 GET 请求中的 body/payload

方法如下:

function getClubhouseStories() {
  try{  
    var myHeaders = {"Content-Type": "application/json"};
    var requestOptions = {
      method: 'GET',
      headers: myHeaders,
      body: JSON.stringify({"query":"lable\:my label"}),
      redirect: 'follow',
      query: {"token": "XXXXXXXXUUIDXXXXX"},
      muteHttpExceptions: true
    };

    var response = UrlFetchApp.fetch("https://api.clubhouse.io/api/v3/search/stories", requestOptions);
    }
  catch(error) {
    console.error(error);
  }
  var responseCode = response.getResponseCode();
  var responseContent = response.getContentText();
  Logger.log(responseCode);
  Logger.log(responseContent);
}

返回:

responseCode >> 401
responseContent  >> "{"message":"Sorry, the organization context for this request is missing. If you have any questions please contact us at support@clubhouse.io.","tag":"organization2_missing"}"

同样的请求可以通过postmanbash 完美运行,不需要正文的请求也可以通过UrlFetchApp.fetch 运行

标签:

#clubhouse-api #google-apps-scripts #postman

【问题讨论】:

  • 应该是 stackoverflow.com/questions/56200001 的副本,但帖子所有者在收到答案之前就放弃了
  • 基本上,当您想要检索数据时,您不应该将有效负载传递给您的请求。

标签: bash google-apps-script postman urlfetch


【解决方案1】:

您收到的消息Sorry, the organization context for this request is missing. 是您无法发送授权令牌/标头时收到的错误。

你需要这样的东西:

var myHeaders = {"Content-Type": "application/json", "Shortcut-Token": "<token>"};

Shortcut API docs

【讨论】:

    【解决方案2】:

    您可以将tokenquery 参数作为URL 的一部分。

    function getClubhouseStories() {
      try {  
        var requestOptions = { muteHttpExceptions: true };
        var parameters = {
          token: 'XXXXXXXXUUIDXXXXX',
          query: 'label:"my label"' // Clubhouse API requires using double quotes around multi-word labels
        };
    
        var url = "https://api.clubhouse.io/api/v3/search/stories";
        var response = UrlFetchApp.fetch(buildUrl_(url, parameters), requestOptions);
      } catch (error) {
        console.error(error);
      }
      var responseCode = response.getResponseCode();
      var responseContent = response.getContentText();
      Logger.log(responseCode);
      Logger.log(responseContent);
    }
    
    /**
     * Builds a complete URL from a base URL and a map of URL parameters.
     * Source: https://github.com/gsuitedevs/apps-script-oauth2/blob/master/src/Utilities.js#L27
     * @param {string} url The base URL.
     * @param {Object.<string, string>} params The URL parameters and values.
     * @return {string} The complete URL.
     * @private
     */
    function buildUrl_(url, params) {
      var paramString = Object.keys(params).map(function(key) {
        return encodeURIComponent(key) + '=' + encodeURIComponent(params[key]);
      }).join('&');
      return url + (url.indexOf('?') >= 0 ? '&' : '?') + paramString;
    }
    

    您面临的其他问题与无效的请求选项有关UrlFetchApp parameters

    • 默认方法是'GET',所以不需要指定
    • Content-Type 应使用contentType 指定,但默认为“application/x-www-form-urlencoded”,因此无需指定
    • body 无效。应该改用payload,但在这种情况下不是,因为我们需要在 URL 中包含参数。
    • redirect 无效。应该使用followRedirects,但这已经默认为true。
    • query 无效。需要手动包含在 URL 中。

    【讨论】:

    • 上面的代码会返回null,因为query: 'label:"my label"' 和我改成query: 'label\:my label' 后返回记录
    • 它为我返回了带双引号的 null
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-30
    • 2020-11-08
    • 2016-09-21
    • 1970-01-01
    • 2016-06-01
    • 1970-01-01
    相关资源
    最近更新 更多