我正在嘗試爲服務器設置一個Google應用腳本API可執行文件,以便爲我設置的Python微服務提供服務器身份驗證。服務器到服務器Auth使用Google Apps腳本API可執行
使用快速入門,我能夠通過Auth2使其工作,但我無法使它與服務帳戶一起工作。我允許訪問腳本和電子表格以服務帳戶電子郵件。客戶端密鑰JSON中的項目ID與應用程序腳本的項目ID匹配。我將它作爲一個API可執行文件進行部署。
這是我下面的代碼(雖然我不認爲代碼是問題):
from oauth2client.service_account import ServiceAccountCredentials
from httplib2 import Http
from googleapiclient.discovery import build
scopes = [
'https://www.googleapis.com/auth/drive',
'https://www.googleapis.com/auth/script.external_request',
'https://www.googleapis.com/auth/script.storage',
'https://www.googleapis.com/auth/spreadsheets',
'https://www.googleapis.com/auth/userinfo.email'
]
credentials = ServiceAccountCredentials.from_json_keyfile_name('client_secret.json', scopes)
http_auth = credentials.authorize(Http())
service = build('script', 'v1', http=http_auth)
request = {'function': 'testApi'}
response = service.scripts().run(body=request, scriptId='SCRIPT_ID').execute()
print(response)
的testApi功能在我的應用程序的腳本是一個簡單的函數,返回「它的工作原理」。
我一直得知用戶在使用個人帳戶時沒有權限(403),使用組織時(G Suite帳戶)沒有權限(403)。
如前所述,Google文檔中的Quickstart教程正常工作,但未使用服務帳戶。
是否有人將Google Apps腳本API可執行文件與服務器配合使用以使用服務器驗證帳戶流?
您的服務帳戶是否在管理控制檯中擁有API客戶端訪問權限? (安全性>高級設置>管理API客戶端訪問)如果不是,則需要添加項目的服務帳戶憑據並授予其訪問適當的範圍。 –