Spotify API authorisation via Google Apps Script - google-apps-script

I am using the following code to make requests to the Spotify API via Google Apps Script:
function search() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var artist = sheet.getRange(1,1).getValue();
artist = encodeURIComponent(artist.trim());
var result = searchSpotify(artist);
Logger.log(result);
}
function searchSpotify(artist) {
//searches spotify and returns artist ID
var response = UrlFetchApp.fetch("https://api.spotify.com/v1/search?q=" + artist + "&type=artist&limit=1",
{ method: "GET",
headers:{
"contentType": "application/json",
'Authorization': "Bearer BQBnpSUdaEweirImw23yh2DH8OGhTwh5a_VnY_fgb2BPML0KvFvYd04CaEdUhQN9N4ZUXMIVfJ1MjFe1_j0Gl0UoHDhcoC_dklluZyOkq8Bo6i2_wfxSbGzP3k5EUjUKuULAnmTwCdkdZQnl-SNU0Co"
},
});
json = response.getContentText();
var data = JSON.parse(json);
var uri = data.artists.items[0].uri.slice(15);
var getArtists = getRelatedArtists(uri);
Logger.log(getArtists);
return getArtists;
}
function getRelatedArtists(uri) {
//searches related artists with the returned ID
var response = UrlFetchApp.fetch("https://api.spotify.com/v1/artists/" + uri + "/related-artists",
{ method: "GET",
headers:{
"contentType": "application/json",
'Authorization': "Bearer BQBnpSUdaEweirImw23yh2DH8OGhTwh5a_VnY_fgb2BPML0KvFvYd04CaEdUhQN9N4ZUXMIVfJ1MjFe1_j0Gl0UoHDhcoC_dklluZyOkq8Bo6i2_wfxSbGzP3k5EUjUKuULAnmTwCdkdZQnl-SNU0Co"
},
});
json = response.getContentText();
var data = JSON.parse(json);
var listArtists = [];
for(var i = 0, len = data.artists.length; i < len; i++){
listArtists.push(data.artists[i].name);
}
return listArtists;
}
This works fine using the temporary Authorisation token from the Spotify website but this token refreshes every hour and so is obviously useless.
I am trying to use my own Authorisation token and ID which I have setup on Spotify however I'm struggling to make this work. As I understand it I may need to add an extra step at the beginning to start the authorisation process but I've tried all methods suggested but keep receiving server errors.

From the document, it seems that "Client Credentials Flow" uses the basic authorization.
In order to use this, at first, you are required to retrieve "client_id" and "client_secret".
Sample script:
var clientId = "### client id ###"; // Please set here.
var clientSecret = "### client secret ###"; // Please set here.
var url = "https://accounts.spotify.com/api/token";
var params = {
method: "post",
headers: {"Authorization" : "Basic " + Utilities.base64Encode(clientId + ":" + clientSecret)},
payload: {grant_type: "client_credentials"},
};
var res = UrlFetchApp.fetch(url, params);
Logger.log(res.getContentText())
From curl sample, grant_type is required to send as form.
Result:
The document says that the response is as follows.
{
"access_token": "NgCXRKc...MzYjw",
"token_type": "bearer",
"expires_in": 3600,
}
Note:
This is a simple sample script. So please modify this for your situation.
I prepared this sample script by the sample curl in the document.
Reference:
Client Credentials Flow
Edit:
As your next issue, you want to retrieve the access token from the returned value. If my understanding is correct, how about this modification? Please modify my script as follows.
From:
Logger.log(res.getContentText())
To:
var obj = JSON.parse(res.getContentText());
Logger.log(obj.access_token)
When the value is returned from API, it returns as a string. So it is required to parse it as an object using JSON.parse().

Related

Google sheets appscript - Pipedrive post

