2013-05-14 157 views
0

我在我的android應用程序中使用oAuth登錄到它。我想在成功登錄後提取用戶名或電子郵件地址?有什麼辦法可以做到嗎?如果是 - 如何?如何從oauth獲取用戶名或電子郵件地址?

我發現這一點:Extracting Gmail username with OAuth access token但真的不知道如何使用它

+1

您如何首先獲得OAuth令牌?如果您從某個第四方應用程序收到它,請忽略此評論。否則,您需要從用戶憑證生成令牌*。 [Android文檔中的教程](http://developer.android.com/training/id-auth/authenticate.html#RequestToken)應該可以幫助你做到這一點。這些示例涵蓋了gmail,因此應該對您有用。如果您剛開始使用OAuth,[本指南](http://hueniverse.com/oauth/)真的可以幫助您解決問題。 – 2013-05-14 22:37:41

回答

0

好,我Digg的網絡,直到它的結束,我想出了這一點:

private String usernameReader() { 
     String jsonOutput = ""; 
     try { 
      jsonOutput = makeSecuredReq(Constants.API_REQUEST, getConsumer(this.prefs)); 
      JSONObject jsonResponse = new JSONObject(jsonOutput); 
      JSONObject m = (JSONObject) jsonResponse.get("feed"); 
      JSONObject email = (JSONObject) m.get("id"); 
      jsonOutput=email.getString("$t"); 
     } catch (Exception e) { 
      Log.e("errroro", "Error executing request", e); 
      // console.setText("Error retrieving contacts : " + jsonOutput); 
     } 
     return jsonOutput; 
    } 

    private String makeSecuredReq(String url, OAuthConsumer consumer) throws Exception { 
     DefaultHttpClient httpclient = new DefaultHttpClient(); 
     HttpGet request = new HttpGet(url); 
     Log.i("Requesting", "Requesting URL : " + url); 
     consumer.sign(request); 
     HttpResponse response = httpclient.execute(request); 
     Log.i("statusLine", "Statusline : " + response.getStatusLine()); 
     InputStream data = response.getEntity().getContent(); 
     BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(data)); 
     String responeLine; 
     StringBuilder responseBuilder = new StringBuilder(); 
     while ((responeLine = bufferedReader.readLine()) != null) { 
      responseBuilder.append(responeLine); 
     } 
     // Log.i(C.TAG,"Response : " + responseBuilder.toString()); 
     return responseBuilder.toString(); 
    } 

    private OAuthConsumer getConsumer(SharedPreferences prefs) { 
     String token = prefs.getString(OAuth.OAUTH_TOKEN, ""); 
     String secret = prefs.getString(OAuth.OAUTH_TOKEN_SECRET, ""); 
     OAuthConsumer consumer = new CommonsHttpOAuthConsumer(Constants.CONSUMER_KEY, Constants.CONSUMER_SECRET); 
     consumer.setTokenWithSecret(token, secret); 
     return consumer; 
    } 

它只是略作修改這個版本http://androidwarzone.blogspot.com/2011/07/android-oauth-full-example-with-source.html

不過,謝謝你的威廉卡特你的時間和精力。

相關問題