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

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 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题
  • ¥15 multisim电路设计
  • ¥20 用keil,写代码解决两个问题,用库函数
  • ¥50 ID中开关量采样信号通道、以及程序流程的设计
  • ¥15 U-Mamba/nnunetv2固定随机数种子
  • ¥15 vba使用jmail发送邮件正文里面怎么加图片
  • ¥15 vb6.0如何向数据库中添加自动生成的字段数据。