android google acc: Exchange the authorization_code for an access_token

谁都会走 提交于 2019-12-25 05:14:33

问题


I tried to to exchange auth code for an access token, but no success. I need to access user profile. Here is my code:

first get auth code:

EDITED:

private void googleAuthenticate(){
    try {
        mOAauthHelper = new OAuthHelper("something.net", "xxxxx", 
                "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

public OAuthHelper(String consumerKey, String consumerSecret, String scope,
        String callbackUrl) throws UnsupportedEncodingException {
    mConsumer = new CommonsHttpOAuthConsumer(consumerKey, consumerSecret);
    mProvider = new CommonsHttpOAuthProvider(
            "https://www.google.com/accounts/OAuthGetRequestToken?scope="
                    + URLEncoder.encode(scope, "utf-8"),
            "https://www.google.com/accounts/OAuthGetAccessToken",
            "https://www.google.com/accounts/OAuthAuthorizeToken?hd=default");
    mProvider.setOAuth10a(true);
    mCallbackUrl = (callbackUrl == null ? OAuth.OUT_OF_BAND : callbackUrl);
}

and

    @Override
protected void onNewIntent(Intent intent) {

    System.out.println("onNewIntent");

    try {
        Uri uri = intent.getData();
        String oauthToken = uri.getQueryParameter("oauth_token");
       // String oauthToken = uri.getQueryParameter("oauth_verifier");
        System.out.println(oauthToken);

        if(oauthToken != null){
            HttpClient client = new DefaultHttpClient();
            HttpPost post = new HttpPost("https://accounts.google.com/o/oauth2/token");
            List<NameValuePair> pairs = new ArrayList<NameValuePair>();
            pairs.add(new BasicNameValuePair("code", oauthToken));
            pairs.add(new BasicNameValuePair("client_id", "something.net"));
            pairs.add(new BasicNameValuePair("client_secret", "xxxxxx"));
            pairs.add(new BasicNameValuePair("redirect_uri", "alex://myScheme"));
            pairs.add(new BasicNameValuePair("grant_type", "authorization_code")); //Leave this line how it is   
            post.setEntity(new UrlEncodedFormEntity(pairs));
            org.apache.http.HttpResponse response = client.execute(post);
            String responseBody = EntityUtils.toString(response.getEntity());
            System.out.println(responseBody); // That just logs it into logCat

            //authorizeGoogleSessionToServer(oauthToken);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

}

and I get: { "error" : "invalid_request" }

Does anybody know how to solve this problem?

Thanks.


回答1:


You are double encoding the redirect_uri param.

It should look something like::

pairs.add(new BasicNameValuePair("redirect_uri", "your_own_redirect_URI"));

Also, you should be careful to not mix both implementations of the protocol (OAuth1 and OAuth2). They look the same but are not equal.



来源:https://stackoverflow.com/questions/11208324/android-google-acc-exchange-the-authorization-code-for-an-access-token

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!