【问题标题】:Google Apps Script API - Permissions for my Python script to execute an Apps Script functionGoogle Apps Script API - 我的 Python 脚本执行 Apps Script 函数的权限
【发布时间】:2020-11-25 11:36:38
【问题描述】:

这是我在这里的第一个贡献。

我正在尝试通过 python 脚本访问 Gmail。为此,我创建了一个 Google Apps Script 函数并在两者之间使用了 Apps Script API。 (This doc 显示我正在尝试做的事情)

所以python script correctly accesses the API, but fails to execute the function。 虽然它在脚本编辑器中工作,但在 Python 中会引发权限问题:

'errorMessage': 'Exception: The script does not have permission to perform that action.
Required permissions: (
https://www.googleapis.com/auth/gmail.labels ||
https://www.googleapis.com/auth/gmail.metadata ||
https://www.googleapis.com/auth/gmail.readonly ||
https://www.googleapis.com/auth/gmail.modify ||
https://mail.google.com/
)',
'errorType': 'ScriptError'

我猜这与客户端 ID OAuth 相关,因为我无法找到授予它权限的位置。我刚刚:

  • 在 Google Cloud Platform 中创建了凭据,
  • 在我的 python 脚本文件夹中将其导出为 creds.json。

这是我的代码,几乎是从教程中复制粘贴的:

import pickle
import os.path
from googleapiclient import errors
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request

# Here I've edited the scopes of authorizations required
SCOPES = [
    "https://www.googleapis.com/auth/gmail.labels",
    "https://www.googleapis.com/auth/gmail.metadata",
    "https://www.googleapis.com/auth/gmail.readonly",
    "https://www.googleapis.com/auth/gmail.modify",
    "https://mail.google.com/"
  ]
    
def get_scripts_service():
    creds = None
    # The file token.pickle stores the user's access and refresh tokens, and is
    # created automatically when the authorization flow completes for the first
    # time.
    if os.path.exists('token.pickle'):
        with open('token.pickle', 'rb') as token:
            creds = pickle.load(token)
    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
            creds.refresh(Request())
        else:
            # Here I've placed the downloaded credentials .json file
            flow = InstalledAppFlow.from_client_secrets_file(
                'creds.json', SCOPES)  
            creds = flow.run_local_server(port=0)
        with open('token.pickle', 'wb') as token:
            pickle.dump(creds, token)

    return build('script', 'v1', credentials=creds)


service = get_scripts_service()

API_ID = # Here i've pasted my API_ID

request = {"function": # Here i've pasted my functionName} 

try:
    response = service.scripts().run(body=request, scriptId=API_ID).execute()

    print (response)
except errors.HttpError as error:
    # The API encountered a problem.
    print(error.content)

如何向我的脚本授予权限?

【问题讨论】:

  • 修改范围后是否删除了token?

标签: python google-apps-script google-cloud-platform oauth-2.0 google-apps-script-api


【解决方案1】:

正如 Aerials 所说的那样简单!谢谢。 这是因为在我编辑范围之前创建了客户端 ID。我已删除令牌并创建了一个新令牌。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-16
    • 1970-01-01
    • 2016-12-13
    相关资源
    最近更新 更多