0

Webサーブレットには次のものがあります。

編集:

public String tryGoogleAuthentication(String auth_token){

    HttpURLConnection connection = null;
    try {

        //connection = (HttpURLConnection) new URL(("https://www.googleapis.com/oauth2v1/tokeninfo?access_token={"+auth_token+"}")).openConnection();
       connection = (HttpURLConnection) new URL(("https://www.googleapis.com/oauth2/v1/user info")).openConnection();
       connection.setRequestMethod("GET");          
       connection.setRequestProperty("Authorization", "Bearer {"+auth_token+"}");
       connection.setRequestProperty("Host", "googleapis.com");

        //read response
        String response = fromInputStreamToString(connection.getInputStream());
        System.out.println(response);

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return CONST.STATUS_OK;
}

アンドロイドの場合:

    private void googleAuthenticate(){
    try {
        mOAauthHelper = new OAuthHelper("something.net", "xxxxxxxxxx", 
                "https://www.googleapis.com/auth/userinfo.profile", "alex://myScheme");
        String uri = mOAauthHelper.getRequestToken();

        startActivity(new Intent("android.intent.action.VIEW", Uri.parse(uri)));

                    //Intent i = new Intent(this, GoogleOAUTHActivity.class);
                    //i.putExtra(GoogleOAUTHActivity.GOOGLE_OAUTH_ENDPOINT_KEY, uri);           
                     //startActivity(i);

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
        failedAuthenticatingAtGoogle();
    } catch (OAuthMessageSignerException e) {
        e.printStackTrace();
        failedAuthenticatingAtGoogle();
    } catch (OAuthNotAuthorizedException e) {
        e.printStackTrace();
        failedAuthenticatingAtGoogle();
    } catch (OAuthExpectationFailedException e) {
        e.printStackTrace();
        failedAuthenticatingAtGoogle();
    } catch (OAuthCommunicationException e) {
        e.printStackTrace();
        failedAuthenticatingAtGoogle();
    }
}

@Override
protected void onNewIntent(Intent intent) {
    //super.onNewIntent(intent);

    Uri uri = intent.getData();
    String oauthToken = uri.getQueryParameter("oauth_token");
    String oauthVerifier = uri.getQueryParameter("oauth_verifier");

    if(oauthToken != null){
        authorizeGoogleSessionToServer(oauthToken);
    }
}

この後、リクエスト トークンをサーブレットに送信し、そこでユーザー プロファイルを取得しようとしましたが、成功しませんでした。

何が問題なのか、Google からエラー 400 が表示される理由を教えてください。

ありがとう。

4

1 に答える 1

1

残念ながら、これにはすでにいくつかの問題があります

  1. ドラフトに記載されているように、URL やBearer ヘッダーに含めるべきではありませんcurly braces

connection = (HttpURLConnection) 新しい URL(("https://www.googleapis.com/oauth2/v1/userinfo?access_token= { "+auth_token+" } ")).openConnection()

  1. 400は、リクエストに何かが欠けていることを意味します。おそらく、特定のエラー ノードと同じ応答に、それに関する詳細情報があります。

  2. 最後に、oauth_verifierparam は OAuth 1 のものです。

Google OAuth2 プレイグラウンドを使用して、リクエスト URL をテストすることをお勧めします

幸運を!

于 2012-06-25T21:46:46.167 に答える