Hello i am trying to deploy an appscript project using the rest api for appscript, it gives me a 404 error in response, my code looks like this
The google appscript api is enabled on my account and i am using a gcp standard project in my script.
function createScriptDeployment(){
var token = ScriptApp.getOAuthToken();
var endPoint = 'https://script.googleapis.com/v1/projects';
var headers = {
'accept': "application/json",
'Authorization': 'Bearer ' + token
}
var body = {
'versionNumber': 2,
'manifestFileName': 'appsscript',
'description': 'second deployment test'
}
var options = {
'method': 'POST',
'headers': headers,
'contentType': 'application/json',
'payload': JSON.stringify(body),
'muteHttpExceptions': true
}
var url = endPoint+"/{scriptId}/deployments";
var response = UrlFetchApp.fetch(url,options);
Logger.log(response);
}
what could be the possible problem ?
any help is deeply appreciated
In line
var url = endPoint+"/{scriptId}/deployments";
replace {scriptId} with your script id from url
You can also use
var url = endPoint+'/' + ScriptApp.getScriptId() + '/deployments`;
If you deploy the same script you're running code at.
Edit:
Full script:
function createScriptDeployment(){
var token = ScriptApp.getOAuthToken();
var endPoint = 'https://script.googleapis.com/v1/projects';
var headers = {
'accept': "application/json",
'Authorization': 'Bearer ' + token
}
var body = {
'versionNumber': 2,
'manifestFileName': 'appsscript',
'description': 'second deployment test'
}
var options = {
'method': 'POST',
'headers': headers,
'contentType': 'application/json',
'payload': JSON.stringify(body),
'muteHttpExceptions': true
}
var url = endPoint+"/"+ScriptApp.getScriptId()+"/deployments";
var response = UrlFetchApp.fetch(url,options);
Logger.log(response);
}
This code works perfectly for me.
Related
I was trying to fetch data from Clearbit API and I get this message : " Exception: The parameters (String,(class),(class)) don't match the method signature for UrlFetchApp.fetch."
Before writing this post I tried to search online but couldn't find the answer.
I share with you my code :
function linkedInUrl(domain) {
let url = `https://company.clearbit.com/v2/companies/find?domain=${domain}`
const api_keys = ""
const header = {
method: 'GET',
headers: {'Authorization':'Bearer ' + api_keys}
}
options = {muteHttpExceptions: true};
var response = UrlFetchApp.fetch(url,header,options)
var data = JSON.parse(response.getContentText())
return "https://www.linkedin.com/"+data.linkedin.handle
}
function techUsed(domain) {
let url = `https://company.clearbit.com/v2/companies/find?domain=${domain}`
const api_keys = ""
const header = {
method: 'GET',
headers: {'Authorization':'Bearer ' + api_keys}
}
options = {muteHttpExceptions: true};
var response = UrlFetchApp.fetch(url,header,options)
var data = JSON.parse(response.getContentText())
tech = ""
for(let i = 0; i < data.tech.length; i++) {
tech += data.tech[i] +","
}
return tech
}
Does anyone have a clue ?
Thank you for your help.
The arguments of fetch(url, params) are url and params. I think that this is the reason for your current issue. So, please modify your script as follows.
From:
let url = `https://company.clearbit.com/v2/companies/find?domain=${domain}`
const api_keys = ""
const header = {
method: 'GET',
headers: {'Authorization':'Bearer ' + api_keys}
}
options = {muteHttpExceptions: true};
var response = UrlFetchApp.fetch(url,header,options)
To:
let url = `https://company.clearbit.com/v2/companies/find?domain=${domain}`;
const api_keys = "###"; // Please set your access token.
options = {
method: 'GET',
headers: { 'Authorization': 'Bearer ' + api_keys },
muteHttpExceptions: true
};
var response = UrlFetchApp.fetch(url, options);
Note:
In this case, your api_keys is valid value for using the API. Please be careful about this.
Unfortunately, I cannot know the response value from the API you want to use. So, if your script below var response = UrlFetchApp.fetch(url, options); occurs an error, please provide the sample value. By this, I would like to confirm your script.
Reference:
fetch(url, params)
You can try this :
function linkedInUrl(domain) {
let url = `https://company.clearbit.com/v2/companies/find?domain=${domain}`
const api_keys = ""
const header = {
method: 'GET',
headers: {'Authorization':'Bearer ' + api_keys}
}
options = {muteHttpExceptions: true};
var res
fetch(url,header,options) //api for the get request
.then(response => response.json())
.then(data => console.log('response',data))
}
I am utilizing the Permissions endpoint call of the Google Drive API v3 to assign reader and writer status to a folder within app scripts for a shared google drive. The main goal is to not send the notification email when the permission is shared with a user. I have included the parameter 'sendNotificationEmail: false' in the body of the request however the email is still being sent notifying the user. The code snippet that I am using is below. Am I including the 'sendNotificationEmail' parameter in the wrong part of the request?
if (currTabName == "Testing" && curRange.getColumn() == 2){
for (var i = row; i < (row+rows); i++) {
var Email = currSS.getRange(i,2,1,1).getValue();
try{
var url = 'https://www.googleapis.com/drive/v3/files/FileID/permissions';
var data = {
'role': 'writer',
'type': 'user',
'sendNotificationEmail': false,
'emailAddress': Email,
'supportsAllDrives': true,
}
var options = {
'method' : 'POST',
'contentType': 'application/json',
'headers' : { Authorization: 'Bearer ' + 'yourAccessToken' },
'payload' : JSON.stringify(data) // Convert JavaScript object to JSON string
};
var response = UrlFetchApp.fetch(url, options);
Logger.log(response);
}
catch(err) {
Logger.log(err.message);
At the method of Permissions: create of Drive API, sendNotificationEmail and supportsAllDrives are required to be included in the query parameter. I think that this is the reason for your issue. So how about the following modification?
From:
var url = 'https://www.googleapis.com/drive/v3/files/folderthatstatusisgrantedfor/permissions';
var data = {
'role': 'writer',
'type': 'user',
'sendNotificationEmail': false,
'emailAddress': Email,
'supportsAllDrives': true,
}
var options = {
'method' : 'POST',
'contentType': 'application/json',
'headers' : { Authorization: 'Bearer ' + 'yourAccessToken' },
'payload' : JSON.stringify(data) // Convert JavaScript object to JSON string
};
var response = UrlFetchApp.fetch(url, options);
To:
var folderId = "###"; // Please set the folder ID.
var url = `https://www.googleapis.com/drive/v3/files/${folderId}/permissions`;
url += "?sendNotificationEmail=false&supportsAllDrives=true";
var data = {
'role': 'writer',
'type': 'user',
'emailAddress': Email,
}
var options = {
'method': 'POST',
'contentType': 'application/json',
'headers': { Authorization: 'Bearer ' + 'yourAccessToken' },
'payload': JSON.stringify(data)
};
var response = UrlFetchApp.fetch(url, options);
Reference:
Permissions: create
I have successfully authenticated Google apps script to salesforce and can query records in Salesforce from Google apps script with the following from gsuitedevs:
function run() {
var service = getService();
if (service.hasAccess()) {
Logger.log("Success.");
var url = service.getToken().instance_url +
'/services/data/v24.0/chatter/users/me';
// Make the HTTP request using a wrapper function that handles expired
// sessions.
var response = withRetry(service, function() {
return UrlFetchApp.fetch(url, {
headers: {
Authorization: 'Bearer ' + service.getAccessToken(),
}
});
});
var result = JSON.parse(response.getContentText());
Logger.log(JSON.stringify(result, null, 2));
} else {
var authorizationUrl = service.getAuthorizationUrl();
Logger.log('Open the following URL and re-run the script: %s',
authorizationUrl);
}
}
However, when I run the following code, I get a 401:
function updateQuoteUrls3() {
var service = getService();
var accessToken = service.getAccessToken();
var quoteId = "a1Gf200000GkZex"; // ID
var folderUrl = "https://drive.google.com/drive/folders/DEFXXX"; // Google_Drive_Folde_URL__c
var sheetUrl = "https://docs.google.com/spreadsheets/d/XXX";
var instanceUrl = "https://na53.salesforce.com";
var queryUrl = "/services/data/v25.0/sobjects/i360__Quote__c/" + quoteId + ".json";
var payload = {
"Google_Drive_Folde_URL__c": folderUrl,
"Pricing_Sheet_URL__c": sheetUrl
};
var headers = {
Authorization: 'Bearer ' + service.getAccessToken(),
"Content-Type": "application/json"
};
var options = {
"method": "patch",
"contentType": "application/json",
"headers": headers,
"payload": JSON.stringify(payload)
};
var url = instanceUrl + queryUrl;
var response = UrlFetchApp.fetch(url, options);
return response; // HTTPresponse - https://developers.google.com/apps-script/reference/url-fetch/http-response
}
I cannot figure out why I am getting the following error:
returned code 401. Truncated server response: [{"message":"This session is not valid for use with the REST API","errorCode":"INVALID_SESSION_ID"}] (use muteHttpExceptions option to examine full response)
I new to this, have searched for hours, but have come up with nothing so far. Help is greatly appreciated.
I've been searching how to do this, but i didn't find anything. After a while, i came up with the script i'll post as an answer.
I'm posting this for myself and anyone who might find it useful.
You'll need to get a dropbox access token, that can be obtained after creating a dropbox app.
function send2dropbox(file) {
var dropboxTOKEN = 'XXXxxx';
var path = '/somePath/' + file.getName();
var dropboxurl = 'https://api.dropboxapi.com/2/files/save_url';
var fileurl = 'https://drive.google.com/uc?export=download&id=' + file.getId();
var headers = {
'Authorization': 'Bearer ' + dropboxTOKEN,
'Content-Type': 'application/json'
};
var payload = {
"path": path,
"url": fileurl
}
var options = {
method: 'POST',
headers: headers,
payload: JSON.stringify(payload)
};
var response = UrlFetchApp.fetch(dropboxurl, options);
return response;
}
You can find an example HERE
streak api: https://www.streak.com/api/
I tried to use this streak api with GAS UrlService and found the right syntax for get requests. But I don't find the rigth syntax for put and post:
(1) f.e. create box
var RequestArguments = {
"contentType": "application/json",
"headers":{
"User-Agent": "MY_APP_NAME",
"Authorization": "Basic " + Utilities.base64Encode(streakApiKey),
},
"validateHttpsCertificates" :false,
"method": "PUT",
"????": "????"
};
var result = UrlFetchApp.fetch(RequestUrl,RequestArguments);
(2) f.e. edit box
var RequestArguments = {
"contentType": "application/json",
"headers":{
"User-Agent": "MY_APP_NAME",
"Authorization": "Basic " + Utilities.base64Encode(streakApiKey),
},
"validateHttpsCertificates":false,
"method": "PUT",
"????": "????"
};
var result = UrlFetchApp.fetch(RequestUrl,RequestArguments);
I use such way in my Chrome extension, didn't try this code but should be similar in GAS:
"url": "https://www.streak.com/api/v1/boxes/"+boxKey+"/fields/"+fieldKey;
"type": 'POST',
"dataType": "json",
"contentType": 'application/json; charset=utf-8',
"data": {value: yourValue}
It took me some time to figure out the right combination of parameters (Streak API documentation is not friendly in this part), but what I see in your code looks alright to me. It should work.
Here's the function you can use to edit a field in an existing box. Creating a new box will follow the same format.
function editBox(boxKey, fieldKey, value) {
var url = 'https://www.streak.com/api/v1/boxes/' + boxKey + '/fields/' + fieldKey;
var params = {
headers: {Authorization: 'Basic ' + Utilities.base64Encode(STREAK_API_KEY + ":")},
method: "POST",
payload: JSON.stringify({
value: value
}),
contentType: 'application/json'
};
var field = UrlFetchApp.fetch(url,params );
return JSON.parse(field.getContentText());
}
This is working:
function editBox() {
var boxKey = "xxx";
var value = "{value:Test}";
var fieldKey = "1001";
//{name:"Deal Size", key:"1001", type:"TEXT_INPUT", lastUpdatedTimestamp:1457089319053}
var url = 'https://www.streak.com/api/v1/boxes/' + boxKey + '/fields/' + fieldKey;
var params = {
headers: {Authorization: 'Basic ' + Utilities.base64Encode(streakApiKey)},
method: "POST",
payload: value,
contentType: 'application/json'
};
var result = UrlFetchApp.fetch(url,params );
var string = result.getContentText();
var Object = JSON.parse(string);
return Object;
}
function createBox() {
var pipelineKey = "xxx";
var name = "sample box name";
var url = 'https://www.streak.com/api/v1/pipelines/' + pipelineKey + '/boxes';
var RequestArguments = {
headers: {"Authorization": "Basic " + Utilities.base64Encode(streakApiKey)},
method: "PUT",
payload: {
name: name
}
};
var box = UrlFetchApp.fetch(url,RequestArguments);
var result = JSON.parse(box.getContentText());
return result;
}