Using Dall-E in google appscript - google-apps-script

I know that it is possible to implement dall-e as a function in google sheets but I need your help to get it right.
openai is not defined and I don't know how to do it correctly.
function dalle() {
var setsheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Data");
var apiKey = setsheet.getRange(8,2).getValue()
response = openai.Image.create(
prompt="a white siamese cat",
n=1,
size="1024x1024"
)
image_url = response['data'][0]['url']
}

You copied the documentation from the OpenAI documentation, but it's mentioned that's only working for node.js or python.
For Apps Script, you have to use UrlFetch.fetch with the correct endpoint.
Example:
function call() {
var url = 'https://api.openai.com/v1/images/generations';
var options = {
"method": 'POST',
"headers": {
'Content-Type': 'application/json',
'Authorization': `Bearer YOUR_API_KEY`
},
"payload": JSON.stringify({
"model": "image-alpha-001",
"prompt": "a white siamese cat",
"num_images": 1,
"size": "256x256",
"response_format": "url"
}),
muteHttpExceptions: true
}
var response = UrlFetchApp.fetch(url, options);
var responseJson = response.getContentText();
var responseData = JSON.parse(responseJson);
Logger.log(responseData);
}
Reference:
UrlFetch
OpenAI authentification (see CURL method)

Related

can I be shown how to fix my syntax error : "unexpected token var line 65"?

(1:) I am writing a webhook script. (2:) Using "google sheets" and it's script editor. It is not liking how I used the below syntax.
`function sendMessage(message, channel)
{
if(webhooks(channel))
var url = webhooks[channel];
else {
Logger.log("Error Sending Message to Channel " + channel);
return "NoStoredWebhookException";
}
var payload = JSON.stringify({content: message});
Logger.log(options, null, 2);
UrlFetchApp.fetch("https://discord.com/api/webhooks/1069728124704653413/_h9KQdxqenUWGSSE5xR91QgBU28bdcUcY2yGslu83KezXvUTEv7BlJzvYo-mVTBv5_ye", options);
var webhooks = {
test: "Obtain a webhook for the channel you'd like and put it here."
var params = {
headers: {"Content-Type": "application/x-www-form-urlencoded"},
method: "POST",
payload: payload,
muteHttpExceptions: true
};
var res = UrlFetchApp.fetch(url, params);
Logger.log(res.getContentText());
}`
You didn't close the second brace on the webhooks object
var webhooks = {
test: "Obtain a webhook for the channel you'd like and put it here."
}

OAuth 2.0 is not enabled for method Error with jira cloud with App-script

