我一直在https://developers.google.com/+/web/signin/服务器端流到我有一个授权代码发送到我的服务器的点。然后在PHP中,我尝试使用以下方法获取访问令牌:
$data = "code=".urlencode(trim($access_code)).
"&grant_type=authorization_code".
"&client_id=".urlencode($this->client_id).
"&client_secret=".urlencode($this->client_secret).
"&redirect_uri=".urlencode(trim($redirect_uri));
$curl = curl_init("https://accounts.google.com/o/oauth2/token");
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$result = json_decode(curl_exec($curl), true);
curl_close($curl);
return $result;
redirect_uri在PHP中与在google-dev控制台中的redirect-uris中完全相同,但调用总是返回:
array(1) { ["error"]=> string(21) "redirect_uri_mismatch" }
我做错了什么?
作为参考,这是我的谷歌API设置的样子:
http://such-nom.com
http://www.such-nom.com
var作为$redirect_uri:传递
$redir = 'http://such-nom.com';
编辑:当请求令牌是在服务器端生成而不是通过谷歌按钮生成时,PHP的工作原理似乎完全相同。
原来问题是添加了一个尾随斜杠并等待了几个小时。因此,URL的完整列表如下所示:
http://such-nom.com
http://www.such-nom.com
http://such-nom.com/
http://www.such-nom.com/