Call external Api with Bearer Token

47 views
Skip to first unread message

Uriel Nusenbaum

unread,
Nov 12, 2022, 9:32:18 AM11/12/22
to Google Apps Script Community
Hi folks, how are you?
I'm trying to call an external API using Bearer token. My goal is to make some calls during the configuration and then a last call to get data. 
The first call to get the token works as expected. The second call that should use the token failed using the token saved on property on scriptProperties return an error. I logged the value and if I use this token using Postman or if I use harcoded on the code it works.
Any idea? Suggestions?
Below part of my code:


GAS.png

Appreciate you help!
Regards.

Andrew Roberts

unread,
Nov 12, 2022, 11:42:22 AM11/12/22
to google-apps-sc...@googlegroups.com
Can you share CleanForGAS()?

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/e1b61baf-f0eb-4c6e-a55d-c6d2bff67612n%40googlegroups.com.

Uriel Nusenbaum

unread,
Nov 12, 2022, 12:13:58 PM11/12/22
to google-apps-sc...@googlegroups.com
Hi Andrew! How are you? Hope you are doing well.
This function is just a test that I did, trying to remove spaces just in case. It doesn't work with and without it.
Here is the code:
function cleanForGAS(str) {
  if (typeof str == "string") {
    var escaped = escape(str.trim());
    for (var i = 0i <= 31i++) {
      var s = i.toString(16);
      var re = new RegExp("%" + (s.length == 1 ? "0" + s : s).toUpperCase(), "g");
      escaped = escaped.replace(re"");
    }
    var remove = ["%7F""%81""%8D""%8F""%90""%9D"];
    remove.forEach(function(e) {
      var re = new RegExp(e"g");
      escaped = escaped.replace(re"");    
    });
    return unescape(escaped).trim();
  } else {
    return str;
  }
}


Regards.

You received this message because you are subscribed to a topic in the Google Groups "Google Apps Script Community" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-apps-script-community/Y5UkFpeCSQw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/CAN1QPJQr%2BV4Njy_djLT1_ogHfQD_gAQJFyKNbGx16tAq1bKiFg%40mail.gmail.com.

Stuart Smith

unread,
Nov 13, 2022, 4:51:46 PM11/13/22
to Google Apps Script Community
Hi Uriel, have you tried either debug mode or log statements to test:
- what is coming back from the property service and whether it's what you expect
- the output of cleanGAS and whether it's what you expect

i.e. for debug mode, set a breakpoint before "var options...." and when execution stops, examine the contents of the headers variable, checking to see if it matches what you expect

Or if you prefer add log statements to output the value of headers so you can see what's actually going on.

Without knowing more, I'd guess that either what's getting stored under the token key isn't really the token, or the cleanForGAS function isn't doing what you expect and is corrupting the token in some fashion.
Reply all
Reply to author
Forward
0 new messages