Guzzle Gmail API 邮件请求失败 - 需要登录

Guzzle Gmail API messages request fails - Login Required

我尝试获取当前登录用户的消息列表。我正在使用 Guzzle 客户端发出请求。不幸的是,我不太了解 Google 关于 API.

的文档

这是我目前拥有的:

$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
]);

使用此代码我收到一条消息 "Error 401 - Login Required"。我怎样才能做到这一点?在我的应用程序中,每个用户都连接到他们自己的 Gmail 帐户。 access_token 保存在数据库中。

我不确定您使用的 Google API,但您肯定有一个与 Guzzle 相关的错误:

$response = $client->get('https://www.googleapis.com/gmail/v1/users/me/messages', [
    'headers' => $headers,
    'query' => $params,
]);

同样根据the docs, you don't need to supply client_id and client_secret upon every request. Take a look at Google's official auth lib for PHP, or/and at the complete SDK.