use*_*778 10 android google-api google-plus
我已将Google plus与我的Android应用集成.一切正常,我也连接到谷歌加,但我无法得到当前用户的名称记录.
public void onConnected(Bundle connectionHint) {
String personName="Unknown";
if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
Person currentPerson = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
personName = currentPerson.getDisplayName();
}
}
Run Code Online (Sandbox Code Playgroud)
Plus.PeopleApi.getCurrentPerson(mGoogleApiClient)
此方法始终返回null.
任何帮助将不胜感激.谢谢.
Pet*_*ter 10
对我而言,此次调用返回null的原因是我的应用程序未启用Google+ API.导航到https://console.developers.google.com,选择您的项目并启用Google+ API以使其正常运行!
你必须添加这一行:
Plus.PeopleApi.loadVisible(mGoogleApiClient, null).setResultCallback(this);
Run Code Online (Sandbox Code Playgroud)
像这样:
public void onConnected(Bundle connectionHint) {
/* This Line is the key */
Plus.PeopleApi.loadVisible(mGoogleApiClient, null).setResultCallback(this);
String personName="Unknown";
if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
Person currentPerson = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
personName = currentPerson.getDisplayName();
.........
}
}
Run Code Online (Sandbox Code Playgroud)
在我的情况下,null返回,因为我已添加该行:
addScope(新范围(Scopes.EMAIL))
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(Plus.API)
.addScope(new Scope(Scopes.PROFILE))
//.addScope(new Scope(Scopes.EMAIL))
.build();
Run Code Online (Sandbox Code Playgroud)
================= UPDATE ========================范围应设置如下:
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(Plus.API)
.addScope(Plus.SCOPE_PLUS_PROFILE)
.addScope(Plus.SCOPE_PLUS_LOGIN)
.build();
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
9718 次 |
最近记录: |