【问题标题】:Google Auth 1.0 access gmail unauthorizedGoogle Auth 1.0 未经授权访问 gmail
【发布时间】:2012-08-13 18:52:32
【问题描述】:

我为我的域创建了谷歌应用程序,它也有一些电子邮件地址, 所以我要去那里访问收件箱电子邮件计数uisng身份验证令牌和消费者秘密。

问题是我成功获得了 oauth_token_secret 和 oauth_token 和 oauth_callback_confirmed

oauth_token={OAUTH_TOKEN}&oauth_token_secret={OAUTH_TOKEN_SECRET}&oauth_callback_confirmed=true

但是当我尝试使用上述令牌访问 gmail 时,它会出错

Warning: file_get_contents(https://mail.google.com/mail/feed/atom/?oauth_token={OAUTH_TOKEN}&oauth_token_secret={OAUTH_TOKEN_SECRET}&oauth_callback_confirmed=true&max-results=100)
[function.file-get-contents]: failed to open stream: HTTP request failed!
HTTP/1.0 401 Unauthorized in /hermes/bosoraweb022/b1554/ipg.webxtreamsnet/freelancer/auth2.php on line 85

这是我的代码

<?php

$consumer = 'www.artbymargaret.co.uk';
$secret = '{CLIENT_SECRET}';
$callback = '';
$sign_method = 'HMAC-SHA1';
$version = '1.0';
$scope = 'https://mail.google.com/mail/feed/atom';

function urlencodeRFC3986($string)
{
   return str_replace('%7E', '~', rawurlencode($string));
}


$mt = microtime();
$rand = mt_rand();
$nonce = md5($mt.$rand);
$time = time();

$url = 'https://www.google.com/accounts/OAuthGetRequestToken';
$path = '/accounts/OAuthGetRequestToken';

$post = array(
    'oauth_callback' => urlencodeRFC3986($callback),
    'oauth_consumer_key' => $consumer,
    'oauth_nonce' => $nonce,
    'oauth_signature_method' => $sign_method,
    'oauth_timestamp' => $time,
    'oauth_version' => $version,
    'scope' => urlencodeRFC3986($scope)
);

$post_string = '';
foreach($post as $key => $value)
{
    $post_string .= $key.'='.($value).'&';
}
$post_string = rtrim($post_string, '&');

$key_parts = array($secret);

$key_parts = urlencodeRFC3986($secret);
//$key = implode('&', $key_parts);
$key = $key_parts.'&';
$base_string = 'GET&'.urlencodeRFC3986($url).'&'.urlencodeRFC3986($post_string);
$signature = base64_encode(hash_hmac('sha1', $base_string, $key, true));


$post = array(
    'oauth_version' => $version,
    'oauth_nonce' => $nonce,
    'oauth_timestamp' => $time,
    'oauth_consumer_key' => $consumer,
    'oauth_callback' => $callback,
    'oauth_signature_method' => $sign_method,
    'oauth_signature' => $signature
);        

$header_string = '';
foreach($post as $key => $value)
{
    $header_string .= $key.'="'.urlencodeRFC3986($value).'", ';
}

$header_string = trim($header_string);
$header_string = rtrim($header_string, ',');

$header[] = 'GET '.$path.'?scope='.urlencodeRFC3986($scope).' HTTP/1.1';
$header[] = 'Host: www.google.com';
$header[] = 'Accept: */*';
//$header[] = 'Content-Type: application/x-www-form-urlencoded';
$header[] = 'Authorization: OAuth '.$header_string;


$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_URL, $url.'?scope='.$scope);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$result = curl_exec($ch);
curl_close($ch);
echo $result;

 $xmlresponse= file_get_contents("https://mail.google.com/mail/feed/atom/?".$result."&mail=jane@funtastic.uk.com&max-results=100");
    print_r($out);

//die();

任何人都知道硬件可以通过 php 使用 auth 1x 版本访问 gmail 收件箱提要,或者我的客户端是否有任何错误

【问题讨论】:

    标签: php oauth gmail google-apps


    【解决方案1】:

    您向 API 端点发出的实际请求仅使用 file_get_contents 和查询参数。使用 OAuth 1.0,您需要正确签署此请求,并传入 Authorization 标头。您可能希望使用上面使用的相同 curl 代码来执行此操作。

    OAuth 1.0 对最终用户授予的授权有 3 个步骤:

    1. 应用服务器针对 OAuth 请求令牌发出服务器到服务器请求。
    2. 应用服务器将用户重定向到端点以授权该令牌。他们批准请求,然后被重定向回应用服务器。
    3. 然后应用服务器将授权的请求令牌交换为访问令牌。

    完成这些步骤后,访问令牌将用于签署对 API 提供者的 OAuth 请求。签名很关键——访问令牌不像在 OAuth 2.0 中那样用作“持有者令牌”。

    更多关于标准 OAuth 1.0 机制的信息: https://developers.google.com/accounts/docs/OAuth_ref#SigningOAuth

    如果您只是尝试在没有任何最终用户批准的情况下发出服务器到服务器的请求,您可以对您控制的 Google Apps 域执行此操作。这不需要请求令牌、访问令牌等——只需要您的消费者密钥+秘密。以下是有关用于发出 API 请求的 2-legged OAuth 1.0 机制的更多信息: https://developers.google.com/accounts/docs/OAuth#AccessFeed

    当然同意升级到 OAuth 2.0 :)

    【讨论】:

      【解决方案2】:

      根据Google OAuth 1.0 for Apps 的文档,您是否使用hd 参数?

      此外,OAuth 1.0 自 2012 年 4 月起已弃用。

      【讨论】:

      • 仍然更喜欢 OAuth 1.0 到 OAuth 2.0 !!
      • 是的,很多人都这样做。但是,如果 OP 使用 1.0 进行开发,然后 Google 终止支持(我相信他们不会),那么他无论如何都必须迁移到 2.0。
      • OAuth 1 很可能在 April 2015 之前得到支持。
      • 我发送这个请求 $xmlresponse= file_get_contents("mail.google.com/mail/feed/atom/…); print_r($out); 这得到未经授权的消息
      猜你喜欢
      • 2015-03-05
      • 2012-11-19
      • 2020-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-23
      • 2019-09-09
      • 2016-08-12
      相关资源
      最近更新 更多