doucongqian6644 2012-02-11 10:41
浏览 285
已采纳

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

I am able to get access_token for multiple permissions like emails, contacts, docs, etc. using oAuth 2.0. I have access_token I got contacts using the following code.

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

Now i want to get users Emails using this access_token. i used this url . but it gives 401 unauthorized Error

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

please guide me how can i get emails using access_token.

  • 写回答

1条回答 默认 最新

  • dongyong3554 2012-03-25 18:34
    关注

    I've seen references to the Gmail feed using oauth_token as a request parameter. However, once I used the OAuth Playground I discovered that you need to pass your OAuth information as an Authorization header, as you'll see below.

    <?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}\"
    "
      )
    );
    
    $context = stream_context_create($opts);
    
    $out = file_get_contents($path, false, $context);
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决