4

I have created a new script which creates "Google Form" on my google account. Following is the sample code: enter image description here

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 enter image description here

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:

  1. Visit https://console.developers.google.com and create a new project
  2. On the left-hand menu, select "Library"
  3. In the App Script Library, search for "Apps Script API" and enable it enter image description here

  4. Next, go to the credentials menu and click on "Create Credentials" > OAuth client ID enter image description here

  5. On the next screen, select Web Application

  6. Enter name the new web application

  7. In the "Authorised JavaScript origins" set "http://localhost"

  8. 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.

  9. 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. enter image description here

  10. 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 enter image description here

  11. 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 enter image description here

  12. 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. enter image description here

enter image description here

  1. The above step will generate the "Authorization code" and further you can generate the "Refresh token" and the "Access token". enter image description here

  2. 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 image description here

    1. Next a Syntax will be generated, asking for the script ID which you can find from the google app script project. For this, on the google App Script project, Click on File > Project Properties and finally a pop-up will open refering the script ID enter image description here

enter image description here

enter image description here

  1. 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

enter image description here

enter image description here

  1. 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
  }
}

enter image description here

Thanks for the solution in advance.

Kamaldeep Singh
  • 765
  • 2
  • 8
  • 28
  • 1
    Have you deployed your project as API executable? – TheMaster Oct 26 '18 at 15:27
  • Yes, I have done that as well and edited my question with the screenshot of "Deploy as API Executable" but still getting the same error. – Kamaldeep Singh Oct 27 '18 at 04:55
  • 1
    client ID and client Secret are required to be retrieved for the project of script. At your question, you say ``1. Visit https://console.developers.google.com and create a new project``. In this case, I worry about that the project of your script might be difference from the created project. So can you confirm this? – Tanaike Oct 27 '18 at 05:21
  • 2
    If that is different, please do "Create Credentials" for the project of script. At the script editor which opens the script with ``myFunction()``, please open the API console like Resource -> Could Platform project -> View API console, and do "Create Credentials" and retrieved client ID and client secret. Then, try from ``10. Now visit https://developers.google.com/oauthplayground and click on the Settings Gear.`` again. If this was not the direct solution of your issue, I'm sorry. – Tanaike Oct 27 '18 at 05:21
  • Hi @Tanaike. I used the same google account for both i.e. creating the app script and creating the project on the google console to generate the client ID. Though both the project are different as creating the project at one place doesn't show up on the second place. You can have an idea by viewing the snapshot on the following link. https://photos.app.goo.gl/zwer5oduBVAtvF4G9 – Kamaldeep Singh Oct 27 '18 at 15:03
  • 1
    @Kamal You could access it from the script editor as Tanaike said in his last comment. – TheMaster Oct 27 '18 at 16:29
  • @Tanaike your solution helped me. Instead of going to Resources > Cloud Platform Project > View API Console > Create Credentials, one can directly access the Cloud platform project by directly accessing the projects in the app script and then from the menu selecting "Cloud Project" Thanks for the support. – Kamaldeep Singh Oct 28 '18 at 14:40
  • 1
    @Kamaldeep Singh I'm glad your issue was solved. – Tanaike Oct 28 '18 at 22:12

1 Answers1

1

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:

  1. 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"

enter image description here

Next, open the project as "Cloud Project" by clicking on the three-dot option menu and then select the "Cloud Project"

enter image description here

Now the Google console screen will get open. Create the OAuth credentials of the opened project.

enter image description here

Kamaldeep Singh
  • 765
  • 2
  • 8
  • 28