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
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))
}
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.
I am using POST https://www.googleapis.com/gmail/v1/users/userId/settings/sendAs and able to add alias id with a signature in Gsuite account however with same code I am trying to update the signature for primary Gsuite account but it's not updating.
Please guide how to apply a logic to update the primary Gsuite account signature using a service account
https://developers.google.com/gmail/api/v1/reference/users/settings/sendAs
var service_account = {
"private_key":"-----BEGIN PRIVATE KEY-----VE=\n-----END PRIVATE KEY-----\n",
"client_email":"xxxxxx",
"client_id": "xxxxxxx",
"userEmail" = 'admin#demo.in';
};
function getOAuthService(user) {
return OAuth2.createService("Service Account")
.setAuthorizationBaseUrl('https://accounts.google.com/o/oauth2/auth')
.setTokenUrl('https://accounts.google.com/o/oauth2/token')
.setPrivateKey(service_account.private_key)
.setIssuer(service_account.client_email)
.setSubject(userEmail)
.setPropertyStore(PropertiesService.getScriptProperties())
.setParam('access_type', 'offline')
.setParam('approval_prompt', 'force')
.setScope('https://www.googleapis.com/auth/gmail.settings.sharing https://www.googleapis.com/auth/gmail.settings.basic');
}
function createAlias() {
var userEmail = 'admin#demo.in';
//var alias = 'Testing#demo.in';
//var alias_name = ' User';
var signature = 'Testing';
var service = getOAuthService();
service.reset();
if (service.hasAccess()) {
var url = 'https://www.googleapis.com/gmail/v1/users/me/settings/sendAs'
var headers ={
"Authorization": 'Bearer ' + service.getAccessToken(),
"Accept":"application/json",
"Content-Type":"application/json",
};
var resource ={
sendAsEmail: alias,
signature: signature,
replyToAddress : alias,
treatAsAlias: true,
verificationStatus: 'accepted',
isPrimary:true,
isDefault:true
};
var options = {
'headers': headers,
'method': 'POST',
'payload':JSON.stringify(resource),
'muteHttpExceptions': true
};
Logger.log(options);
var response = UrlFetchApp.fetch(url, options);
Logger.log(response.getContentText());
}
}
function reset() {
var service = getOAuthService();
service.reset();
}
You need to use the method Users.settings.sendAs: patch
In Apps Script, you can it as following:
function changeSignature() {
var userId = 'your primary email';
var sendAsEmail = userId;
var signature = 'Testing';
var service = getService();
service.reset();
if (service.hasAccess()) {
var url = 'https://www.googleapis.com/gmail/v1/users/'+userId+'/settings/sendAs/'+sendAsEmail
var headers ={
"Authorization": 'Bearer ' + service.getAccessToken(),
"Accept":"application/json",
"Content-Type":"application/json",
};
var resource ={
"signature": signature,
};
var options = {
'headers': headers,
'method': 'PATCH',
'payload':JSON.stringify(resource),
'muteHttpExceptions': true
};
var response = UrlFetchApp.fetch(url, options);
Logger.log(response.getContentText());
}
}
You seem to already do it correctly, but just to point it out:
When you build your service account, it is important to specify the scope https://www.googleapis.com/auth/gmail.settings.basic' and to include .setSubject(userEmail)
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;
}