2012-06-12 61 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」 –

相關問題