2012-02-11 121 views
0

我能夠使用oAuth 2.0獲取access_token以獲得多個權限,如電子郵件,聯繫人,文檔等。我有access_token 我有使用以下代碼的聯繫人。Google API使用access_token獲取收件箱電子郵件?

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

現在我想要使用此access_token獲取用戶電子郵件。 我用這個網址。但它給401 unauthorized Error

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

請指導我如何使用可以拿到的access_token電子郵件。

回答

2

我已經看到使用oauth_token作爲請求參數的Gmail提要的引用。但是,一旦我使用了OAuth Playground,我發現您需要將OAuth信息作爲Authorization標頭傳遞,如下所示。

<?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}\"\r\n" 
) 
); 

$context = stream_context_create($opts); 

$out = file_get_contents($path, false, $context); 
?> 
相關問題