I have created a new script which creates "Google Form" on my google account. Following is the sample code:
function myFunction() {
var form = FormApp.create('New Form');
var item = form.addCheckboxItem();
item.setTitle('What condiments would you like on your hot dog?');
item.setChoices([
item.createChoice('Ketchup'),
item.createChoice('Mustard'),
item.createChoice('Relish')
]);
form.addMultipleChoiceItem()
.setTitle('Do you prefer cats or dogs?')
.setChoiceValues(['Cats','Dogs'])
.showOtherOption(true);
form.addPageBreakItem()
.setTitle('Getting to know you');
form.addDateItem()
.setTitle('When were you born?');
form.addGridItem()
.setTitle('Rate your interests')
.setRows(['Cars', 'Computers', 'Celebrities'])
.setColumns(['Boring', 'So-so', 'Interesting']);
Logger.log('Published URL: ' + form.getPublishedUrl());
Logger.log('Editor URL: ' + form.getEditUrl());
}
Next, make the API Executable by going to Publish > Deploy as API Executable
Now if I execute the code directly from the Google App Script, it works perfectly fine and the form is also getting created.
Now I am facing the problem in executing the code from the Google OAuth 2.0 Playground. For this, I followed the following steps:
- Visit https://console.developers.google.com and create a new project
- On the left-hand menu, select "Library"
In the App Script Library, search for "Apps Script API" and enable it
Next, go to the credentials menu and click on "Create Credentials" > OAuth client ID
On the next screen, select Web Application
Enter name the new web application
In the "Authorised JavaScript origins" set "http://localhost"
In the "Authorised redirect URIs" set "https://developers.google.com/oauthplayground" as currently we will be requiring the authentication response on the Google OAuth Playground. And click on Create.
On success, you will receive the "Client ID" & "Client secret" of your account which you wil be providing in the Google OAuth Playground to authenticate other users application.
Now visit https://developers.google.com/oauthplayground and click on the Settings Gear. In the drop down menu, check the "Use your own OAuth credentials" and enter the "OAuth Client ID" and "OAuth Client Secret" received on the step 9
Next, in the "Step 1 Select & authorize APIs" section, select the "Apps Script API v1" and further select the "https://www.googleapis.com/auth/forms" option and click Authorize
Next it will ask for the Authorization of the account of whose you wanna access the selected scope. In this I am using the same account on which the "App Script" for for creation code is created and the same from which the "Client ID" and "Client Secret" is generated.
The above step will generate the "Authorization code" and further you can generate the "Refresh token" and the "Access token".
Next, we have to consume the services to execute the google app script code. Click on the "List Possible Operations" and then select "Run Scripts"
- Enter the script ID in the PlayGround and then set the request Body by clicking on the "Enter Request Body" button. To understand the request body parameters, refer to the document https://developers.google.com/apps-script/api/reference/rest/v1/scripts/run
- Now click on the "Send Request"
After following all the above steps, we are getting the following authentication error:
POST /v1/scripts/{ScriptId}:run HTTP/1.1
Host: script.googleapis.com
Content-length: 95
Content-type: application/json
Authorization: Bearer {your authentication}
{
"function": "myFunction",
"parameters": [],
"sessionState": "Test",
"devMode": true
}
HTTP/1.1 403 Forbidden
Content-length: 126
X-xss-protection: 1; mode=block
X-content-type-options: nosniff
Transfer-encoding: chunked
Vary: Origin, X-Origin, Referer
Server: ESF
-content-encoding: gzip
Cache-control: private
Date: Fri, 26 Oct 2018 13:44:57 GMT
X-frame-options: SAMEORIGIN
Alt-svc: quic=":443"; ma=2592000; v="44,43,39,35"
Content-type: application/json; charset=UTF-8
{
"error": {
"status": "PERMISSION_DENIED",
"message": "The caller does not have permission",
"code": 403
}
}
Thanks for the solution in advance.
To solve the problem you need to create the credentials of the project you have created in the App Script and not to create a new project in the Google Console. Hence skip the step 1 mentioned in the question which says:
- Visit https://console.developers.google.com and create a new project
And open the App Script project. In the current case, the project is "Test Google Form API Personal"
Next, open the project as "Cloud Project" by clicking on the three-dot option menu and then select the "Cloud Project"
Now the Google console screen will get open. Create the OAuth credentials of the opened project.
来源:https://stackoverflow.com/questions/53010287/how-to-run-google-app-script-function-from-google-oauth-2-0-playground-the-cal