Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Authenticating to REST API from Google Sheets

Bo Lasater November 20, 2022

I am trying to send an authentication token in a request to Jira from Google Apps Scripts.  I haven't been able to make it work.  I've tried using tokens by base64 encoding my email and password as well as ones from the API token section of my profile.  Neither works.  I have success using a colleague's token, but he doesn't know how he got it.  Any help would be appreciated!

function testEdit(){

  let headers = {

    "content-type": "application/json",

    "Accept": "application/json",

    "Authorization": "Basic Y2hhc2XXXXX” //Chase

  };

// A final few options to complete the JSON string

  let data

  {

    "fields" : {

        //"customfield_10110" :

        "summary":

        {"value" : "foo"}

    }

  };

  var payload = JSON.stringify(data);

  var options = {

    "content-type": "application/json",

    "method": “PUT”,

    "headers": headers,

    "payload": payload,

    "muteHttpExceptions":false

  };

  var response = UrlFetchApp.fetch("http://whisper.atlassian.net/rest/api/2/issue/IMG-1625", options);

  

  let responseContent = response.getContentText();

 

  let jsonContent = JSON.parse(responseContent);

  Logger.log(jsonContent.key);

}



1 answer

0 votes
marc -Collabello--Phase Locked-
Community Champion
November 21, 2022

Using email and password for authentication won't work.  You must use email and a token, Base64 encoded.

If your token does not work, I would recommend the following:

  • Generate a new token
  • Test if you (i.e. your Jira user actually has access to the issue)
  • Use the new token for access from Google AppsScript/Sheets
Bo Lasater November 24, 2022

Thanks, Marc.  When you say "generate a token", do you mean from the "Generate API token" of my Jira account, or from Base64 encoding my username and password, or from something else?

marc -Collabello--Phase Locked-
Community Champion
November 28, 2022

You should generate a token from the "Generate API token" account page.  Then take this token like `Password:Token`, and base64 encode the result.  This base64 encoded string is what you use when accessing Jira.

However, make sure you understand how AppsScript is calling the API.  Maybe AppsScript already does the base64 encoding for you.

Suggest an answer

Log in or Sign up to answer