Thr*_*ead 9 php post google-api
也许有人可以在这里指出我可能做错了什么.我正在搞乱Google API和OAuth,所以我可以通过谷歌将用户注册到我的应用程序.我发现他们提供的PHP非常繁琐,所以我决定更多练习执行https请求等.到目前为止,我已经取得了一些成功,但获取令牌以交换用户信息的最后一步需要POST方法.只需重定向浏览器,使用GET方法复制最终URL会返回错误.我即将开始在cURL扩展中挖掘,但也许有人可以发现这段代码有什么问题?
$url = "https://accounts.google.com/o/oauth2/token";
$fields = array(
'code' => $_GET['code'],
'client_id' => $google['clientID'],
'client_secret' => $google['clientSecret'],
'redirect_uri' => "http://www.qwiku.com/scripts/php/google/reg_response.php",
'grant_type' => "authorization_code"
);
$data = http_build_query($fields);
echo $data."<br />";
$context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => 'Content-Type: application/x-www-form-urlencoded',
'content' => $data
)
));
$result = file_get_contents($url, false, $context);
var_dump($result);
Run Code Online (Sandbox Code Playgroud)
结果转储是错误的.你可以在这里看到谷歌文档,但我99%肯定数据是否正确.http://code.google.com/apis/accounts/docs/OAuth2WebServer.html#formingtheurl
UPDATE
我现在正在使用curl,它似乎正在工作.唯一的问题是谷歌返回错误"invalid_grant".不知道为什么它被设置为他们指定的.
$url = 'https://accounts.google.com/o/oauth2/token';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
Run Code Online (Sandbox Code Playgroud)
更新2
我刷新整个过程,因为我只是刷新重定向和宾果游戏,它正在工作.代码必须已过期.
小智 0
您可以尝试使用另一种方法,例如使用 http 客户端类,它为您提供如下所有内容:
http://www.phpclasses.org/package/576-PHP-GET-HEAD-POST-methods-with-a-lot-of-features.html
或者尝试使用 OAuth 类,如下所示:
http://www.phpclasses.org/package/7700-PHP-Authorize-and-access-APIs-using-OAuth.html
我已经通过 Practico Framework 实现了这一点,一切对我来说都很好,而不是使用stream_context_create。
问候
| 归档时间: |
|
| 查看次数: |
16607 次 |
| 最近记录: |