如何从Google OAuth 2.0 Playground运行Google App脚本功能|呼叫者没有权限 [英] How to run Google App Script function from Google OAuth 2.0 Playground | The caller does not have permission

本文介绍了如何从Google OAuth 2.0 Playground运行Google App脚本功能|呼叫者没有权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个新脚本,该脚本在我的google帐户上创建了"Google表单".以下是示例代码:

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());
}

接下来,通过转到发布">部署为API可执行文件",使API可执行文件

Next, make the API Executable by going to Publish > Deploy as API Executable

现在,如果我直接从Google App脚本执行代码,那么它可以很好地工作并且表单也将被创建.

Now if I execute the code directly from the Google App Script, it works perfectly fine and the form is also getting created.

现在我在执行Google OAuth 2.0 Playground中的代码时遇到了问题.为此,我遵循以下步骤:

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. 访问 https://console.developers.google.com 并创建一个新项目
  2. 在左侧菜单上,选择库"
  3. 在App脚本库中,搜索"Apps Script API"并启用它

  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

下一步,转到凭据菜单,然后单击创建凭据"> OAuth客户端ID

Next, go to the credentials menu and click on "Create Credentials" > OAuth client ID

在下一个屏幕上,选择"Web应用程序"

On the next screen, select Web Application

输入新的Web应用程序的名称

Enter name the new web application

在授权的JavaScript起源"中,设置" http://localhost "

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

在授权重定向URI"中,设置" https://developers.google.com/oauthplayground ",因为目前,我们将需要在Google OAuth Playground上进行身份验证响应.然后点击创建.

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.

成功后,您将收到客户端ID"和您将在Google OAuth Playground中提供的帐户客户端机密",以对其他用户应用程序进行身份验证.

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.

现在访问 https://developers.google.com/oauthplayground 并单击设置齿轮.在下拉菜单中,选中使用您自己的OAuth凭据",然后输入在步骤9中收到的"OAuth客户端ID"和"OAuth客户端密码"

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

接下来,在步骤1选择和授权API"部分中,选择"Apps Script API v1",然后进一步选择"

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

接下来,它将询问您要访问其选定范围的帐户的授权.在此,我使用相同的帐户,在该帐户上创建用于创建代码的应用程序脚本",并从中生成客户端ID"和客户端机密".

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.

  1. 以上步骤将生成授权代码",并且进一步您可以生成刷新令牌"和访问令牌".

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

接下来,我们必须使用服务来执行google app脚本代码.单击列出可能的操作",然后选择运行脚本"

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. 接下来将生成一个语法,询问您可以从google app脚本项目中找到的脚本ID.为此,在google App脚本项目中,单击文件>项目属性,最后将弹出一个引用脚本ID的弹出窗口.
  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

  1. 在PlayGround中输入脚本ID,然后通过单击输入请求正文"按钮来设置请求正文.要了解请求正文参数,请参阅文档 https ://developers.google.com/apps-script/api/reference/rest/v1/scripts/run

  1. 现在单击发送请求"

完成上述所有步骤后,我们收到以下验证错误:

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

感谢您的解决方案.

推荐答案

要解决该问题,您需要创建在App脚本中创建的项目的凭据,而不是在Google控制台中创建新项目.因此,跳过问题中提到的步骤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. 访问 https://console.developers.google.com 并创建一个新项目
  1. Visit https://console.developers.google.com and create a new project

并打开App Script项目.在当前情况下,该项目为"Test Google Form API Personal"

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"

现在,Google控制台屏幕将打开.创建打开的项目的OAuth凭据.

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

这篇关于如何从Google OAuth 2.0 Playground运行Google App脚本功能|呼叫者没有权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