I have implemented oauth2 (3LO) using App-script for integrating Google Sheet and Jira. I have set var SCOPE = read:jira-user read:jira-work write:jira-work
When I use any get request, system works fine. But when I try to make put or post request, system throws error. See this image below:
[][enter code here1]
See here my put request for edit an issue
I am not sure what is the issue here. I have checked that I have enabled oauth2 and as an administrator, I have all power. I have also proper scope. And this configuration works for any get request but it does not work with POST and PUT requests.
Could you please let me know if you have an idea or clue how I can make it work or is there anything I can check to make sure my confirmation is right for POST or PUT requests?
function updateIssue() {
var service = getService();
var issueIdOrKey = 'CP-16'
Logger.log(service.hasAccess());
if (service.hasAccess()) {
var data = `{
"summary":"New summary version 1"
}`;
var route = `/rest/api/3/issue/${issueIdOrKey}`;
var cloudid = getCloudId(service);
var url = 'https://api.atlassian.com/ex/jira/' + cloudid + route;
Logger.log(url);
var response = UrlFetchApp.fetch(url, {
'Method': 'PUT',
headers: {
//'Method': 'PUT',
'Accept': 'application/json',
'Authorization': 'Bearer ' + service.getAccessToken(),
// Convert the JavaScript object to a JSON string.
'ContentType': 'application/json' //, 'payload': JSON.stringify(data)
},
'payload': JSON.stringify(data)
});
var result = JSON.parse(response.getContentText());
Logger.log(JSON.stringify(result, null, 2));
}
}
Thank you
Modification points:
In your script, at var data = {"summary":"New summary version 1"};, data` has already been converted to the string.
When 'ContentType' is used, I thought that it was 'Content-Type'. But, in your script, I thought that 'contentType': 'application/json' can be used outside of the request header.
When these points are reflected in your script, it becomes as follows.
Modified script:
function updateIssue() {
var service = getService();
var issueIdOrKey = 'CP-16'
Logger.log(service.hasAccess());
if (service.hasAccess()) {
var data = { "summary": "New summary version 1" }; // Modified
var route = `/rest/api/3/issue/${issueIdOrKey}`;
var cloudid = getCloudId(service);
var url = 'https://api.atlassian.com/ex/jira/' + cloudid + route;
Logger.log(url);
var response = UrlFetchApp.fetch(url, {
'Method': 'PUT',
'headers': { // Modified
'Accept': 'application/json',
'Authorization': 'Bearer ' + service.getAccessToken(),
},
'contentType': 'application/json', // Added
'payload': JSON.stringify(data)
});
var result = JSON.parse(response.getContentText());
Logger.log(JSON.stringify(result, null, 2));
}
}
Note:
In this modification, it supposes that your access token and your endpoint, and your request body are valid values. Please be careful about this. If an error occurs, please show the error message and please provide the official document of the API you want to use. By this, I would like to confirm it.
Reference:
fetch(url, params)

How Can I use Dropbox Refresh Tokens in Google Apps Script to transfer Drive Files to Dropbox?

Based on several examples, I wrote a function in Google apps script that takes a given input file and saves it at the specified location on Dropbox. It does this using a short lived access token obtained from the Dropbox apps console...
function driveToDropbox(inputFile, targetFilePath, writeMode='add') {
//inputFile = Drive file object, targetFilePath = string, writeMode = 'overwrite' or 'add'
inputFile = DriveApp.getFilesByName('temp.html').next(); //for testing
targetFilePath = '/temp.html' //for testing
var apiUrl = 'https://content.dropboxapi.com/2/files/upload';
var dropboxAccessToken = '<SL Token>';
var parameters = {
path: targetFilePath,
mode: writeMode,
autorename: true,
mute: false,
strict_conflict: false,
};
var headers = {
'Content-Type': 'application/octet-stream',
Authorization: 'Bearer ' + dropboxAccessToken,
'Dropbox-API-Arg': JSON.stringify(parameters),
};
var options = {
method: 'POST',
headers: headers,
payload: inputFile.getBlob().getBytes(),
};
var response = JSON.parse(UrlFetchApp.fetch(apiUrl, options).getContentText());
//Logger.log(response);
Logger.log('File uploaded successfully');
}
This will only work for 4 hours, so I obtained the refresh token for the app through the standard flow, and with this cURL command via terminal I am able to generate new working SL tokens.
curl https://api.dropbox.com/oauth2/token -d grant_type=refresh_token -d refresh_token=<R Token> -u <App key>:<App secret>
My problem is converting that CURL command into Google apps Javascript and pulling the returned SL token. This was my attempt...
var apiUrl = 'https://api.dropbox.com/oauth2/token';
var refreshToken = '<R Token>';
var appKey = '<App key>';
var appSecret = '<App secret>';
var appKeySecret = '<Combined app key+secret>'
var parameters = {
refresh_token: refreshToken,
client_id: appKey,
client_secret: appSecret,
};
var headers = {
'Content-Type': 'application/json',
Authorization: 'Basic ' + appKeySecret
'Dropbox-API-Arg': JSON.stringify(parameters),
};
var options = {
method: 'POST',
headers: headers,
grant_type: "refresh_token",
muteHttpExceptions: true,
};
var response = JSON.parse(UrlFetchApp.fetch(apiUrl, options).getContentText());
Logger.log(response);
My error response is "{error_description=missing required field "grant_type", error=unsupported_grant_type}" confirming that my issue is misunderstanding the formatting, but I'm not sure how to fix it.
After fixing that, I would parse out the SL token from the response and then use that with the working file upload code above. Is there something obvious that I am missing with the formatting?
EDIT: Here is the working function based on the selected answer. This should probably be broken into two functions.
function driveToDropbox(inputFile, targetFilePath, writeMode='add') {
//inputFile = Drive file object, targetFilePath = string, writeMode = 'overwrite' or 'add'
inputFile = DriveApp.getFilesByName('temp.html').next(); //for testing
targetFilePath = '/temp.html'; //for testing
////Obtain new 4 hour SL access token with Refresh token
var refreshToken = '<R Token>';
var appKey = '<App key>';
var appSecret = '<App secret>';
var apiUrl = 'https://api.dropbox.com/oauth2/token';
var options = {
method: 'POST',
headers: { "Authorization": "Basic " + Utilities.base64Encode(`${appKey}:${appSecret}`) },
payload: {
grant_type: "refresh_token",
refresh_token: refreshToken
},
muteHttpExceptions: true,
};
var response = UrlFetchApp.fetch(apiUrl, options);
var accessToken = JSON.parse(response.getContentText()).access_token;
////Transfer file with refreshed SL access token
apiUrl = 'https://content.dropboxapi.com/2/files/upload';
var parameters = {
path: targetFilePath,
mode: writeMode,
autorename: true,
mute: false,
strict_conflict: false,
};
options = {
method: 'POST',
headers: {
'Content-Type': 'application/octet-stream',
Authorization: 'Bearer ' + accessToken,
'Dropbox-API-Arg': JSON.stringify(parameters),
},
payload: inputFile.getBlob().getBytes(),
};
response = JSON.parse(UrlFetchApp.fetch(apiUrl, options).getContentText());
Logger.log('File uploaded successfully');
}
I believe your goal is as follows.
You want to convert the following curl command to Google Apps Script.
curl https://api.dropbox.com/oauth2/token -d grant_type=refresh_token -d refresh_token= -u :
In this case, how about the following modification?
Modified script:
Please set your values of refreshToken, appKey, appSecret.
var refreshToken = '<R Token>';
var appKey = '<App key>';
var appSecret = '<App secret>';
var apiUrl = 'https://api.dropbox.com/oauth2/token';
var options = {
method: 'POST',
headers: { "Authorization": "Basic " + Utilities.base64Encode(`${appKey}:${appSecret}`) },
payload: {
grant_type: "refresh_token",
refresh_token: refreshToken
},
muteHttpExceptions: true,
};
var response = UrlFetchApp.fetch(apiUrl, options);
var obj = JSON.parse(response.getContentText());
var accessToken = obj.access_token; // You can retrieve the access token.
console.log(accessToken);
When I saw your sample curl command, it seems that it is required to send grant_type and refresh_token as the form data. And, the basic authorization is used.
Note:
In this answer, it supposes that your sample curl command works. Please be careful about this.
Reference:
fetch(url, params)

How to request specific data from the Google Search Console API using Google Apps Script?

I'm trying to recover Google Discover data from the Google Search Console through its API and display it in a Google Sheet.
I'm following a tutorial that walked me through setting up the needed authentications in the Google Cloud platform and showed me how to set up a basic request that retrieves a list of websites I have access to in the Search Console.
What I don't understand is how to modify the code below so that the API retrieves Google Discover data instead of a list of websites I have access to.
The Google Apps Script code gets the mentioned websites and populates two columns, one with the website link and another with the access level I have for that website.
function listAccountSites() {
var service = getService();
if (service.hasAccess()) {
var apiURL = "https://www.googleapis.com/webmasters/v3/sites";
var headers = {
"Authorization": "Bearer " + getService().getAccessToken()
};
var options = {
"headers": headers,
"method" : "GET",
"muteHttpExceptions": true
};
var response = UrlFetchApp.fetch(apiURL, options);
var json = JSON.parse(response.getContentText());
Logger.log(json)
var URLs = []
for (var i in json.siteEntry) {
URLs.push([json.siteEntry[i].siteUrl, json.siteEntry[i].permissionLevel]);
}
s_sites.getRange(2,1,URLs.length,2).setValues(URLs);
} else {
var authorizationUrl = service.getAuthorizationUrl();
Logger.log('Open the following URL and re-run the script: %s', authorizationUrl);
Browser.msgBox('Open the following URL and re-run the script: ' + authorizationUrl)
}
}
Using this Google documentation page I was able to construct an API request that would get the Google Discover data I need, but as I said I cannot figure out how to do this. The mentioned request gets the data I need is the following one:
{
"startDate": "2021-11-01",
"endDate": "2021-11-05",
"dimensions": [
"PAGE"
],
"type": "DISCOVER"
}
I believe your goal is as follows.
You want to achieve the following request using Google Apps Script. Ref
POST https://www.googleapis.com/webmasters/v3/sites/https%3A%2F%2Fwww.example.com%2F/searchAnalytics/query?key={MY_API_KEY}
{
"startDate": "2015-04-01",
"endDate": "2015-05-01",
"dimensions": ["country","device"]
}
In your situation, you want to use the following request body.
{
"startDate": "2021-11-01",
"endDate": "2021-11-05",
"dimensions": [
"PAGE"
],
"type": "DISCOVER"
}
If my understanding is correct, how about the following sample script?
Sample script:
This sample script supposes that your access token can be used and you have already been able to use the API. Please be careful this.
var siteUrl = "https://www.example.com/"; // Please set the site URL.
var apiURL = `https://www.googleapis.com/webmasters/v3/sites/${encodeURIComponent(siteUrl)}/searchAnalytics/query`;
var headers = { "Authorization": "Bearer " + getService().getAccessToken() };
var payload = {
"startDate": "2021-11-01",
"endDate": "2021-11-05",
"dimensions": ["PAGE"],
"type": "DISCOVER"
};
var options = {
"headers": headers,
"method": "POST",
"muteHttpExceptions": true,
"contentType": "application/json",
"payload": JSON.stringify(payload),
};
var response = UrlFetchApp.fetch(apiURL, options);
References:
Search Analytics: query
fetch(url, params)

How to filter Google Drive API v3 mimeType?

I wrote a script that uses DriveApp to read Google Drive, SpreadsheetApp to log data in Sheets, and Google Drive API v3 + service account + OAuth to make changes on behalf of G Suite users.
It would be nice to search Google Drive from the target user perspective (calling Google Drive API v3) instead of the account running the script (calling DriveApp). I can't get the filter to work.
The query is built with parent folder keys, mimeType = or mimeType != for folders, and is passed into the function. The format is:
var query = "('GoogleDriveFolderKey01' in parents or 'GoogleDriveFolderKey02' in parents) and trashed = false and mimeType = 'application/vnd.google-apps.folder'"
The DriveApp function uses:
files = Drive.Files.list({
q: query,
maxResults: 100,
pageToken: pageToken
});
The Google Drive API v3 function uses:
var url = 'https://www.googleapis.com/drive/v3/files/'
var options = {
'contentType': 'application/json',
'method' : 'get',
'headers' : { Authorization: 'Bearer ' + service.getAccessToken() },
'muteHttpExceptions': true,
'corpora' : 'domain',
'q' : query,
'spaces' : 'drive',
'pageSize' : 100,
'pageToken' : pageToken
};
var response = UrlFetchApp.fetch(url, options);
var resultParsed = JSON.parse(response.getContentText());
files = resultParsed.files
pageToken = resultParsed.pageToken
The results with DriveApp are as expected, but Google Drive API v3 results in:
"files": [
{
"kind": "drive#file",
"id": "01abc123_etc",
"name": "Something something (2021-04-15).pdf",
"mimeType": "application/pdf"
},
{
"kind": "drive#file",
"id": "02ABC4321-qwertyuiop",
"name": "Super Special Worksheet",
"mimeType": "application/vnd.google-apps.spreadsheet"
},
{
"kind": "drive#file",
"id": "whatever",
"name": "Copy of V1",
"mimeType": "application/vnd.google-apps.folder"
},
...
Any ideas?
Edit:
Thank you! The problem seems to be 'corpus' and 'space'. I have used to following in Google AppsScript:
var options = {
'contentType': 'application/json',
'method' : 'get',
'headers' : { Authorization: 'Bearer ' + service.getAccessToken() },
'muteHttpExceptions': true,
'pageSize' : 100
};
url += '?q=' + encodeURIComponent(query);
if ( pageToken.length > 0 ) url += '&pageToken=' + pageToken;
var response = UrlFetchApp.fetch(url, options);
How about this answer?
Modification points:
At the method of "Files: list" in Drive API v3, the values of corpora, q, space, pageSize, pageToken are used as the query parameters. I think that the reason of your issue is due to this.
At GET method, contentType is not required.
Modified script:
When your script is modified, it becomes as follows.
var query = "('GoogleDriveFolderKey01' in parents or 'GoogleDriveFolderKey02' in parents) and trashed = false and mimeType = 'application/vnd.google-apps.folder'"
var url = 'https://www.googleapis.com/drive/v3/files' // Modified
var options = { // Modified
'method' : 'get',
'headers' : { Authorization: 'Bearer ' + service.getAccessToken() },
'muteHttpExceptions': true,
};
url += `?corpora=domain&q=${encodeURIComponent(query)}&spaces=drive&pageSize=100&pageToken=${pageToken}`; // Added
var response = UrlFetchApp.fetch(url, options);
var resultParsed = JSON.parse(response.getContentText());
files = resultParsed.files
pageToken = resultParsed.pageToken
Note:
This modified script supposes that service.getAccessToken() can be used for using the method of "Files: list" in Drive API v3.
If an error occurs please remove corpora=domain from the URL like below and test it again.
url += `?q=${encodeURIComponent(query)}&spaces=drive&pageSize=100&pageToken=${pageToken}`;
Reference:
Files: list