I am getting the error 'Exception: Request failed for https://api.pipedrive.com returned code 404. Truncated server response: {"status":false,"error":"Unknown method ."} (use muteHttpExceptions option to examine full response)' when attempting a put/post request to a Pipedrive deal. (Date Field) Below is the function producing the error. I am pretty sure this is something obvious but have not been able to determine the cause on my own. Any help would be appreciated.
function Update_pipedrive_Date(dealid, field_name){
var todays_date = Utilities.formatDate(new Date(), "GMT-5", "yyyy-MM-dd"); // "yyyy-MM-dd'T'HH:mm:ss'Z'"
var url = 'https://api.pipedrive.com/v1/deals/' + dealid + '?api_token='+oys_api_key
var data = {field_name: todays_date};
var options = {
'method': 'post',
'contentType': 'application/json',
'payload': JSON.stringify(data)
};
var response = UrlFetchApp.fetch(url, options);
return response
}
def update_field(this_dealid, fieldid):
tday = now.strftime("%Y-%m-%d")
headers = {'content-type': 'application/json'}
updates = {fieldid: tday}
resp = requests.put(
'https://api.pipedrive.com/v1/deals/' + str(
this_dealid) + '?api_token=' + api_key,
data=json.dumps(updates), headers=headers)
return resp
I believe your goal is as follows.
Your provided python script works fine.
You want to convert your python script to Google Apps Script.
Modification points:
Your python script requests the PUT method. But your Google Apps Script requests the POST method.
In the case of {field_name: todays_date}, the key is always "field_name".
When these points are reflected in your script, how about the following modification?
Modified script:
var field_name = "###"; // Please set your field_name.
var dealid = "###"; // Please set your dealid.
var oys_api_key = "###"; // Please set your oys_api_key.
var todays_date = Utilities.formatDate(new Date(), "GMT-5", "yyyy-MM-dd");
var url = 'https://api.pipedrive.com/v1/deals/' + dealid + '?api_token=' + oys_api_key;
var data = { [field_name]: todays_date };
var options = {
'method': 'put',
'contentType': 'application/json',
'payload': JSON.stringify(data)
};
var response = UrlFetchApp.fetch(url, options);
Note:
I think that the request of this Google Apps Script is the same as your python script. But if this script occurs an error, please confirm your variables of field_name, dealid, oys_api_key again. Even when these variables are the valid values, when an error occurs, I'm worried that the URL might not be able to be accessed from the Google side.
Reference:
fetch(url, params)

i am trying to connect to independentreserve api using google apps script

i think the issue is with the signature, the request body is same as in the independentreserve api docs. i am using apps script to connect with api, i tried using python and it works fine, but i am new to javascript and google apps script.
this is my code. can someone help with this?
function myFunction() {
var key = 'api-key'
var secret = 'api-secret'
var url = 'https://api.independentreserve.com/Private/GetOpenOrders'
// initialize nonce to current unix time in milliseconds
nonce = (new Date()).getTime();
// Set custom User-Agent string
var headers = {"User-Agent": "Independent Reserve Javascript API Client"};
var nonce = nonce++;
console.info("hELLO")
var message = [url, 'apiKey=' + key, 'nonce=' + nonce].join(',') ;
//var signer = crypto.createHmac('sha256', Buffer(secret, 'utf8'));
var signer = Utilities.computeDigest(Utilities.DigestAlgorithm.SHA_256, secret);
var signature = signer
.map(function(byte) {
// Convert from 2's compliment
var v = (byte < 0) ? 256 + byte : byte;
// Convert byte to hexadecimal
return ("0" + v.toString(16)).slice(-2);
}).join(',');
var headers = {'Content-Type': 'application/json', 'muteHttpExceptions': true};
var options = {
"apiKey": key,
"nonce": nonce,
"signature": signature,
"primaryCurrencyCode": "Xbt",
"secondaryCurrencyCode": "Usd",
"pageIndex": 1,
"pageSize": 25
}
var r = UrlFetchApp.fetch(url, options)
console.info(r.getContentText())
}
I believe your goal is as follows.
In order to request to api.independentreserve.com, you want to convert the following sample script of python. Ref
import time
import requests
import hmac
import hashlib
import json
from collections import OrderedDict
url = 'https://api.independentreserve.com/Private/GetOpenOrders'
key = 'api_key'
secret = 'api_secret'
nonce = int(time.time())
# make sure that parameter order is correct as specified in method documentation
parameters = [
url,
'apiKey=' + key,
'nonce=' + str(nonce),
'primaryCurrencyCode=Xbt',
'secondaryCurrencyCode=Usd',
'pageIndex=1',
'pageSize=10'
]
message = ','.join(parameters)
signature = hmac.new(
secret.encode('utf-8'),
msg=message.encode('utf-8'),
digestmod=hashlib.sha256).hexdigest().upper()
# make sure this collection ordered in the same way as parameters
data = OrderedDict([
('apiKey', key),
('nonce', nonce),
('signature', str(signature)),
('primaryCurrencyCode', 'Xbt'),
('secondaryCurrencyCode', 'Usd'),
('pageIndex', 1),
('pageSize', 10)])
headers = {'Content-Type': 'application/json'}
r = requests.post(url, data=json.dumps(data, sort_keys=False), headers=headers)
print(r.content)
When I saw your script, I thought the following modification points:
Utilities.computeDigest will be Utilities.computeHmacSha256Signature.
(new Date()).getTime() cannot be directly used. In this case, I thought that it's (new Date()).getTime().toString().slice(0, 10).
muteHttpExceptions cannot be used in the request header.
'Content-Type': 'application/json' can be used as contentType: "application/json". And in this case, it is required to convert the JSON object to the string value.
When the above python script is converted to Google Apps Script by modifying these points, it becomes as follows.
Sample script:
function myFunction() {
var key = 'api_key';
var secret = 'api_secret';
var url = 'https://api.independentreserve.com/Private/GetOpenOrders';
var nonce = (new Date()).getTime().toString().slice(0, 10);
var parameters = [
url,
'apiKey=' + key,
'nonce=' + nonce.toString(),
'primaryCurrencyCode=Xbt',
'secondaryCurrencyCode=Usd',
'pageIndex=1',
'pageSize=10'
];
var message = parameters.join(",");
var signature = Utilities.computeHmacSha256Signature(message, secret, Utilities.Charset.UTF_8).map(b => ('0' + (b & "0xFF").toString(16)).slice(-2)).join('');
var payload = {
"apiKey": key,
"nonce": nonce,
"signature": signature,
"primaryCurrencyCode": "Xbt",
"secondaryCurrencyCode": "Usd",
"pageIndex": 1,
"pageSize": 25
};
var options = {
method: "post",
contentType: "application/json",
payload: JSON.stringify(payload),
muteHttpExceptions: true,
};
var r = UrlFetchApp.fetch(url, options);
console.log(r.getContentText());
}
Note:
I confirmed that this request of the above Google Apps Script is the same as the sample python script. So, when an error occurs, please confirm your key, secret, and the values of parameters, again.
References:
Airbridge API
computeHmacSha256Signature(value, key, charset)
fetch(url, params)

