0

我试图用身份验证码交换访问令牌,但没有成功。我需要访问用户个人资料。这是我的代码:

首先获取授权码:

编辑:

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

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

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

}

我得到:{“错误”:“无效请求”}

有谁知道如何解决这个问题?

谢谢。

4

1 回答 1

1

您正在对参数进行双重编码。redirect_uri

它应该看起来像::

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

此外,您应该注意不要混合协议的两种实现(OAuth1 和 OAuth2)。它们看起来一样,但并不相等。

于 2012-06-27T08:57:36.440 回答