我尝试从当前登录的用户获取消息列表。我正在使用guzzle客户发出请求。不幸的是,我不太了解有关api的Google文档。
这是我到目前为止所拥有的:
$client = new Client;
$headers = [
'content-type' => 'application/json',
'Authorization' => 'Bearer '.$token->access_token
];
$params = [
'maxResults' => 10,
'client_id' => config('gmail.clientId'),
'client_secret' => config('gmail.clientSecret'),
];
$response = $client->get('https://www.googleapis.com/gmail/v1/users/me/messages', [
$headers,
$params
]);
使用此代码,我收到一条消息“错误401-需要登录”我该如何实现?在我的应用程序中,每个用户都连接到他们自己的Gmail帐户。access_tokens保存在数据库中。
我不确定您所使用的Google API,但您肯定有一个与Guzzle相关的错误:
$response = $client->get('https://www.googleapis.com/gmail/v1/users/me/messages', [
'headers' => $headers,
'query' => $params,
]);
同样根据the docs,您不需要在每次请求时都提供client_id
和client_secret
。看一下Google's official auth lib for PHP,或/和the complete SDK。