Help Me guys :)

119 views
Skip to first unread message

Samuel Adomeh

unread,
Jun 23, 2022, 8:18:56 AM6/23/22
to Google Apps Script Community
Hi Guys,

Hopefully, someone can assist me.

I have the script below which I have been working on for quite a while ( still new to app script). I have a payload that posts certain data to an API and then returns some values.

I was able to generate a token, but each time I try to post to the main url ( which post some values and returns the expected updated values with newly updated fields)  I always get an error  "Exception: Request failed for https://api.bol.com returned code 400"

Not sure what is happening. Any help and advise on how to go about this will be really helpful.

function getApiToken() {

    var url = "#####################";
    var user = "##############";
    var password = "########################";
    var headerss = {
        "Accept": "application/xml",
        "Content-Type": "application/xml",
        "Authorization": "Basic "+ Utilities.base64Encode(user+":"+password)}
    
    var data = {
        "method" : "POST",
        "headers" : headerss
    }; 
    var response = UrlFetchApp.fetch(url,data);

  
    var url2 = "###################";
    var accessToken = response;  
    Logger.log(accessToken);
    var payload =       {
                            "commissionQueries": [
                          {
                              "ean": "3148950032102",
                              "condition": "NEW",
                              "unitPrice": "14.54"
                              ,
                              "fixedAmount": " ",
                              "percentage":  " ",
                              "totalCost": " "  
                          },
                            {
                              "ean": "3148950040855",
                              "condition": "NEW",
                              "unitPrice": "16.06"
                              ,
                              "fixedAmount": " ",
                              "percentage":  " ",
                              "totalCost": " "  
                            },
                            {
                              "ean": "3148950040879",
                              "condition": "NEW",
                              "unitPrice": "21.92"
                              ,
                              "fixedAmount": " ",
                              "percentage":  " ",
                              "totalCost": " "  
                            },
                            {
                              "ean": "5411711429069",
                              "condition": "NEW",
                              "unitPrice": "22.82",
                              "fixedAmount": " ",
                              "percentage":  " ",
                              "totalCost": " "  
                            },
                        ] };

    var headers = {
      headers: {
         Authorization: 'Bearer ' + accessToken,
         "Accept": "application/vnd.retailer.v6+json",
        "Content-Type": "application/vnd.retailer.v6+json",
      }
   }

    var options = {
        "method" : "POST",
        "headers": headers,  
         payload: JSON.stringify(payload),
         
         };

   const responses = UrlFetchApp.fetch(url2, options);
   if (responses.getResponseCode() != 200) {
        console.error("Failed to send to chat with error: " + response.getContentText());
        return;
    }



2022-06-23_14h14_35.png

R Tichy

unread,
Jun 23, 2022, 12:58:00 PM6/23/22
to Google Apps Script Community
Try this? 

var headers = {
      "headers": {
         "Authorization": 'Bearer ' + accessToken,
         "Accept": "application/vnd.retailer.v6+json",
        "Content-Type": "application/vnd.retailer.v6+json",
      }
   }

    var options = {
        "method" : "POST",
        "headers": headers,  
         "payload": JSON.stringify(payload),
         
         };

R Tichy

unread,
Jun 23, 2022, 1:34:12 PM6/23/22
to Google Apps Script Community
ALSO, Are you certain that the headers in the API call are supposed to read like this:

"headers": { "headers": {
         "Authorization": 'Bearer ' + accessToken,
         "Accept": "application/vnd.retailer.v6+json",
        "Content-Type": "application/vnd.retailer.v6+json",  },

BECAUSE it seems like the word "headers" is nested too many times, likely.

Samuel Adomeh

unread,
Jun 23, 2022, 2:33:01 PM6/23/22
to Google Apps Script Community
Hi Rob, thanks a lot for looking into this for me. It actually works even after nesting the header. The issue now is when I run the 
   var responses = UrlFetchApp.fetch(url2, options);
   Logger.log(responses) // 
this logger.log prints     "8:28:10 PM Info" 
without returning the result from the payload body after posting the data, and getting updates. I am trying to figure out now how to return the pay_load data with updated information after posting it.



R Tichy

unread,
Jun 23, 2022, 2:46:47 PM6/23/22
to Google Apps Script Community
Does this matter?  See red lowercase "s" on end of response in response.getContentText()

const responses = UrlFetchApp.fetch(url2, options);
   if (responses.getResponseCode() != 200) {
        console.error("Failed to send to chat with error: " + responses.getContentText());
        return;

R Tichy

unread,
Jun 23, 2022, 2:48:09 PM6/23/22
to Google Apps Script Community
AND I still might try un-nesting the "header" json-object one level because the authtoken is in there and needs to be found ...

Samuel Adomeh

unread,
Jun 23, 2022, 3:25:43 PM6/23/22
to Google Apps Script Community
True! you were right. It was not reading the token. After I remove the nested header it worked fine. Just the issue now is that it's giving me the error below.

{title=Problem with JWT, status=400.0, detail=Supplied JWT is malformed or unsupported} . 

R Tichy

unread,
Jun 23, 2022, 3:53:49 PM6/23/22
to Google Apps Script Community
var response = UrlFetchApp.fetch(url,data);

This is where you grab the response that has the token, but you are grabbing the whole http response, I think you can Logger.log() that object.  I suspect you want something like response.data instead of the whole response with http wrappers.
Reply all
Reply to author
Forward
0 new messages