我正在研究Android,我正在实施Google+ Sign。
我已经认证很好,但是,我希望得到用户的名字,姓氏和电子邮件地址。
我正在使用以下内容尝试获取用户的名称,但是,这只会显示我的显示名称,而不是我通过Google+登录为网络设置的名字和姓氏。
if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null)
{
Person currentPerson = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
Log.d("Display Name", currentPerson.getDisplayName());
}
第二个问题,我无法从响应中收到电子邮件。如果我调试应用程序并查看响应,我可以看到我的电子邮件地址,但由于某些原因在代码中它只是循环并错过了我的电子邮件所以返回null。
以下是电子邮件检索的代码。
final String account = Plus.AccountApi.getAccountName(mGoogleApiClient);
AsyncTask<Void, Void, String> task = new AsyncTask<Void, Void, String>()
{
@Override
protected String doInBackground(Void... params) {
HttpURLConnection urlConnection = null;
try
{
URL url = new URL("https://www.googleapis.com/plus/v1/people/me");
String sAccessToken = GoogleAuthUtil.getToken(SignIn.this, account,
"oauth2:" + Scopes.PLUS_LOGIN + " https://www.googleapis.com/auth/plus.profile.emails.read");
urlConnection = (HttpURLConnection)url.openConnection();
urlConnection.setRequestProperty("Authorization", "Bearer " + sAccessToken);
String content = CharStreams.toString(new InputStreamReader(urlConnection.getInputStream(),
Charsets.UTF_8));
if (!TextUtils.isEmpty(content))
{
JSONArray emailArray = new JSONObject(content).getJSONArray("emails");
for (int i = 0; i < emailArray.length(); i++)
{
JSONObject obj = (JSONObject)emailArray.get(i);
if (obj.getString("type") == "account")
{
return obj.getString("value");
}
}
}
}
catch (UserRecoverableAuthException ex)
{
startActivityForResult(ex.getIntent(), RC_SIGN_IN_GET_EMAIL);
Log.e("SignIn", ex.toString());
}
catch (Exception ex)
{
Log.e("SignIn", ex.toString());
}
finally
{
if (urlConnection != null)
{
urlConnection.disconnect();
}
}
return null;
}
@Override
protected void onPostExecute(String info)
{
registerUserFromGooglePlusSignIn(info);
}
};
task.execute();
}
感谢您提供的任何帮助。
答案 0 :(得分:8)
在onConnected
Person person = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
String firstName = person.getName().getGivenName();
String lastName = person.getName().getFamilyName();
检查更多字段 https://developers.google.com/android/reference/com/google/android/gms/plus/model/people/Person.Name
答案 1 :(得分:2)
在连接上你需要
Person person = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
String name = person.getDisplayName(); //full name
Plus.AccountApi.getAccountName(mGoogleApiClient) //email
显示名称是名字和姓氏
答案 2 :(得分:1)
//After the signing we are calling this function
private void handleSignInResult(GoogleSignInResult result) {
//If the login succeed
if (result.isSuccess()) {
signInButton.setVisibility(View.GONE);
//Getting google account
GoogleSignInAccount acct = result.getSignInAccount();
String name=acct.getDisplayName();
String email=acct.getEmail();
Log.d("namegoogle",name);
Log.d("emailgoogle",email);
edit_signin_emailid.setText(email);
String fullname = acct.getDisplayName();
String[] parts = fullname.split("\\s+");
Log.d("Length-->",""+parts.length);
if(parts.length==2) {
String firstname = parts[0];
String lastname = parts[1];
Log.d("First-->", "" + firstname);
Log.d("Last-->", "" + lastname);
AppController.setStringPref("firstnamebook", firstname);
AppController.setStringPref("lastnamebook", lastname);
Log.d("FirstApp", "" + AppController.getStringPref("firstnamebook"));
Log.d("LastApp", "" + AppController.getStringPref("lastnamebook"));
}
else if(parts.length==3){
String firstname = parts[0];
String middlename = parts[1];
String lastname = parts[2];
Log.d("First-->", "" + firstname);
Log.d("Last-->", "" + lastname);
AppController.setStringPref("firstnamebook", firstname);
AppController.setStringPref("lastnamebook", lastname);
}
}
}