Zjost
Zjost

Reputation: 77

403 response on API call from GoogleScript

I used this piece of code from https://developers.rebrandly.com/docs/api-custom-url-shortener. I filled my key. Also typed work space "Main Workspace" (not sure if that is correct [or required]. doesn't really look like an ID, but could not find anything else).

(I tested my domain setup with the shortening interface on the Rebrandly.com website self. that works. so setup should be fine).

function Rebrandly(){
  var payload = {
    destination: "https://www.youtube.com/channel/UCHK4HD0ltu1-I212icLPt3g",
    domain: { fullName: "rebrand.ly" }
    //, slashtag: "A_NEW_SLASHTAG"
    //, title: "Rebrandly YouTube channel"
  }

  var headers = {
    apikey: "YOUR_API_KEY",
    workspace: "YOUR_WORKSPACE_ID"
  }

  var params = {
    headers: headers,
    contentType: "application/json",
    method: 'post',
    payload: JSON.stringify(payload),
    muteHttpExceptions: true
  }
 var response = UrlFetchApp.fetch("https://api.rebrandly.com/v1/links", params);

  Logger.log(response.getResponseCode());

  if (response.getResponseCode() == 200){
    var link = JSON.parse(response.getContentText())
    Console.log(`Long URL was ${payload.destination}, short URL is ${link.shortUrl}`);
  }
} 

(Sorry, maybe question looks stupid, but am just an amateur programmer)

Upvotes: 0

Views: 138

Answers (1)

Tanaike
Tanaike

Reputation: 201603

Modification points:

  • From I used this piece of code from https://developers.rebrandly.com/docs/api-custom-url-shortener. I filled my key. Also typed work space "Main Workspace" (not sure if that is correct [or required]. doesn't really look like an ID, but could not find anything else)., I thought that you use your API key for apikey: "YOUR_API_KEY". And you use your workspace ID for workspace: "YOUR_WORKSPACE_ID" as "Main Workspace".

    • If my understanding is correct, in this case, I think that "Main Workspace" is the workspace name. This is not the workspace ID. When "Main Workspace" is the workspace ID, such error occurs. I think that this is the reason of your issue. In order to retrieve your workspace ID, you can retrieve it using the following script.

        function getWorkSpaceId() {
          const apikey = "###";  // Please set your api key.
          const url = "https://api.rebrandly.com/v1/account/workspaces?orderBy=createdAt&orderDir=desc&limit=25";
          const res = UrlFetchApp.fetch(url, {headers: {apikey: apikey}});
          const obj = JSON.parse(res.getContentText())
          const workspaceIds = obj.map(({id}) => id);
          console.log(workspaceIds)
        }
      
  • In your script, Console.log(Long URL was ${payload.destination}, short URL is ${link.shortUrl}); is required to be modified. In this case, please use console.log.

Modified script:

When getWorkSpaceId() is included in your script, it becomes as follows. Before you use this script, please set your API key to apikey.

function getWorkSpaceId(apikey) {
  const url = "https://api.rebrandly.com/v1/account/workspaces?orderBy=createdAt&orderDir=desc&limit=25";
  const res = UrlFetchApp.fetch(url, {headers: {apikey: apikey}});
  const obj = JSON.parse(res.getContentText())
  const workspaceIds = obj.map(({id}) => id);
  return workspaceIds;
}

function Rebrandly(){

  var apikey = "YOUR_API_KEY"; // <--- Please set your api key.

  var workspaceIds = getWorkSpaceId(apikey);
  var workspaceId = "";
  if (workspaceIds.length > 0) {
    workspaceId = workspaceIds[0];
  } else {
    throw new Error("No workspace ID.");
  }

  var payload = {
    destination: "https://www.youtube.com/channel/UCHK4HD0ltu1-I212icLPt3g",
    domain: { fullName: "rebrand.ly" }
    //, slashtag: "A_NEW_SLASHTAG"
    //, title: "Rebrandly YouTube channel"
  }

  var headers = {
    apikey: apikey,
    workspace: workspaceId
  }

  var params = {
    headers: headers,
    contentType: "application/json",
    method: 'post',
    payload: JSON.stringify(payload),
    muteHttpExceptions: true
  }
 var response = UrlFetchApp.fetch("https://api.rebrandly.com/v1/links", params);

  Logger.log(response.getResponseCode());

  if (response.getResponseCode() == 200){
    var link = JSON.parse(response.getContentText())
    console.log(`Long URL was ${payload.destination}, short URL is ${link.shortUrl}`);
  }
}

Note:

  • In this modified script, the 1st workspace ID is used by giving the api key. So this is used for testing script. But, if you want to use the specific workspace ID, please modify above script.

  • Above script supposes that your API key is valid. Please be careful this.

References:

Upvotes: 1

Related Questions