我在我的 android 应用程序中使用 oAuth 来登录它。我想在成功登录后提取用户名或电子邮件地址?有什么办法吗?如果是 - 如何?
我发现了这个:Extracting Gmail username with OAuth access token 但真的不知道如何使用它
我在我的 android 应用程序中使用 oAuth 来登录它。我想在成功登录后提取用户名或电子邮件地址?有什么办法吗?如果是 - 如何?
我发现了这个:Extracting Gmail username with OAuth access token 但真的不知道如何使用它
好吧,我一直在挖掘互联网直到它结束,我想出了这个:
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的略微修改版本
尽管如此,还是感谢 William Carter 付出的时间和精力。