获取Google API令牌

Exp*_* be 5 token oauth-2.0

我需要获取谷歌有效令牌才能使用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;
Run Code Online (Sandbox Code Playgroud)

// 输出: {"error":"invalid_grant"} //

谢谢!

mim*_*ing 1

您可能会发现通过官方客户端库之一使用 Google API(尤其是 OAuth 内容)更容易。

这是 PHP 的链接: http: //code.google.com/p/google-api-php-client/

以及 OAuth 2.0 文档和库的链接(带有一些很棒的示例代码):http://code.google.com/p/google-api-php-client/wiki/OAuth2