2012-01-19 119 views
1

我在使用Oauth 2.0閱讀Gmail中的收件箱時遇到問題。我用這個作爲我的範圍:https://mail.google.com/mail/feed/atom/Unauthorize 401 Oauth 2.0 google

這是我的非工作代碼

$fields=array(
    'code'=> urlencode($authcode), 
    'client_id'=> urlencode($clientid), 
    'client_secret'=> urlencode($clientsecret), 
    'redirect_uri'=> urlencode($redirecturi), 
    'grant_type'=> urlencode('authorization_code') 
); 

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

$ch = curl_init(); 

curl_setopt($ch,CURLOPT_URL,'https://accounts.google.com/o/oauth2/token'); 
curl_setopt($ch,CURLOPT_POST,5); 
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
$result = curl_exec($ch); 
curl_close($ch); 

$response= json_decode($result); 
$accesstoken= $response->access_token; 

$xmlresponse= file_get_contents('https://mail.google.com/mail/feed/atom/?oauth_token='.$accesstoken); 

我甚至把我的訪問令牌,但仍然沒有運氣,得到一個取消授權401錯誤。

+0

我有同樣的問題。如果你有解決方案。請張貼在這裏。 – Asghar

回答

1

https://mail.google.com/mail/feed/atom/不在其範圍內,因爲您可以從中獲取供稿的服務器端點。請參閱doc。這裏是Vb.net

objClient.Credentials = New System.Net.NetworkCredential(username, password) 
Dim nodelist As XmlNodeList 
Dim node As XmlNode 
Dim response As String 
Dim xmlDoc As New XmlDocument 

'get emails from gmail 
response = Encoding.UTF8.GetString(objClient.DownloadData("https://mail.google.com/mail/feed/atom")) 
response = response.Replace("<feed version=""0.3"" xmlns=""http://purl.org/atom/ns#"">", "<feed>") 

'Get the number of unread emails 
xmlDoc.LoadXml(response) 
node = xmlDoc.SelectSingleNode("/feed/fullcount") 
mailCount = node.InnerText 
nodelist = xmlDoc.SelectNodes("/feed/entry") 
node = xmlDoc.SelectSingleNode("title") 

一個工作代碼使用PHP和客戶端登錄

<?php 
    $mailbox = imap_open("{imap.googlemail.com:993/ssl}INBOX", "[email protected]", "PASSWORD"); 
    $mail = imap_search($mailbox, "ALL"); 
    $mail_headers = imap_headerinfo($mailbox, $mail[0]); 
    $subject = $mail_headers->subject; 
    $from = $mail_headers->fromaddress; 
    imap_setflag_full($mailbox, $mail[0], "\\Seen \\Flagged"); 
    imap_close($mailbox); 
?> 

我不知道如何做到這一點使用OAuth2.0的或者即使它可能使用OAuth獲得飼料2.0。

+0

我可以在PHP中執行此操作嗎? –

+0

編輯答案。我不知道您是否可以使用OAuth2.0獲取收件箱Feed。 – Shadow

+0

謝謝。對未來很好的參考。 –