繁体   English   中英

Google API使用access_token获取收件箱电子邮件?

[英]Google API Get inbox emails using access_token?

我可以使用oAuth 2.0获取access_token以获取多个权限,如电子邮件,联系人,文档等。 我有access_token我使用以下代码获得了联系人。

$url = 'https://www.google.com/m8/feeds/contacts/default/full?max-  results='.$max_results.'&oauth_token='.$access_token;

$response_contacts=  curl_get_file_contents($url);

现在我想让用户使用此access_token发送电子邮件。 我用过这个网址。 但它给出了401 unauthorized Error

$url = 'https://mail.google.com/mail/feed/atom&oauth_token='.$access_token;
$response_emails=  curl_get_file_contents($url);

请指导我如何使用access_token收到电子邮件。

我已经看到使用oauth_token作为请求参数来引用Gmail Feed。 但是,一旦我使用OAuth Playground,我发现您需要将OAuth信息作为Authorization标头传递,如下所示。

<?php
$now = time();
$consumer = ...; // your own value here
$secret = ...; // your own value here
$nonce = ...; // same value you've been using
$algo = "sha1";
$sigmeth = "HMAC-SHA1";
$av = "1.0";
$scope = "https://mail.google.com/mail/feed/atom";
$path = $scope;
$auth = ...; // an object containing outputs of OAuthGetAccessToken

$args = "oauth_consumer_key=" . urlencode($consumer) .
  "&oauth_nonce=" . urlencode($nonce) .
  "&oauth_signature_method=" . urlencode($sigmeth) .
  "&oauth_timestamp=" . urlencode($now) .
  "&oauth_token=" . urlencode($auth->oauth_token) .
  "&oauth_version=" . urlencode($av);

$base = "GET&" . urlencode($path) . "&" . urlencode($args);

$sig = base64_encode(hash_hmac($algo, $base,
  "{$secret}&{$auth->oauth_token_secret}", true));

$url = $path . "?oauth_signature=" . urlencode($sig) . "&" . $args;

// Create a stream
$opts = array(
  "http" => array(
    "method" => "GET",
    "header" => "Authorization: OAuth " .
       "oauth_version=\"{$av}\", " .
       "oauth_nonce=\"{$nonce}\", " .
       "oauth_timestamp=\"{$now}\", " .
       "oauth_consumer_key=\"{$consumer}\", " .
       "oauth_token=\"{$auth->oauth_token}\", " .
       "oauth_signature_method=\"{$sigmeth}\", " .
       "oauth_signature=\"{$sig}\"\r\n"
  )
);

$context = stream_context_create($opts);

$out = file_get_contents($path, false, $context);
?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM