【问题标题】:Gmail API with PHP/Curl List Message带有 PHP/Curl 列表消息的 Gmail API
【发布时间】:2015-11-28 13:50:25
【问题描述】:

我正在尝试使用 Google API 获取我的 gmail 邮件列表。我设法使用此 curl 方法获取我的联系人列表和其他一些 Google 资源,但每当我尝试检索我的电子邮件列表时,我都会收到“无效凭据”。 - 请注意,我的范围设置正确。

这是 php 代码:

    <?

$thetokenarray=array();
$thetokenarray= json_decode($_SESSION["token"] ,true);
$thetoken =  $thetokenarray["access_token"] ;

$thedomain =  substr(strrchr($_SESSION["email"], "@"), 1);

$ch = curl_init();
$headers = array("Authorization: access_token ".$thetoken."");
print_r($headers);

curl_setopt($ch, CURLOPT_URL, 'https://www.googleapis.com/gmail/v1/users/enattee%40gmail.com/messages');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
curl_close($ch);

$response = json_decode($response,true);
print_r($response);

?>

这是谷歌答案:

Array ( [error] => Array ( [errors] => Array ( [0] => Array ( [domain] => global [reason] => authError [message] => Invalid Credentials [locationType] => header [location] => Authorization ) ) [code] => 401 [消息] => 无效的凭据))

这是我在使用 https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=ya29.4gE6OJdoxxxT1UP0Yxxxxy3DvlPphF-1CVIFPfqp0j9Cuhm-hT0W_Qy3oGrcF- 时从谷歌获得的令牌反馈

{
 "issued_to": "64125009955-1hlmj0jgi9j2hd4sxxxxxaem9c9qhqcf8f.apps.googleusercontent.com",
 "audience": "64125xxxx09955-1hlmj0jgixxxxx9c9qhqcf8f.apps.googleusercontent.com",
 "user_id": "10597422xxxx25084",
 "scope": "https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/gmail.readonly https://www.google.com/m8/feeds/ https://www.googleapis.com/auth/admin.directory.user.readonly https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/plus.profile.emails.read https://www.googleapis.com/auth/plus.moments.write https://www.googleapis.com/auth/plus.me https://www.googleapis.com/auth/plus.profile.agerange.read https://www.googleapis.com/auth/plus.profile.language.read https://www.googleapis.com/auth/plus.circles.members.read",
 "expires_in": 3427,
 "email": "exxx@gmail.com",
 "verified_email": true,
 "access_type": "offline"
}

最终,如果有一种更简单且不同的方法来检索已登录的用户电子邮件列表,那也很棒:)

【问题讨论】:

    标签: php curl google-api token gmail-api


    【解决方案1】:

    其实代码没问题。 AuthUrl 生成器在我的测试服务器上设置了错误的参数。

    对于那些想要使用代码的人来说,Google-PHP-Api https://github.com/google/google-api-php-client 是示例中使用的代码。

    【讨论】:

      猜你喜欢
      • 2017-05-23
      • 2020-01-31
      • 2016-11-05
      • 1970-01-01
      • 2016-03-11
      • 2017-04-04
      • 1970-01-01
      • 2017-03-03
      • 2013-03-21
      相关资源
      最近更新 更多