【问题标题】:Server to Server Auth with Google Apps scripts API executable使用 Google Apps 脚本 API 可执行的服务器到服务器身份验证
【发布时间】:2017-10-16 14:47:56
【问题描述】:

我正在尝试为我正在设置的 Python 微服务设置一个带有服务器到服务器身份验证的 Google 应用脚本 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 函数是一个返回“It works”的简单函数。

我不断收到用户在使用个人帐户时没有权限 (403),在使用组织(G Suite 帐户)时甚至是 500。

如前所述,Google 文档中的快速入门教程有效,但这并没有使用服务帐户。

有没有人让 Google Apps Scripts API 可执行,与服务器到服务器的身份验证帐户流一起工作?

【问题讨论】:

  • 您的服务帐户是否在管理控制台中具有 API 客户端访问权限? (安全 > 高级设置 > 管理 API 客户端访问)如果没有,您需要添加项目的服务帐户凭据并授予其访问适当范围的权限。

标签: python python-3.x google-apps-script google-api


【解决方案1】:

您可能想查看有关Using Google Service Accounts with Google Apps Script 的教程。此示例代码展示了如何使用 Service Accounts 在 Google Apps 脚本中使用 OAuth。

要使此代码正常工作,您需要将 create a Google Service accountdomain-wide delegation 替换,将私钥和客户端客户端电子邮件替换为实际值,并使用 Drive API 范围将客户端 ID 添加到您的 Google Apps 管理控制台。 OAuth 2.0 访问令牌存储在脚本属性中。

var JSON = {
    "private_key": "Your Private Key",
    "client_email": "serviceacount@project-ctrlq.iam.gserviceaccount.com",
    "client_id": "1234567890",
    "user_email": "amit@labnol.org"
};

function getOAuthService(user) {
    return OAuth2.createService("Service Account")
        .setTokenUrl('https://accounts.google.com/o/oauth2/token')
        .setPrivateKey(JSON.private_key)
        .setIssuer(JSON.client_email)
        .setSubject(JSON.user_email)
        .setPropertyStore(PropertiesService.getScriptProperties())
        .setParam('access_type', 'offline')
        .setScope('https://www.googleapis.com/auth/drive');
}

function getUserFiles() {
    var service = getOAuthService();
    service.reset();
    if (service.hasAccess()) {
        var url = 'https://www.googleapis.com/drive/v2/files?pageSize=1';
        var response = UrlFetchApp.fetch(url, {
            headers: {
                Authorization: 'Bearer ' + service.getAccessToken()
            }
        });
        Logger.log(response.getContentText());
    }
}

function reset() {
    var service = getOAuthService();
    service.reset();
}

此外,如果您收到 403 Insufficient permission 错误,可能是因为应用程序请求访问未经 Google Apps 管理控制台授权的 API 范围。 invalid_grant 错误可能是由于托管应用程序的服务器的日期和时间设置不正确。

【讨论】:

    猜你喜欢
    • 2019-07-23
    • 1970-01-01
    • 2020-04-22
    • 1970-01-01
    • 2019-11-06
    • 1970-01-01
    • 2013-10-24
    • 2010-10-21
    • 2016-01-02
    相关资源
    最近更新 更多