【发布时间】:2012-09-02 02:40:50
【问题描述】:
我正在尝试通过 Gmail OAuth 2.0 访问用户的邮件,我正在通过 Google 的 OAuth 2.0 Playground 解决这个问题
在这里,他们指定我需要将其作为 HTTP 请求发送:
POST /mail/feed/atom/ HTTP/1.1
Host: mail.google.com
Content-length: 0
Content-type: application/json
Authorization: OAuth SomeHugeOAuthaccess_tokenThatIReceivedAsAString
我尝试编写代码来发送此请求,如下所示:
$crl = curl_init();
$header[] = 'Content-length: 0
Content-type: application/json';
curl_setopt($crl, CURLOPT_HTTPHEADER, $header);
curl_setopt($crl, CURLOPT_POST, true);
curl_setopt($crl, CURLOPT_POSTFIELDS, urlencode($accesstoken));
$rest = curl_exec($crl);
print_r($rest);
不工作,请帮忙。 :)
更新:我接受了 Jason McCreary 的建议,现在我的代码如下所示:
$crl = curl_init();
$headr = array();
$headr[] = 'Content-length: 0';
$headr[] = 'Content-type: application/json';
$headr[] = 'Authorization: OAuth '.$accesstoken;
curl_setopt($crl, CURLOPT_HTTPHEADER,$headr);
curl_setopt($crl, CURLOPT_POST,true);
$rest = curl_exec($crl);
curl_close($crl);
print_r($rest);
但是我没有得到任何输出。我认为 cURL 在某处默默地失败了。请帮忙。 :)
更新 2: NomikOS 的伎俩为我做到了。 :) :) :) 谢谢!!
【问题讨论】:
-
无耻的自我推销。你可能对我写的 oAuth 库感兴趣:github.com/PeeHaa/PHPoAuthLib
-
哦,嘿,这看起来很酷。稍后会检查它:)
-
@peehaa +1..... 好东西必须推广,无论如何。
-
@PeeHaa 好的,对不起。我会收回我的陈述。