2

大学のプロジェクトで YouTube API を使用していますが、エラーが発生し続けます。ここでは、ログインするために認証ページに送信します。アクセスが許可されると、$_GET['code'] 文字列が返されます。次に、これを他のデータと一緒に送信すると、JSON オブジェクトが返されます。代わりに私はただ得ています

警告: file_get_contents(https://accounts.google.com/o/oauth2/token) [function.file-get-contents]: ストリームを開くことができませんでした: HTTP リクエストが失敗しました! http://www.example.net/callback.phpの 27 行目の HTTP/1.0 400 Bad Request

セキュリティのためだけにドメインを example.net に置き換えました

urlencode($_GET['コード']),
                'client_id' => urlencode('111522767640.apps.googleusercontent.com '),
                'client_secret' => urlencode('secret'),
                'redirect_uri' => urlencode('http://example.net/callback.php'),
                'grant_type' => urlencode('authorization_code')
            )
        );

        $params =
        array('http' =>
            配列(
                'メソッド' => 'POST /o/oauth2/token HTTP/1.1',
                'header' => 'ホスト: accounts.google.com\r\n'.                           
                            'コンテンツタイプ: application/x-www-form-urlencoded',
                'コンテンツ' => $postdata
            )
        );

        $context = stream_context_create($params);
        $result = file_get_contents('https://accounts.google.com/o/oauth2/token', false,$context);
        var_dump($_SESSION);
        var_dump($結果);
    }
    else //コードが設定されていない場合、ユーザーは間違ってここに来たか、このプログラムへのアクセスを拒否したに違いありません
    {
        //header( '場所: www.example.net/error.php' ) ;
    }

?>
4

2 に答える 2

0

oauth2 を使用している場合は、libraries/oauth2/provider.php に移動し、182 行目のコードのコメントを解除します。

            $ci = get_instance();
            $ci->load->spark('curl/1.2.1');
            $ci->curl
                ->create($url)
                ->post($params, array('failonerror' => false));

            $response = $ci->curl->execute();
于 2016-09-17T10:35:23.840 に答える
0

file_get_contentsGET指定された URLにリクエストを送信しようとしていoauth2/tokenますが、リクエストが必要POSTです。

リファレンスGoogle OAuth2PHP HTTPを参照してください。

于 2012-03-24T20:13:55.873 に答える