我使用Guzzle向Gmail API发送HTTP请求。
它会抛出未捕获的异常'InvalidArgumentException',并显示消息'Invalid resource type:object'
看起来我错误地喂了GuzzleHttp\Psr7\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();
// 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 GuzzleHttp\Psr7\Request('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'
感谢Andrew Nolan和Denis Solakovic,现在$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 GuzzleHttp\Psr7\Request('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" } }
答案 0 :(得分:0)
您需要向
授予发布权限serviceAccount:gmail-api-push@system.gserviceaccount.com ,Cloud Pub/Sub Developer Console permissions interface
然后代码通过Guzzle发送直接HTTP请求:
$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 GuzzleHttp\Psr7\Request('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 Classes 的代码 - 这比较困难,您需要查看 User.php
和 WatchRequest.php
,如果不是this article,那么像我这样的业余爱好者就没有机会了:
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