【问题标题】:Calling a google apps script execution api from PHP using a service account使用服务帐户从 PHP 调用 google Apps 脚本执行 api
【发布时间】:2015-11-11 12:33:24
【问题描述】:

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

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

function get() {
    return  ContentService.createTextOutput('get method');
}

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

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

然后我点击“激活和管理 API”并激活“Google Apps 脚本执行 API”。我单击“凭据”并看到之前的操作自动创建了 OAuth2 凭据。但我需要的是服务帐户凭据。然后我创建一个Add credentials > Service account 并下载生成的 p12 文件。我得到了此服务帐户的 clientId = 109160023321840004240 和 clientMail = 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

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

现在让我们开始 PHP 服务器端。使用此处提供的“适用于 PHP 的 Google API 客户端库”https://github.com/google/google-api-php-client 我尝试从 PHP 调用我的脚本函数:

$client = new Google_Client();
$client->setClientId('109160023321840004240');
$client->setApplicationName('myScript');

$cred = new Google_Auth_AssertionCredentials(
    'account-1@project-id-okwrcpiqeewhwvpdhb.iam.gserviceaccount.com',
    [/*no scope nedeed for this simple script*/],
    file_get_contents('path_to_myScript.p12')
);
$client->setAssertionCredentials($cred);

if ($client->getAuth()->isAccessTokenExpired()) {
    $client->getAuth()->refreshTokenWithAssertion($cred);
}

$service = new Google_Service_Script($client);
$scriptId = 'MM8zQqofS1OIcnFiNSlm1CGEl5qMrMVBt';

// Create an execution request object.
$request = new Google_Service_Script_ExecutionRequest();
$request->setFunction('get');
$response = $service->scripts->run($scriptId, $request);

这是我一直得到的回应

Error calling POST https://script.googleapis.com/v1/scripts/MM8zQqofS1OIcnFiNSlm1CGEl5qMrMVBt:run: (403) The caller does not have permission

如果我在部署脚本时选择授予“仅限我”的访问权限,我会收到以下响应。

Error calling POST https://script.googleapis.com/v1/scripts/MM8zQqofS1OIcnFiNSlm1CGEl5qMrMVBt:run: (404) Requested entity was not found.

如果你们中的任何一个有想法可以帮助我,我会很高兴:)

【问题讨论】:

    标签: php google-apps-script service-accounts


    【解决方案1】:

    apps 脚本还不支持带有执行 api 的服务帐户。见https://code.google.com/p/google-apps-script-issues/issues/detail?id=5461

    谷歌表示他们正在调查,但显然不会很快发生(基于谷歌对 google+ 帖子的回复,例如 https://plus.google.com/+MartinHawksey/posts/Zquix9XqzkK)

    【讨论】:

    • 好吧之字形。非常感谢你这么快地帮助我,这样我就不用花更多的时间来调查我错了:)
    • 我做了,但我的投票只有在我有 15 票时才会出现(抱歉)你知道我可以使用一种解决方法,以便我的服务器可以调用我自己的脚本而不提示我的网站用户进行授权吗?服务器密钥会起作用吗? console.developers.google.com/apis/credentials/…
    • 查看不同的应用脚本发布选项。你可以自己做服务。
    • 问题是我希望这个脚本作为“我”(脚本的所有者)或我授予完全访问权限的帐户服务执行。我不想与我网站的用户共享它并请求他们允许运行该脚本。
    • 由于脚本 url 在您的服务器上,因此任何人都不会猜到它。添加一个类似密码的参数,它更加晦涩难懂。只要您不将网址暴露在其他任何地方,它就像猜测 oauth 令牌一样难。
    猜你喜欢
    • 2018-07-20
    • 1970-01-01
    • 1970-01-01
    • 2016-01-23
    • 1970-01-01
    • 2016-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多