小编imb*_*ebe的帖子

使用服务帐户从 PHP 调用 google 应用程序脚本执行 api

我正在尝试使用服务帐户从 php 调用一个非常简单的谷歌应用程序脚本,以便只有我的服务器可以访问它,而不是网站的用户。

这是我的做法。我在这里创建脚本https://script.google.com

function get() {
    return  ContentService.createTextOutput('get method');
}
Run Code Online (Sandbox Code Playgroud)

当我保存它时会自动关联一个新项目。然后我打开File > Project Properties得到scriptId = MM8zQqofS1OIcnFiNSlm1CGEl5qMrMVBt

Resources > Project Developers console通过单击显示的弹出窗口顶部的项目链接来访问相关项目的开发人员控制台。

然后我点击“激活和管理 API”并激活“Google Apps Script Execution API”。我单击“凭据”并看到之前的操作自动创建了 OAuth2 凭据。但我需要的是服务帐户凭据。然后我创建一个Add credentials > Service account并下载生成的 p12 文件。我得到了这个服务帐户的clientId = 109160023321840004240clientMail = account-1@project-id-uokwrcpiqeewhwvpdhb.iam.gserviceaccount.com

我返回到我的脚本并与具有读写访问权限的服务帐户电子邮件共享它File > Share。首先,我在我的个人邮箱中收到一封电子邮件,通知我

Delivery to the following recipient failed permanently:

 account-1@project-id-uokwrcpiqeewhwvpdhb.iam.gserviceaccount.com
Run Code Online (Sandbox Code Playgroud)

然后我将脚本发布Publish > Publish as an execution API为每个人都可以访问的执行 API 。

现在让我们进入 PHP 服务器端。使用此处提供的“适用于 PHP …

php google-apps-script service-accounts

2
推荐指数
1
解决办法
2375
查看次数

标签 统计

google-apps-script ×1

php ×1

service-accounts ×1