Alexandru Circus
Alexandru Circus

Reputation: 5538

android: google APi OAuth 2.0 - error 400 getting user info

I have the following on a web servlet:

EDITED:

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;
}

In android:

    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();
    }
}

and

@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);
    }
}

After this, I send the request token to my servlet where I tried to get user profile, but with no success.

Could you please tell me what's wrong and why I'm getting error 400 from google?

Thanks.

Upvotes: 0

Views: 1409

Answers (1)

Antonio Saco
Antonio Saco

Reputation: 1650

Unfortunately, I can see a few issues with this already

  1. you should never have curly braces in your URL or even in the Bearer header as stated in the draft.

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

  1. 400 means that you're missing something in your request, there is probably more information about it in the same response as specific error node.

  2. Finally, take care, oauth_verifier param is from OAuth 1.

I suggest you test your request URL's, using the Google OAuth2 playground

Good luck!

Upvotes: 1

Related Questions