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 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