【问题标题】:google plus api invalid_requestgoogle plus api invalid_request
【发布时间】:2013-01-02 15:13:43
【问题描述】:

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

我一直在尝试使用刷新代码。 (我不想重复使用过期的访问代码)。我的代码基于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->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

【问题讨论】:

    标签: php oauth google-api google-plus


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      我认为您的代码是正确的。请检查执行步骤以验证是否执行了正确的代码。

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

      【讨论】:

        【解决方案3】:

        您需要请求令牌以使用“离线”而不是“在线”才能取回刷新令牌。

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

        【讨论】:

        • 我更改了它,但现在它显示错误“:invalid_grant”。还有什么方法可以通过使用 php sdk 来做到这一点
        • 您好,在阅读完文档后,我更新了代码。现在它显示“暂时移动”。我通过添加 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); 解决了这个问题现在的问题是响应为 null 。请参阅上面的“修改代码”部分(最后)
        • 我尝试打印返回的响应。其实就是google的登录页面
        • 我刚刚发现如果我在请求中指定client_secret,它会告诉错误:invalid_request Parameter not allowed for this message type: client_secret
        • 如果删除 client_secret 则显示缺少必需参数:response_type。他们的文档显示要提供 5 个参数,而我只有 5 个参数,仅此而已
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-10
        • 1970-01-01
        • 2013-08-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多