[英]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執行代碼的問題 。 為此,我按照以下步驟操作:
在下一個屏幕上,選擇Web Application
輸入新Web應用程序的名稱
在“Authorized JavaScript origin”中設置“ http:// localhost ”
在“授權重定向URI”中設置“ https://developers.google.com/oauthplayground ”,因為目前我們需要在Google OAuth Playground上進行身份驗證響應。 然后單擊“創建”。
成功后,您將收到您在Google OAuth Playground中提供的帳戶的“客戶ID”和“客戶機密”,以對其他用戶應用程序進行身份驗證。
現在訪問https://developers.google.com/oauthplayground並點擊Settings Gear。 在下拉菜單中,選中“使用您自己的OAuth憑據”,然后輸入在步驟9中收到的“OAuth客戶端ID”和“OAuth客戶端密鑰”
接下來,在“步驟1選擇和授權API”部分中,選擇“Apps Script API v1”並進一步選擇“ https://www.googleapis.com/auth/forms ”選項,然后單擊“授權”。
接下來,它將要求您要訪問所選范圍的帳戶的授權。 在這里,我使用相同的帳戶,在該帳戶上創建用於創建代碼的“App Script”,並且生成“Client ID”和“Client Secret”。
完成上述所有步驟后,我們收到以下身份驗證錯誤:
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:
並打開App Script項目。 在目前的情況下,該項目是“測試Google Form API Personal”
接下來,通過單擊三點選項菜單將項目打開為“Cloud Project”,然后選擇“Cloud Project”
現在,Google控制台屏幕將會打開。 創建已打開項目的OAuth憑據。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.