google-apps-scriptgoogle-formsoauth2-playgroundgoogle-apps-script-apigoogle-playground

How to run Google App Script function from Google OAuth 2.0 Playground | The caller does not have permission


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.


Solution

  • 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