【问题标题】:Gmail API: example of a request to the APIGmail API:对 API 的请求示例
【发布时间】:2015-08-17 14:58:21
【问题描述】:

我正在尝试使用我的应用程序访问 Gmail API。我应该已经正确地完成了所有设置,并且我正在测试对 API 的请求,具体而言是获取只读消息的请求。

我的代码:

public function gmail_get_messages()
{
    $client = new Google_Client();

    $client->setApplicationName("Gmail API test");
    $client->setDeveloperKey("MY_KEY");
    $client->setClientSecret('MY_CLIENT_SECRET');
    $client->setScopes(array('https://www.googleapis.com/auth/gmail.readonly'));

    // $client->setAccessToken($token);

    $service = new Google_Service_Gmail($client);

    $url = 'https://www.googleapis.com/gmail/v1/users/MY_EMAIL/messages';

    $header = array("Authorization: access_token {MY_ACCESS_TOKEN}");

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_ENCODING, "gzip");
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)');

    $retValue = curl_exec($ch);
    $response = json_decode(curl_exec($ch));
    $ee       = curl_getinfo($ch);
    print_r($ee);

    print_r($retValue);
}

回复:

{ "error": { "errors": [ { "domain": "global", "reason": "authError", "message": "Invalid Credentials", "locationType": "header", "location": "Authorization" } ], "code": 401, "message": "Invalid Credentials" } }

关于令牌的一个要点。我已将访问令牌和刷新令牌作为 json 格式的文件获取,但我不知道如何将它们包含在请求中。

如何正确地将令牌传递给请求?是唯一缺少的东西还是有其他东西?

如果有人可以提供正确请求的示例,我将不胜感激!

【问题讨论】:

  • 您不应该刚刚编辑过您的其他问题吗? stackoverflow.com/questions/30613152/…你为什么需要一个新的?
  • 我确实编辑了这个问题并回答了。我终于能够在 json 文件中获取令牌。我现在正尝试将令牌包含在我对 API 的请求中以使其正常工作。

标签: php google-api google-oauth google-api-php-client gmail-api


【解决方案1】:

我认为你很接近!尝试像这样将访问令牌分配给客户端:

$client = new Google_Client();


$client->setApplicationName('Gmail API test');
$client->setDeveloperKey('MY_KEY');
$client->setClientSecret('MY_CLIENT_SECRET');
$client->SetClientId('MY_CLIENT_ID');
$client->setScopes(array('https://www.googleapis.com/auth/gmail.readonly'));
$client->setAccessToken('{"access_token":"MY_ACCESS_TOKEN",
                          "token_type":"Bearer"‌​,"expires_in":3600,
                          "refresh_token":"MY_REFRESH TOKEN","created":1433329214}');

$service = new Google_Service_Gmail($client);

$messages = $service->users_messages->listUsersMessages('me');
$list = $messages->getMessages();

// Look at the contents of the first message
$message = $list[0];
$parts = $message->getPayload()->getParts();

$body = $parts[0]['body'];
$rawData = $body->data;
$sanitizedData = strtr($rawData,'-_', '+/');
$decodedMessage = base64_decode($sanitizedData);
var_dump($decodedMessage);

【讨论】:

  • 还没有工作。我得到了同样的错误。您是否有工作请求的完整示例?
  • 感谢您的努力!我收到此错误:未捕获的异常 'Google_Auth_Exception' 带有消息'无法 json 解码令牌'。我可以将令牌作为外部 json 文件传递​​吗?可能一旦完成,一切都会正常工作。
  • @johnnyfittizio 试试最新的编辑!我不确定如何从文件中获取它:(
  • 我试过了。我认为你的代码更合适。但是很遗憾还是有这个问题要传token
  • @johnnyfittizio 你尝试过细微的改变吗? :) $client->setAccessToken('{"access_token":"MY_ACCESS_TOKEN"}');
猜你喜欢
  • 1970-01-01
  • 2020-07-03
  • 1970-01-01
  • 2017-10-27
  • 2015-10-21
  • 2012-02-11
  • 2015-06-27
  • 1970-01-01
  • 2018-05-21
相关资源
最近更新 更多