How to integrate Gumroad API with Google Apps Script

I'm trying to see if a user is a paying customer for my gumroad product. I'm trying to integrate the Gumroad API to Google Apps Script.
I have the following code
function checkAccount(){
var token = <<token>>;
var userEmail = Session.getActiveUser().getEmail();
var url = "https://api.gumroad.com/v2/sales";
var headers = {"access_token=" : token};
var options = {
"method" : "GET",
"email" : userEmail,
"headers" : headers
};
var response = UrlFetchApp.fetch(url, options);
var jsonObject = JSON.parse(response.getContentText());
Logger.log(jsonObject);
}
I get the following error Exception: Request failed for https://api.gumroad.com returned code 401 which Gumroad is telling me 401 Unauthorized you did not provide a valid access token. I've checked the token and it's correct. I've logged the options and headers, and they show up correctly.
I'm just not sure why it's giving me a 401.
Try changing headers to this:
var headers = {
"Authorization": `access_token=${token}`
};
EDIT:
Based on this you could try:
headers: {
Authorization: `Bearer ${token}`}

How to use Google Photos API Method: mediaItems.search in Google apps script for a spreadsheet

I really tried to figure this out on my own...
I am trying to load photo metadata from google photos into a sheet using the Google Photos API and google apps script.
I was able to make some progress after a lot of help on a previous question
Is it possible to load google photos metadata into google sheets?
I now have two functions.
function photoAPI_ListPhotos() - Uses Method: mediaItems.list and gives me all my photos that are not archived
function photoAPI_ListAlbums() - Uses Method: albums.list and gives me all my albums
What I want to do is retrieve all photos from a specific album. Method: mediaItems.search should do this but it uses the POST protocol and the previous working examples I found only use GET. Looking at the examples available on that page, there is a javascript portion but it does not work in apps script.
The documentation for UrlFetchApp tells me how to format a POST request but not how to add the parameters for authentication.
The external APIs also is not giving me the examples I am looking for.
I feel like I'm missing some essential tiny piece of info and I hope I'm not wasting everyone's time asking it here. Just a solid example of how to use POST with oauth in apps script should get me where I need to go.
Here is my working function for listing all non-archived photos.
function photoAPI_ListPhotos() {
/*
This function retrieves all photos from your personal google photos account and lists each one with the Filename, Caption, Create time (formatted for Sheet), Width, Height, and URL in a new sheet.
it will not include archived photos which can be confusing if you happen to have a large chunk of archived photos some pages may return only a next page token with no media items.
Requires Oauth scopes. Add the below line to appsscript.json
"oauthScopes": ["https://www.googleapis.com/auth/spreadsheets.currentonly", "https://www.googleapis.com/auth/photoslibrary", "https://www.googleapis.com/auth/photoslibrary.readonly", "https://www.googleapis.com/auth/script.external_request"]
Also requires a standard GCP project with the appropriate Photo APIs enabled.
https://developers.google.com/apps-script/guides/cloud-platform-projects
*/
//Get the spreadsheet object
var ss = SpreadsheetApp.getActiveSpreadsheet();
//Check for presence of target sheet, if it does not exist, create one.
var photos_sh = ss.getSheetByName("photos") || ss.insertSheet("photos", ss.getSheets().length);
//Make sure the target sheet is empty
photos_sh.clear();
var narray = [];
//Build the request string. Max page size is 100. set to max for speed.
var api = "https://photoslibrary.googleapis.com/v1/mediaItems?pageSize=100";
var headers = { "Authorization": "Bearer " + ScriptApp.getOAuthToken() };
var options = { "headers": headers, "method" : "GET", "muteHttpExceptions": true };
//This variable is used if you want to resume the scrape at some page other than the start. This is needed if you have more than 40,000 photos.
//Uncomment the line below and add the next page token for where you want to start in the quotes.
//var nexttoken="";
var param= "", nexttoken;
//Start counting how many pages have been processed.
var pagecount=0;
//Make the first row a title row
var data = [
"Filename",
"description",
"Create Time",
"Width",
"Height",
"ID",
"URL",
"NextPage"
];
narray.push(data);
//Loop through JSON results until a nextPageToken is not returned indicating end of data
do {
//If there is a nextpagetoken, add it to the end of the request string
if (nexttoken)
param = "&pageToken=" + nexttoken;
//Get data and load it into a JSON object
var response = UrlFetchApp.fetch(api + param, options);
var json = JSON.parse(response.getContentText());
//Check if there are mediaItems to process.
if (typeof json.mediaItems === 'undefined') {
//If there are no mediaItems, Add a blank line in the sheet with the returned nextpagetoken
//var data = ["","","","","","","",json.nextPageToken];
//narray.push(data);
} else {
//Loop through the JSON object adding desired data to the spreadsheet.
json.mediaItems.forEach(function (MediaItem) {
//Check if the mediaitem has a description (caption) and make that cell blank if it is not present.
if(typeof MediaItem.description === 'undefined') {
var description = "";
} else {
var description = MediaItem.description;
}
//Format the create date as appropriate for spreadsheets.
var d = new Date(MediaItem.mediaMetadata.creationTime);
var data = [
MediaItem.filename,
"'"+description, //The prepended apostrophe makes captions that are dates or numbers save in the sheet as a string.
d,
MediaItem.mediaMetadata.width,
MediaItem.mediaMetadata.height,
MediaItem.id,
MediaItem.productUrl,
json.nextPageToken
];
narray.push(data);
});
}
//Get the nextPageToken
nexttoken = json.nextPageToken;
pagecount++;
//Continue if the nextPageToaken is not null
//Also stop if you reach 400 pages processed, this prevents the script from timing out. You will need to resume manually using the nexttoken variable above.
} while (pagecount<4 && nexttoken);
//Continue if the nextPageToaken is not null (This is commented out as an alternative and can be used if you have a small enough collection it will not time out.)
//} while (nexttoken);
//Save all the data to the spreadsheet.
photos_sh.getRange(1, 1, narray.length, narray[0].length).setValues(narray);
}
You want to retrieve all photos of the specific album using Google Photo API.
You want to know how to use the method of mediaItems.search using Google Apps Script.
You have already been able to retrieve the data using Google Photo API.
If my understanding is correct, how about this sample script? Please think of this as just one of several answers.
Sample script 1:
var albumId = "###"; // Please set the album ID.
var headers = {"Authorization": "Bearer " + ScriptApp.getOAuthToken()};
var url = "https://photoslibrary.googleapis.com/v1/mediaItems:search";
var mediaItems = [];
var pageToken = "";
do {
var params = {
method: "post",
headers: headers,
contentType: "application/json",
payload: JSON.stringify({albumId: albumId, pageSize: 100, pageToken: pageToken}),
}
var res = UrlFetchApp.fetch(url, params);
var obj = JSON.parse(res.getContentText());
Array.prototype.push.apply(mediaItems, obj.mediaItems);
pageToken = obj.nextPageToken || "";
} while (pageToken);
Logger.log(mediaItems)
At the method of mediaItems.search, albumId, pageSize and pageToken are included in the payload, and the values are sent as the content type of application/json.
Sample script 2:
When your script is modified, how about the following modified script?
function photoAPI_ListPhotos() {
var albumId = "###"; // Please set the album ID.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var photos_sh = ss.getSheetByName("photos") || ss.insertSheet("photos", ss.getSheets().length);
photos_sh.clear();
var narray = [];
var api = "https://photoslibrary.googleapis.com/v1/mediaItems:search";
var headers = { "Authorization": "Bearer " + ScriptApp.getOAuthToken() };
var nexttoken = "";
var pagecount = 0;
var data = ["Filename","description","Create Time","Width","Height","ID","URL","NextPage"];
narray.push(data);
do {
var options = {
method: "post",
headers: headers,
contentType: "application/json",
payload: JSON.stringify({albumId: albumId, pageSize: 100, pageToken: nexttoken}),
}
var response = UrlFetchApp.fetch(api, options);
var json = JSON.parse(response.getContentText());
if (typeof json.mediaItems === 'undefined') {
//If there are no mediaItems, Add a blank line in the sheet with the returned nextpagetoken
//var data = ["","","","","","","",json.nextPageToken];
//narray.push(data);
} else {
json.mediaItems.forEach(function (MediaItem) {
if(typeof MediaItem.description === 'undefined') {
var description = "";
} else {
var description = MediaItem.description;
}
var d = new Date(MediaItem.mediaMetadata.creationTime);
var data = [
MediaItem.filename,
"'"+description,
d,
MediaItem.mediaMetadata.width,
MediaItem.mediaMetadata.height,
MediaItem.id,
MediaItem.productUrl,
json.nextPageToken
];
narray.push(data);
});
}
nexttoken = json.nextPageToken || "";
pagecount++;
} while (pagecount<4 && nexttoken);
photos_sh.getRange(1, 1, narray.length, narray[0].length).setValues(narray);
}
Note:
This script supposes as follows.
Google Photo API is enabed.
The scope of https://www.googleapis.com/auth/photoslibrary.readonly or https://www.googleapis.com/auth/photoslibrary are included in the scopes.
Reference:
Method: mediaItems.search
If I misunderstood your question and this was not the result you want, I apologize.

