Cal*_*röm 3 php gmail google-api google-api-php-client gmail-api
我正在尝试使用Google API 2.0.0 RC4 for PHP从我自己的收件箱(作为项目所有者)获取给定的电子邮件.
我有一个项目设置,一个JSON格式的下载认证文件,我在Google Developers Console中启用了gmail API.
但是,当我运行以下代码时,出现"错误请求"错误,原因是"failedPrecondition".现在这让我相信访问权限存在问题,但我无法弄清楚我需要做什么.
码:
<?php
require_once("google-api-php-client-2.0.0-RC4/vendor/autoload.php");
$client = new Google_Client();
// set the scope(s) that will be used
$client->setScopes(["https://www.googleapis.com/auth/gmail.readonly"]);
$client->setApplicationName("MyProject");
// set the authorization configuration using the 2.0 style
$client->setAuthConfigFile("myAuthFile.json");
$gmailHandle = new Google_Service_Gmail($client);
$messages = $gmailHandle->users_messages->get("myemail@mydomain.com", "12343445asd56");
echo json_encode($messages);
?>
Run Code Online (Sandbox Code Playgroud)
完整的错误看起来像这样:
致命错误:未捕获的异常'Google_Service_Exception',消息'{"错误":{"errors":[{"domain":"global","reason":"failedPrecondition","message":"Bad Request"}], "code":400,"message":"Bad Request"}}'
我刚碰到这个,终于找到了解决方案.您收到该错误是因为您使用的是服务帐户而未指定"主题"作为配置的一部分.这需要API知道您在使用服务帐户时所使用的电子邮件地址.你可以看到它试图使用Google_Client :: createApplicationDefaultCredentials这个问题
解决方法是在调用setAuthConfig()后在代码中添加以下行
$client->setConfig('subject', 'you@email.com');
Run Code Online (Sandbox Code Playgroud)
在从收件箱中提取邮件时,请务必使用关键字"我",因为您现在充当了您已配置为"主题"的电子邮件地址.
| 归档时间: |
|
| 查看次数: |
1663 次 |
| 最近记录: |