Reputation: 143
I'm new to using API connections to get data into Google Sheets using google script.
How do I write the CURL request below in Google Script?
curl -u {client_id}:{client_secret} \
https://api.podbean.com/v1/oauth/token \
-X POST -d 'grant_type=authorization_code&code={code}&redirect_uri={your_redirect_uri}'
And this one
curl https://api.podbean.com/v1/analytics/podcastReports \
-G -d 'access_token={access_token}' -d 'podcast_id={podcast_id}' -d 'year=2018'
Upvotes: 1
Views: 248
Reputation: 201603
I believe your goal as follows.
You want to convert the following 2 curl commands to Google Apps Script.
Curl command 1.
curl -u {client_id}:{client_secret} \
https://api.podbean.com/v1/oauth/token \
-X POST -d 'grant_type=authorization_code&code={code}&redirect_uri={your_redirect_uri}'
Curl command 2.
curl https://api.podbean.com/v1/analytics/podcastReports \
-G -d 'access_token={access_token}' -d 'podcast_id={podcast_id}' -d 'year=2018'
function sample1() {
// Please set your values to the following variables.
const client_id = "your client_id";
const client_secret = "your client_secret";
const code = "your code";
const redirect_uri = "your redirect_uri";
const params = {
method: "post",
payload: {
grant_type: "authorization_code",
code: code,
redirect_uri: redirect_uri
},
headers: {Authorization: "Basic " + Utilities.base64Encode(`${client_id}:${client_secret}`)},
muteHttpExceptions: true
};
const url = "https://api.podbean.com/v1/oauth/token";
const res = UrlFetchApp.fetch(url, params);
console.log(res.getContentText())
}
function sample2() {
// Please set your values to the following variables.
const access_token = "your access_token";
const podcast_id = "your podcast_id";
const year = "2018";
const baseUrl = "https://api.podbean.com/v1/analytics/podcastReports";
const url = `${baseUrl}?access_token=${access_token}&podcast_id=${podcast_id}&year=${year}`;
const res = UrlFetchApp.fetch(url, {muteHttpExceptions: true});
console.log(res.getContentText())
}
Before you use above scripts, please confirm your values of client_id
, client_secret
, code
, redirect_uri
, access_token
, podcast_id
, year
in the scripts again.
About "Sample script for Curl command 2", when the special characters are included in the values of access_token
and podcast_id
, please use encodeURIComponent()
to the values like const url = `${baseUrl}?access_token=${encodeURIComponent(access_token)}&podcast_id=${encodeURIComponent(podcast_id)}&year=${year}`;
.
Upvotes: 1