I used the below code to POST grant_type
, client_id
, client_secret
, refresh token
to
Below is the postman Javascript-fetch console which is successful when I try in POstman
var myHeaders = new Headers();
myHeaders.append("Content-Type", "application/x-www-form-urlencoded");
var urlencoded = new URLSearchParams();
urlencoded.append("grant_type", "refresh_token");
urlencoded.append("client_id", "xxxx");
urlencoded.append("client_secret", "yyyy");
urlencoded.append("refresh_token", "zzzz");
var requestOptions = {
method: 'POST',
headers: myHeaders,
body: urlencoded,
redirect: 'follow'
};
fetch(";, requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));
Kindly help me to get the result in Appscript. So I tried the below code in the Google Sheets script, but it's showing error
function webexDev() {
var cisurl = ";;
var data = {
'grant_type': 'refresh_token',
'client_id': 'xxxx',
'client_secret': 'yyyy',
'refresh_token': 'zzzz'
};
var options = { 'method': 'post', 'payload': data, 'contentType': 'application/x-www-form-urlencoded'}
var response = UrlFetchApp.fetch(cisurl,options);
var cisjson=response.getContentText();
var cisdata=JSON.parse(cisjson)
Logger.log(cisjson)
}
I tried the same in Postman and it got successful. I want to the same in Google Sheet Script
I used the below code to POST grant_type
, client_id
, client_secret
, refresh token
to https://webexapis./v1/access_token
Below is the postman Javascript-fetch console which is successful when I try in POstman
var myHeaders = new Headers();
myHeaders.append("Content-Type", "application/x-www-form-urlencoded");
var urlencoded = new URLSearchParams();
urlencoded.append("grant_type", "refresh_token");
urlencoded.append("client_id", "xxxx");
urlencoded.append("client_secret", "yyyy");
urlencoded.append("refresh_token", "zzzz");
var requestOptions = {
method: 'POST',
headers: myHeaders,
body: urlencoded,
redirect: 'follow'
};
fetch("https://webexapis./v1/access_token", requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));
Kindly help me to get the result in Appscript. So I tried the below code in the Google Sheets script, but it's showing error
function webexDev() {
var cisurl = "https://webexapis./v1/access_token";
var data = {
'grant_type': 'refresh_token',
'client_id': 'xxxx',
'client_secret': 'yyyy',
'refresh_token': 'zzzz'
};
var options = { 'method': 'post', 'payload': data, 'contentType': 'application/x-www-form-urlencoded'}
var response = UrlFetchApp.fetch(cisurl,options);
var cisjson=response.getContentText();
var cisdata=JSON.parse(cisjson)
Logger.log(cisjson)
}
I tried the same in Postman and it got successful. I want to the same in Google Sheet Script
Share Improve this question edited Jun 30, 2021 at 15:05 Moses asked Jun 30, 2021 at 13:46 MosesMoses 2361 gold badge4 silver badges17 bronze badges 2- 1 Instead of (or as well as) looking at the user interface in Postman, look at the actual request generated by Postman, when you hit "Send". Then you will see how the data in the screenshot is converted into a valid request. Use that as your guide for what you need to build in your script.. – andrewJames Commented Jun 30, 2021 at 14:12
-
Also:
"x-www-form-urlencoder"
appears to have a typo in it (it ends with ad
notr
. And it's a part of a content type (see here). It's also the default content type for form data. See the "Headers" tab in Postman. – andrewJames Commented Jun 30, 2021 at 14:13
1 Answer
Reset to default 2You are mixing things up. Those parameters grant_type, client_id, client_secret and refresh_token are not HTTP-Headers but the payload that you're sending to the server.
According to the docs (https://developers.google./apps-script/reference/url-fetch/url-fetch-app), this should work:
var cisurl = "https://webexapis./v1/access_token";
var data = {
'grant_type': 'refresh_token',
'client_id': 'abcdefg',
'client_secret': 'hijklmn',
'refresh_token': 'opqrstuvw'
};
var options = {
'method': 'post',
'payload': data
}
var response = UrlFetchApp.fetch(cisurl,options);
P.S.: Do not share your secrets on stack-overflow