1
votes

Problem Statement: Unable to retrieve data using clubhouse.io api in Google sheets > Script Editor

Per developers.google.com: Certain HTTP methods (for example, GET) do not accept a payload. However, the clubhouse v3 api expect body/payload in GET request

Here is method:

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);
}

Returns:

responseCode >> 401
responseContent  >> "{"message":"Sorry, the organization context for this request is missing. If you have any questions please contact us at [email protected].","tag":"organization2_missing"}"

The same request works perfect via postman or bash, and requests that don't need body also work via UrlFetchApp.fetch

Tags:

#clubhouse-api #google-apps-scripts #postman
2
Should be a duplicate of stackoverflow.com/questions/56200001 but that post owner gave up just before they received a answerTheMaster
Basically, when you want to retrieve data, you should not pass a payload to your request.ziganotschka

2 Answers

1
votes

You can include the token and query parameters as part of the 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;
}

Other issues you're facing are related to request options that aren't valid UrlFetchApp parameters:

  • Default method is 'GET', so no need to specify
  • Content-Type should be specified using contentType, but it defaults to "application/x-www-form-urlencoded", so no need to specify
  • body is not valid. Should use payload instead, but not in this case, because we need to include parameters in the URL.
  • redirect is not valid. Should use followRedirects, but that already defaults to true.
  • query is not valid. Need to manually include in the URL.
0
votes

The message you received, Sorry, the organization context for this request is missing. is the error you'll receive when you fail to send an authorization token/header.

You need something like this:

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

Shortcut API docs