Guzzle向Gmail API发送HTTP请求,“无效的资源类型:对象”

时间:2017-05-24 07:40:28

标签: php gmail-api guzzle google-client

我使用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" } }

1 个答案:

答案 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