Using Google Apps Script and the Pastebin.com API to post a paste

I am trying to make a Pastebin.com paste using Google Apps Script from the spreadsheet script editor. Can anyone tell me what I'm doing wrong?
function postPastebinPost() {
var options, url, apiKey, payload, response;
apiKey = <api key goes here>;
payload = 'Hello World';
options = {
'method' : 'post',
'payload' : payload
};
url = 'https://pastebin.com/api/api_post.php'
+ '?api_dev_key=' + apiKey
+ '&api_option=paste'
+ '&api_paste_code=' + encodeURIComponent(payload);
response = UrlFetchApp.fetch(url, options);
Logger.log(response);
}
I run this and my log reads Bad API request, invalid api_option. I've searched for solutions but I have not found any.
Documentation:
• Pastebin.com API
• Google Apps Script's UrlFetchApp Class
The parameters should be passed in the payload of the POST request.
function postPastebinPost() {
var apiKey = 'YOUR KEY GOES HERE';
var text = 'Hello World';
var payload = {
api_dev_key: apiKey,
api_option: 'paste',
api_paste_code: text
};
var options = {
method : 'POST',
payload: payload
};
var url = 'https://pastebin.com/api/api_post.php';
var response = UrlFetchApp.fetch(url, options);
Logger.log(response.getContentText());
}
The following is in case the user wants to create a new paste as part of their own Pastebin account (and not «Paste as a guest»). It's just an adaptation of Amit Agarwal's answer.
function postPastebinPost() {
var title = 'abc';
var contents = 'Hello World \n next line of content \n more text';
var payload = {
api_dev_key: 'aa6f3ab...', // https://pastebin.com/api#1
api_option: 'paste',
api_paste_name: title,
api_paste_code: contents,
api_paste_private: '0', // public paste
api_user_name: 'diccionario...', // name of your Pastebin account
api_user_password: 'dk398d...', // password to your Pastebin account
api_user_key: '39dk3...', // https://pastebin.com/api/api_user_key.html
};
var options = {
method : 'POST',
payload: payload
};
var url = 'https://pastebin.com/api/api_post.php';
var response = UrlFetchApp.fetch(url, options);
Logger.log(response.getContentText());
}
The whole documentation is at https://pastebin.com/api.