【发布时间】:2011-08-06 21:50:04
【问题描述】:
我需要获取 google 有效令牌才能使用 Google API,但是 我的代码不起作用。你能给我建议吗?
$client_id = '495225261106.apps.googleusercontent.com';
$client_secret = urlencode('MY_SECRET_CDE');
$redirect_uri = urlencode('http://MYPAGE.net/test.php');
//$grant_type = urlencode('authorization_code'); //it does not work either.
$grant_type = 'authorization_code';
$post_string = "code=4/P7q7W91a-oMsCeLvIaQm6bTrgtp6&client_id={$client_id}&client_secret={$client_secret}&redirect_uri={$redirect_uri}&grant_type={$grant_type}";
//echo_key_value('post_string',$post_string);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://accounts.google.com/o/oauth2/token');
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$result = curl_exec($ch); // Execute the HTTP command
$errmsg = curl_error($ch);
if($errmsg) echo $errmsg;
// 输出: {"错误":"invalid_grant"} //
谢谢!
【问题讨论】:
-
我正在尝试在 javascript 中实现这一点,但遇到了同样的错误。我试过从命令行使用 curl ,但仍然没有运气。我已按照this page 上的说明进行操作。