2012-06-12 59 views
0

我已在上一个网页的servlet如下:的Android:谷歌APi的OAuth 2.0用户 - 错误400获取用户信息

编辑:

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

之后,我发送r equest令牌到我的servlet,我试图获取用户配置文件,但没有成功。

你能告诉我什么是错的,为什么我从谷歌得到错误400?

谢谢。

回答

0

不幸的是,我可以看到一些使用问题,已经

  1. should never havecurly braces在您的网址甚至在草案规定的Bearer header

    连接=(HttpURLConnection类新的网址( “https://www.googleapis.com/oauth2/v1/userinfo?access_token={ ”+ AUTH_TOKEN +“ }”))。的openConnection()

  2. 400意味着你失去了一些东西在您的请求,在与特定错误节点相同的响应中可能有更多关于它的信息。

  3. 最后,照顾,oauth_verifier参数是从OAuth的1

我建议你测试你的URL请求的,使用Google OAuth2 playground

祝你好运!

+0

3.我不使用oauthVerifier。 –

+0

1.请参阅:https://developers.google.com/accounts/docs/OAuth2Login#userinfocall。我改变了请求(删除大括号和添加头文件)。请参阅上面的编辑。 –

+0

此外,使用谷歌OAuth2操场和击中查找可用的URI,我得到:“发生了什么不愉快的事情:invalid_token” –

相关问题