0

構築中の Web アプリの OpenID / oAuth2 ログイン プロセスを作成しています。

Google+ に対して認証リクエストを行うことができます。私のredirect_uriにはいくつかのパラメータが返されます:

access_token = averylongstring
token_type = Bearer
expires_in = 3600
id_token = anextremelylongstring
refresh_token = 1/Jo_tXhJtL3sQTBZURWyKWwebQSjxY1Rb-7sflDC74Pw
created = 1370140758

次に、次のような cURL GET を実行します。

$url = 'https://www.googleapis.com/oauth2/v1/userinfo';
$curl_data = "?access_token=".$tokens['access_token']."&code=".urlencode('xxxxxxxxx.apps.googleusercontent.com')."&client_id=".urlencode('xxxxxxxxx.apps.googleusercontent.com')."&client_secret=".urlencode('xxxxxxxxx')."&redirect_uri=".urlencode('http://www.mydomain.com/oauth2callback.php')."&grant_type=authorization_code";

これにより、ID が得られます。これは、認証フローの成功した結論であると思います。

それで、私はどこで状態変数を失いましたか

https://accounts.google.com/o/oauth2/auth?response_type=code&redirect_uri=http%3A%2F%2Fwww.mydomain.com%2Foauth2callback.php&client_id=xxxxxxxxxxxxxx.apps.googleusercontent.com&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fplus.login&access_type=offline&approval_prompt=force&state={%22cid%22%3A%22%22%2C%22tid%22%3A%221370142477%22%2C%22app%22%3A%22anappname%22%2C%22Provider%22%3A%22%22}

...そして私のredirect_uri?

4

1 に答える 1

0

コールバック URL へのリダイレクトが 2 つあるようです。最初のリダイレクトでは、ユーザーが Google 情報へのアクセスを許可した直後に、URL 変数「state」と「code」がコールバック ページに送信されます。しかし、別の呼び出しである最終的な認証要求がコールバック ページから行われ、その結果はクエリ文字列のないクリーンな URL になります。

そのため、コールバック ページへの最初のリダイレクト中に、GET 変数を読み取り、それらを SESSION 変数に変換します。次に、cURL が実行されます (そして、cURL の投稿を作成しているため、この部分がまだ正しくありません:

https://accounts.google.com/o/oauth2/token

...そして私のcURLコードは次のようになります:

    $ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"$url");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('access_token' => $access_token,
                                                            'code' => $_REQUEST['code'],
                                                            'client_id' => 'myclientid.apps.googleusercontent.com',
                                                            'client_secret' => 'mylittlesecret',
                                                            'redirect_uri' => 'http://www.mydomain.com/oauth2callback.php',
                                                            'grant_type' => 'authorization_code',
                                                            'approval_prompt' => 'force')));

取得:

{ "error" : "invalid_request" }

コーディング プロジェクトのこの部分では、まったく喜びを感じません。

于 2013-06-03T15:22:50.347 に答える