簡體   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

我創建了一個新腳本,在我的Google帳戶上創建“Google表單”。 以下是示例代碼: 在此輸入圖像描述

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

接下來,通過轉到Publish> Deploy as API Executable來生成API Executable 在此輸入圖像描述

現在,如果我直接從Google App腳本執行代碼,它的工作完全正常,表單也會被創建。

現在我遇到了從Google OAuth 2.0 Playground執行代碼的問題 為此,我按照以下步驟操作:

  1. 訪問https://console.developers.google.com並創建一個新項目
  2. 在左側菜單中,選擇“庫”
  3. 在App Script Library中,搜索“Apps Script API”並啟用它 在此輸入圖像描述

  4. 接下來,轉到憑據菜單,然后單擊“創建憑據”> OAuth客戶端ID 在此輸入圖像描述

  5. 在下一個屏幕上,選擇Web Application

  6. 輸入新Web應用程序的名稱

  7. 在“Authorized JavaScript origin”中設置“ http:// localhost

  8. 在“授權重定向URI”中設置“ https://developers.google.com/oauthplayground ”,因為目前我們需要在Google OAuth Playground上進行身份驗證響應。 然后單擊“創建”。

  9. 成功后,您將收到您在Google OAuth Playground中提供的帳戶的“客戶ID”和“客戶機密”,以對其他用戶應用程序進行身份驗證。 在此輸入圖像描述

  10. 現在訪問https://developers.google.com/oauthplayground並點擊Settings Gear。 在下拉菜單中,選中“使用您自己的OAuth憑據”,然后輸入在步驟9中收到的“OAuth客戶端ID”和“OAuth客戶端密鑰” 在此輸入圖像描述

  11. 接下來,在“步驟1選擇和授權API”部分中,選擇“Apps Script API v1”並進一步選擇“ https://www.googleapis.com/auth/forms ”選項,然后單擊“授權”。 在此輸入圖像描述

  12. 接下來,它將要求您要訪問所選范圍的帳戶的授權。 在這里,我使用相同的帳戶,在該帳戶上創建用於創建代碼的“App Script”,並且生成“Client ID”和“Client Secret”。 在此輸入圖像描述

在此輸入圖像描述

  1. 上面的步驟將生成“授權代碼”,您還可以生成“刷新令牌”和“訪問令牌”。 在此輸入圖像描述

  2. 接下來,我們必須使用服務來執行谷歌應用程序腳本代碼。 單擊“列出可能的操作”,然后選擇“運行腳本” 在此輸入圖像描述

    1. 接下來將生成一個語法,詢問您可以從谷歌應用程序腳本項目中找到的腳本ID。 為此,在Google App Script項目上,單擊文件>項目屬性,最后將打開一個彈出窗口,引用腳本ID 在此輸入圖像描述

在此輸入圖像描述

在此輸入圖像描述

  1. 在PlayGround中輸入腳本ID,然后單擊“輸入請求正文”按鈕設置請求正文。 要了解請求正文參數,請參閱文檔https://developers.google.com/apps-script/api/reference/rest/v1/scripts/run

在此輸入圖像描述

在此輸入圖像描述

  1. 現在點擊“發送請求”

完成上述所有步驟后,我們收到以下身份驗證錯誤:

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 Script中創建的項目的憑據,而不是在Google控制台中創建新項目。 因此,請跳過問題中提到的步驟1:

  1. 訪問https://console.developers.google.com並創建一個新項目

並打開App Script項目。 在目前的情況下,該項目是“測試Google Form API Personal”

在此輸入圖像描述

接下來,通過單擊三點選項菜單將項目打開為“Cloud Project”,然后選擇“Cloud Project”

在此輸入圖像描述

現在,Google控制台屏幕將會打開。 創建已打開項目的OAuth憑據。

在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM