我正在尝试使用服务帐户从 php 调用一个非常简单的 google apps 脚本,以便只有我的服务器可以访问它,而网站的用户不能访问它。
这就是我的做法。我在这里创建脚本 https://script.google.com
function get() {
return ContentService.createTextOutput('get method');
}
当我保存它时,会自动关联一个新项目。
然后我打开File > Project Properties
获取 scriptId = MM8zQqofS1OIcnFiNSlm1CGEl5qMrMVBt
我访问关联项目的开发者控制台抛出 Resources > Project Developers console
单击显示的弹出窗口顶部的项目链接。
然后,我点击“激活和管理 API”并激活“Google Apps 脚本执行 API”。我单击“凭据”并看到之前的操作自动创建了 OAuth2 凭据。但我需要的是服务帐户凭据。然后我创建一个 Add credentials > Service account
并下载生成的p12文件。我得到clientId = 109160023321840004240
和clientMail = <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b7d6d4d4d8c2d9c39a86f7c7c5d8ddd2d4c39aded39ac2d8dcc0c5d4c7dec6d2d2c0dfc0c1c7d3dfd599ded6da99d0c4d2c5c1ded4d2d6d4d4d8c2d9c399d4d8da" rel="noreferrer noopener nofollow">[email protected]</a>
对于此服务帐户。
我返回我的脚本并将其与具有读写访问权限的服务帐户电子邮件共享 File > Share
。首先,我在我的个人邮箱中收到一封电子邮件,通知我
Delivery to the following recipient failed permanently:
a<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="aac9c9c5dfc4de879beadad8c5c0cfc9de87c3ce87dfc5c1ddd8c9dac3dbcfcfddc2dddcdacec2c884c3cbc784cdd9cfd8dcc3c9cfcbc9c9c5dfc4de84c9c5c7" rel="noreferrer noopener nofollow">[email protected]</a>
然后我将脚本发布为执行 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(
'<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5a3b3939352f342e776b1a2a2835303f392e77333e7735312d28392a332b3f3f2d322d2c2a3e323874333b37743d293f282c33393f3b3939352f342e74393537" rel="noreferrer noopener nofollow">[email protected]</a>',
[/*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.
如果你们中有一个人有想法来帮助我,我会很高兴:)
最佳答案
apps 脚本尚不支持具有执行 API 的服务帐户。请参阅https://code.google.com/p/google-apps-script-issues/issues/detail?id=5461
谷歌表示他们正在调查此事,但显然不会很快发生(基于谷歌对 google+ 帖子的回复,如https://plus.google.com/+MartinHawksey/posts/Zquix9XqzkK)
关于php - 使用服务帐户从 PHP 调用 google apps 脚本执行 api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33650636/