我使用 Guzzle 向 Gmail API 发送 HTTP 请求。
它抛出未捕获的异常"无效参数异常",并带有消息"无效的资源类型:对象"看来我给GuzzleHttpPsr7Request()
班喂错了?
require_once __DIR__.'/vendor/autoload.php';
$client = new Google_Client();
$client->setScopes("https://www.googleapis.com/auth/gmail.readonly");
putenv('GOOGLE_APPLICATION_CREDENTIALS=sample.json');
$client->useApplicationDefaultCredentials();
// returns a Guzzle HTTP Client
$httpClient = $client->authorize();
$data = new stdClass;
$data-> topicName ='projects/sample.com:sample/topics/topic';
$data-> labelIds = ["INBOX"];
$data-> labelFilterAction = 'include';
$request = new GuzzleHttpPsr7Request('POST','https://www.googleapis.com/gmail/v1/users/post@sample.com/watch',['Content-type'=>'application/json'],$data);
$response = $httpClient->send($request);
var_dump($response);
//------------output----------------------
Uncaught exception 'InvalidArgumentException' with message 'Invalid resource type: object'
感谢安德鲁·诺兰和丹尼斯·索拉科维奇,现在$request
似乎格式正确。
以下是更新:我正在使用服务帐户凭据来监视新邮件,有谁知道我哪里做错了?非常感谢。
require_once __DIR__.'/vendor/autoload.php';
$client = new Google_Client();
$client->setScopes("https://www.googleapis.com/auth/gmail.readonly");
putenv('GOOGLE_APPLICATION_CREDENTIALS=sample.json');
$client->useApplicationDefaultCredentials();
$gmailService = new Google_Service_Gmail($client);
// returns a Guzzle HTTP Client
$httpClient = $client->authorize();
$data = new stdClass;
$data-> topicName ='projects/sample.com:sample/topics/topic';
$data-> labelIds = ["INBOX"];
$data-> labelFilterAction = 'include';
$data = json_encode((array)$data);
$request = new GuzzleHttpPsr7Request('POST', 'https://www.googleapis.com/gmail/v1/users/post@sample.com/watch',["Content-type" =>"application/json"],$data);
$response = $httpClient->send($request);
echo ($response->getBody());
//--------------output---------------------
{ "error": { "errors": [ { "domain": "global", "reason": "failedPrecondition", "message": "Bad Request" } ], "code": 400, "message": "Bad Request" } }
好的,我现在明白了,我错过了这一步:
您需要向 授予发布权限
serviceAccount:gmail-api-push@system.gserviceaccount.com 在云发布/订阅开发人员控制台权限界面
然后通过发送直接 HTTP 请求与 Guzzle 的代码:
$client = new Google_Client();
$client->setAuthConfig('xxxx-b2838442f7ad.json'); // downloaded json file.
$client->setScopes(array("https://www.googleapis.com/auth/gmail.readonly"));
$user_to_impersonate = 'post@sample.com'; //very important
$client->setSubject($user_to_impersonate); //important
$httpClient = $client->authorize();
$data = new stdClass;
$data-> topicName ='projects/sample.com:sample/topics/topic';
$data-> labelIds = ["INBOX"];
$data-> labelFilterAction = 'include';
$data = json_encode($data);
$request = new GuzzleHttpPsr7Request('POST', 'https://www.googleapis.com/gmail/v1/users/post@sample.com/watch',["Content-type" =>"application/json"],$data);
$response = $httpClient->send($request);
echo($response->getBody());
// ------------------output-----------------------------------
{ "historyId": "31325", "expiration": "1496503166524" }
或者Gmail类的代码-这更难,您需要研究User.php
并WatchRequest.php
,如果不是这篇文章,像我这样的业余爱好者就没有机会:
require_once __DIR__.'/vendor/autoload.php';
$client = new Google_Client();
$client->setAuthConfig('xxxx-b2838442f7ad.json'); // downloaded json file.
$client->setScopes(array("https://www.googleapis.com/auth/gmail.readonly"));
$user_to_impersonate = 'post@sample.com'; //very important
$client->setSubject($user_to_impersonate); //important
$watchreq = new Google_Service_Gmail_WatchRequest;
$watchreq->setLabelIds(array('INBOX'));
$watchreq->setTopicName('projects/sample.com:sample/topics/topic');
$watchreq->setLabelFilterAction('include');
$gmailService = new Google_Service_Gmail($client);
$response =$gmailService->users->watch('me', $watchreq);
echo($response->expiration)."<br/>";
echo($response->historyId);
//---------------------output--------------------------
1496505109513
31331