duanhuo3392 2015-11-11 12:33
浏览 66
已采纳

使用服务帐户从PHP调用Google Apps脚本执行API

I'm trying to call a very simple google apps script from php using a service account so that only my server can access it, not users of the website.

Here is how I do. I create the script here https://script.google.com

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

and a new project is automatically associated when i save it. Then I open File > Project Properties to get the scriptId = MM8zQqofS1OIcnFiNSlm1CGEl5qMrMVBt

I access the developer console of the associated project threw Resources > Project Developers console by clicking on the project link at the top of the popup displayed.

Then I click 'Activate and manage API' and activate the 'Google Apps Script Execution API'. I click on 'Credentials' and see that the previous operation automatically created OAuth2 credentials. But what I need is service account credentials. Then I create one Add credentials > Service account and download generated p12 file. I get the clientId = 109160023321840004240 and clientMail = account-1@project-id-uokwrcpiqeewhwvpdhb.iam.gserviceaccount.com for this service account.

I go back to my script and share it with the service account email with read&write access File > Share. First of all i get an email in my personal mailbox which notifies me that

Delivery to the following recipient failed permanently:

 account-1@project-id-uokwrcpiqeewhwvpdhb.iam.gserviceaccount.com

Then I publish the script as an execution API Publish > Publish as an execution API with access to everybody.

Now lets go on the PHP server side. Using the 'Google APIs Client Library for PHP' available here https://github.com/google/google-api-php-client I try to call my script function from 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);

And here is the response I get all the time

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

If, when I deploy the script, I choose to give access to 'Me only', i get the following response.

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

I would be so happy if one of you have an idea to help me :)

  • 写回答

1条回答 默认 最新

  • duanji7881 2015-11-11 13:22
    关注

    apps script does not yet support service accounts with execution api. see https://code.google.com/p/google-apps-script-issues/issues/detail?id=5461

    google said they are looking into it but apparently wont happen soon (based on google replies on google+ posts about this like https://plus.google.com/+MartinHawksey/posts/Zquix9XqzkK)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接