简体   繁体   English

如何从Google OAuth 2.0 Playground运行Google App Script功能| 来电者没有权限

[英]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. 我创建了一个新脚本,在我的Google帐户上创建“Google表单”。 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 接下来,通过转到Publish> Deploy as API Executable来生成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. 现在,如果我直接从Google App脚本执行代码,它的工作完全正常,表单也会被创建。

Now I am facing the problem in executing the code from the Google OAuth 2.0 Playground . 现在我遇到了从Google OAuth 2.0 Playground执行代码的问题 For this, I followed the following steps: 为此,我按照以下步骤操作:

  1. Visit https://console.developers.google.com and create a new project 访问https://console.developers.google.com并创建一个新项目
  2. On the left-hand menu, select "Library" 在左侧菜单中,选择“库”
  3. In the App Script Library, search for "Apps Script API" and enable it 在App Script Library中,搜索“Apps Script API”并启用它 在此输入图像描述

  4. Next, go to the credentials menu and click on "Create Credentials" > OAuth client ID 接下来,转到凭据菜单,然后单击“创建凭据”> OAuth客户端ID 在此输入图像描述

  5. On the next screen, select Web Application 在下一个屏幕上,选择Web Application

  6. Enter name the new web application 输入新Web应用程序的名称

  7. In the "Authorised JavaScript origins" set " http://localhost " 在“Authorized JavaScript origin”中设置“ 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. 在“授权重定向URI”中设置“ https://developers.google.com/oauthplayground ”,因为目前我们需要在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. 成功后,您将收到您在Google OAuth Playground中提供的帐户的“客户ID”和“客户机密”,以对其他用户应用程序进行身份验证。 在此输入图像描述

  10. Now visit https://developers.google.com/oauthplayground and click on the Settings Gear. 现在访问https://developers.google.com/oauthplayground并点击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 在下拉菜单中,选中“使用您自己的OAuth凭据”,然后输入在步骤9中收到的“OAuth客户端ID”和“OAuth客户端密钥” 在此输入图像描述

  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 接下来,在“步骤1选择和授权API”部分中,选择“Apps Script API v1”并进一步选择“ https://www.googleapis.com/auth/forms ”选项,然后单击“授权”。 在此输入图像描述

  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. 在这里,我使用相同的帐户,在该帐户上创建用于创建代码的“App Script”,并且生成“Client ID”和“Client Secret”。 在此输入图像描述

在此输入图像描述

  1. The above step will generate the "Authorization code" and further you can generate the "Refresh token" and the "Access token". 上面的步骤将生成“授权代码”,您还可以生成“刷新令牌”和“访问令牌”。 在此输入图像描述

  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" 单击“列出可能的操作”,然后选择“运行脚本” 在此输入图像描述

    1. Next a Syntax will be generated, asking for the script ID which you can find from the google app script project. 接下来将生成一个语法,询问您可以从谷歌应用程序脚本项目中找到的脚本ID。 For this, on the google App Script project, Click on File > Project Properties and finally a pop-up will open refering the script ID 为此,在Google App Script项目上,单击文件>项目属性,最后将打开一个弹出窗口,引用脚本ID 在此输入图像描述

在此输入图像描述

在此输入图像描述

  1. Enter the script ID in the PlayGround and then set the request Body by clicking on the "Enter Request Body" button. 在PlayGround中输入脚本ID,然后单击“输入请求正文”按钮设置请求正文。 To understand the request body parameters, refer to the document https://developers.google.com/apps-script/api/reference/rest/v1/scripts/run 要了解请求正文参数,请参阅文档https://developers.google.com/apps-script/api/reference/rest/v1/scripts/run

在此输入图像描述

在此输入图像描述

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

在此输入图像描述

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. 要解决此问题,您需要创建在App Script中创建的项目的凭据,而不是在Google控制台中创建新项目。 Hence skip the step 1 mentioned in the question which says: 因此,请跳过问题中提到的步骤1:

  1. Visit https://console.developers.google.com and create a new project 访问https://console.developers.google.com并创建一个新项目

And open the App Script project. 并打开App Script项目。 In the current case, the project is "Test Google Form API Personal" 在目前的情况下,该项目是“测试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" 接下来,通过单击三点选项菜单将项目打开为“Cloud Project”,然后选择“Cloud Project”

在此输入图像描述

Now the Google console screen will get open. 现在,Google控制台屏幕将会打开。 Create the OAuth credentials of the opened project. 创建已打开项目的OAuth凭据。

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM