Boa*_*rdy 1 android google-plus
我正在开发一个机器人,我正在实施Google+ Sign.
我已经认证很好,但是,我想得到用户的名字,姓氏和电子邮件地址.
我使用以下内容尝试获取用户的名称,但是,这只能获取显示名称,而不是我通过Google+登录网站分别创建的名字和姓氏.
if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null)
{
Person currentPerson = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
Log.d("Display Name", currentPerson.getDisplayName());
}
Run Code Online (Sandbox Code Playgroud)
第二个问题,我无法从响应中收到电子邮件.如果我调试应用程序并查看响应,我可以看到我的电子邮件地址,但由于某些原因在代码中它只是循环并错过了我的电子邮件所以返回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();
}
Run Code Online (Sandbox Code Playgroud)
感谢您的任何帮助,您可以提供.
在onConnected
Person person = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
String firstName = person.getName().getGivenName();
String lastName = person.getName().getFamilyName();
Run Code Online (Sandbox Code Playgroud)
检查更多字段 https://developers.google.com/android/reference/com/google/android/gms/plus/model/people/Person.Name
| 归档时间: |
|
| 查看次数: |
3449 次 |
| 最近记录: |