Google Auth 1.0 未经授权访问 Gmail


Google Auth 1.0 access gmail unauthorized

我为我的域创建了谷歌应用程序,它也有一些电子邮件地址,所以我要访问那里的收件箱电子邮件计数 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收件箱提要,或者我的客户端是否有任何错误

您向 API 终端节点发出的实际请求仅使用file_get_contents和查询参数。 使用 OAuth 1.0,您需要正确签署此请求,并传入授权标头。 您可能希望使用与上面相同的 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 方 OAuth 1.0 机制的详细信息:https://developers.google.com/accounts/docs/OAuth#AccessFeed

绝对同意升级到OAuth 2.0,尽管:)

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

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