Php 未经授权401OAuth2.0Google

Php 未经授权401OAuth2.0Google,php,curl,oauth,google-api,Php,Curl,Oauth,Google Api,我在使用OAuth2.0阅读gmail收件箱时遇到问题。我将此用作我的范围: 这是我的非工作代码 $fields=array( 'code'=> urlencode($authcode), 'client_id'=> urlencode($clientid), 'client_secret'=> urlencode($clientsecret), 'redirect_uri'=> urlencode($redirecturi),

我在使用OAuth2.0阅读gmail收件箱时遇到问题。我将此用作我的范围:

这是我的非工作代码

$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错误。

不是服务器端点的作用域,您可以从中获取提要。看。下面是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")
objClient.Credentials=New System.Net.NetworkCredential(用户名、密码)
Dim节点列表作为XmlNodeList
Dim节点作为XmlNode
作为字符串的暗淡响应
Dim xmlDoc作为新的XmlDocument
“从gmail获取电子邮件
response=Encoding.UTF8.GetString(objClient.DownloadData(“https://mail.google.com/mail/feed/atom"))
响应=响应。替换(“,”)
'获取未读电子邮件的数量
LoadXml(响应)
node=xmlDoc.SelectSingleNode(“/feed/fullcount”)
mailCount=node.InnerText
nodelist=xmlDoc.SelectNodes(“/feed/entry”)
node=xmlDoc.SelectSingleNode(“标题”)
使用PHP和客户端登录

<?php
    $mailbox = imap_open("{imap.googlemail.com:993/ssl}INBOX", "USERNAME@googlemail.com", "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实现这一点,甚至不知道是否可以使用OAuth2.0获取提要。

不是可以从中获取提要的服务器端点的作用域。看。下面是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")
objClient.Credentials=New System.Net.NetworkCredential(用户名、密码)
Dim节点列表作为XmlNodeList
Dim节点作为XmlNode
作为字符串的暗淡响应
Dim xmlDoc作为新的XmlDocument
“从gmail获取电子邮件
response=Encoding.UTF8.GetString(objClient.DownloadData(“https://mail.google.com/mail/feed/atom"))
响应=响应。替换(“,”)
'获取未读电子邮件的数量
LoadXml(响应)
node=xmlDoc.SelectSingleNode(“/feed/fullcount”)
mailCount=node.InnerText
nodelist=xmlDoc.SelectNodes(“/feed/entry”)
node=xmlDoc.SelectSingleNode(“标题”)
使用PHP和客户端登录

<?php
    $mailbox = imap_open("{imap.googlemail.com:993/ssl}INBOX", "USERNAME@googlemail.com", "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实现这一点,甚至不知道是否可以使用OAuth2.0获取提要。

编辑了答案。我不知道您是否可以使用OAuth2.0获取收件箱提要。谢谢。很好的未来参考。编辑了答案。我不知道您是否可以使用OAuth2.0获取收件箱提要。谢谢。很好的未来参考。我也有同样的问题。如果你有解决办法。请在这里发帖。我也有同样的问题。如果你有解决办法。请在这里邮寄。