google plus api invalid_request

时间:2013-01-18 20:12:37

标签: php oauth google-api google-plus

过去几天我一直试图解决这个问题。我已经阅读了所有的论坛,仍然没有解决方案。任何帮助表示赞赏。

我一直在尝试使用刷新代码。 (我不希望在过期时重用访问代码)。我的代码基于http://www.jensbits.com/demos/ga/app/ga_app_oauth2_refreshtoken.txt

代码如下:

if (isset($_GET['code'])) {
    $accessToken = get_oauth2_token($_REQUEST['code'],"online"); //refresh_token not fetched

}

//请注意,如果我将上述行替换为$ client-> authenticate()然后$ client-> getAccessToken(),则该程序正在运行。但后来我没有得到刷新代码

下面我从链接

粘贴功能
function get_oauth2_token3($grantCode, $grantType) {
$redirect_uri = "xxxx";

$oauth2token_url = "https://accounts.google.com/o/oauth2/token";
$clienttoken_post = array(
    "client_id" => GCLIENT_ID,
    "client_secret" => GCLIENT_SECRET);

if ($grantType === "online") {
    $clienttoken_post["code"] = $grantCode;
    $clienttoken_post["redirect_uri"] = $redirect_uri;
    $clienttoken_post["grant_type"] = "authorization_code";
}

if ($grantType === "offline") {
    $clienttoken_post["refresh_token"] = $grantCode;
    $clienttoken_post["grant_type"] = "refresh_token";
}

$curl = curl_init($oauth2token_url);

curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $clienttoken_post);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

$json_response = curl_exec($curl);
curl_close($curl);

echo '<pre>';
print_r($grantCode);
print_r($json_response);
print_r(json_decode($json_response));
echo '</pre>';

此处没有刷新

$authObj = json_decode($json_response);

//if offline access requested and granted, get refresh token
if (isset($authObj->refresh_token)) {
    global $refreshToken;

没有刷新令牌         $ refreshToken = $ authObj-&gt; refresh_token;     }

$accessToken = $authObj->access_token;
return $accessToken;

}

修改后的代码

if ($grantType === "offline") {
    $clienttoken_post["grant_type"] = "authorization_code";
    $clienttoken_post["response_type"] = "code";

    $clienttoken_post["scope"] = "https://www.googleapis.com/auth/plus.me";
    $clienttoken_post["redirect_uri"] = $redirect_uri;
}

还将网址从 https://accounts.google.com/o/oauth2/token更改为https://accounts.google.com/o/oauth2/auth

3 个答案:

答案 0 :(得分:1)

您需要请求令牌才能使用“离线”代替“在线”才能获得刷新令牌。

有关幕后情况的概述,请参阅https://developers.google.com/accounts/docs/OAuth2WebServer,这有助于了解OAuth 2离线流量。

答案 1 :(得分:1)

我觉得你的代码是正确的。请检查执行步骤以验证是否执行了正确的代码。

如果您的SDK不是最新版本,请尝试更新。当我遇到这个问题时,我更新了SDK并且它有效......

答案 2 :(得分:-1)

此消息的另一个可能原因是具有不正确的CLIENT_ID或CLIENT_SECRET