【问题标题】:android google acc: Exchange the authorization_code for an access_tokenandroid google account:用授权码交换access_token
【发布时间】:2012-06-27 18:21:46
【问题描述】:

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

首先获取授权码:

已编辑:

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

}

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

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

谢谢。

【问题讨论】:

    标签: android oauth


    【解决方案1】:

    你是 双重编码 redirect_uri 参数。

    它应该看起来像::

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

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

    【讨论】:

    • 我已经从 redirect_uri 和 client_secret 中删除了 URLEncoder,但仍然 invalid_request 来自服务器。我已经用新的更改编辑了我的帖子。谢谢。
    • 另外,我认为 OAuth Consumer Key 是客户端 ID,OAuth Consumer Secret 是客户端密码。
    • 我意识到您的重定向 URI 无效(由于标准),您应该使用已在您的 Google API 控制台 > API 访问 > Web 应用程序的客户端 ID > 重定向 URI 中获得授权的其中一个/跨度>
    • OAuth Consumer Key 和 OAuth Consumer Secret 这两个术语来自 OAuth1,注意不要混用。我建议不要使用该版本的实现(已弃用)。
    猜你喜欢
    • 1970-01-01
    • 2017-03-24
    • 1970-01-01
    • 2019-08-07
    • 2012-06-20
    • 2014-11-06
    • 2011-08-05
    • 1970-01-01
    • 2015-04-17
    相关资源
    最近更新 更多