Kot*_*tsu 2 android google-api
我搜索了很多但仍然没有得到解决方案.Plus.PeopleApi.getCurrentPerson()总是返回null.我得到了调试SHA1并释放了SHA1.它们都作为具有正确包名的OAuth 2.0客户端ID添加到控制台.(遗憾的是,我无法发布图片)这是我的GoogleApiClient:
private GoogleApiClient buildGoogleApiClient() {
return new GoogleApiClient.Builder(getContext())
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(Plus.API)
.addScope(new Scope(Scopes.PROFILE))
.addScope(new Scope(Scopes.PLUS_LOGIN))
.build();
}
Run Code Online (Sandbox Code Playgroud)
很简单.我从Fragment的onCreateView()中调用它.然后我想找到当前的人:
@Override
public void onConnected(Bundle bundle) {
Plus.PeopleApi.loadVisible(mGoogleApiClient, null).setResultCallback(this);
if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
Person currentPerson = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
String personName = currentPerson.getDisplayName();
String personGooglePlusProfile = currentPerson.getUrl();
}
Run Code Online (Sandbox Code Playgroud)
并且总是空的.mGoogleApiClient工作正常,它连接,它不是null,但仍然.在控制台 - >启用了Apis Google+ API,但"使用情况"标签中没有任何请求.
我尝试了所有可以找到的解决方案而没有任何帮助.我用geny模拟器运行应用程序.拜托,帮帮我,给我任何想法.
补充:这是我几乎完整的Fragment类:
public class AuthorizationFragment2 extends Fragment implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
View.OnClickListener,
ResultCallback {
private static final String KEY_IS_RESOLVING = "is_resolving";
private static final String KEY_SHOULD_RESOLVE = "should_resolve";
/* Request code used to invoke sign in user interactions. */
protected static final int RC_SIGN_IN = 0;
/* GP Is there a ConnectionResult resolution in progress? */
private boolean mIsResolving = false;
/* GP Should we automatically resolve ConnectionResults when possible? */
private boolean mShouldResolve = false;
private GoogleApiClient mGoogleApiClient;
/* View to display current status (signed-in, signed-out, disconnected, etc) */
private TextView mStatus;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
if (savedInstanceState != null) {
mIsResolving = savedInstanceState.getBoolean(KEY_IS_RESOLVING);
mShouldResolve = savedInstanceState.getBoolean(KEY_SHOULD_RESOLVE);
}
mGoogleApiClient = buildGoogleApiClient();
View view = inflater.inflate(R.layout.authorization_view, container, false);
// Register onClickListener for gp_login_button
view.findViewById(R.id.gp_login_button).setOnClickListener(this);
view.findViewById(R.id.sign_out_button).setOnClickListener(this);
mStatus = (TextView) view.findViewById(R.id.mStatus);
return view;
}
@Override
public void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
@Override
public void onStop() {
super.onStop();
mGoogleApiClient.disconnect();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean(KEY_IS_RESOLVING, mIsResolving);
outState.putBoolean(KEY_SHOULD_RESOLVE, mShouldResolve);
}
@Override
public void onConnected(Bundle bundle) {
Plus.PeopleApi.loadVisible(mGoogleApiClient, null).setResultCallback(this);
// It is allways null
if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
Person currentPerson = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
// Retrieve Person's information
}
updateUI(true);
}
@Override
public void onConnectionSuspended(int i) {
Log.d(MainActivity.LOG_TAG, "onCOnnectionSuspended()");
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
if(!mIsResolving && mShouldResolve) {
if (connectionResult.hasResolution()) {
try {
// It calls the Activity's onActivityResult()
connectionResult.startResolutionForResult(getActivity(), RC_SIGN_IN);
mIsResolving = true;
} catch (IntentSender.SendIntentException e) {
mIsResolving = false;
mGoogleApiClient.connect();
}
} else {
// Could not resolve the connection result, show the user an
// error dialog.
showErrorDialog(connectionResult);
}
} else {
// Show the signed-out UI
updateUI(false);
}
}
private GoogleApiClient buildGoogleApiClient() {
return new GoogleApiClient.Builder(getActivity().getApplicationContext())
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(Plus.API)
.addScope(Plus.SCOPE_PLUS_PROFILE)
.addScope(Plus.SCOPE_PLUS_LOGIN)
.build();
}
/**
* This called only from Activity's onActivityResult() with requestCode == RC_SIGN_IN
*/
protected void connect(int resultCode) {
// If the error resolution was not successful we should not resolve further.
if (resultCode != Activity.RESULT_OK) {
mShouldResolve = false;
}
mIsResolving = false;
mGoogleApiClient.connect();
}
}
Run Code Online (Sandbox Code Playgroud)
并表明:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="somepackage" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<application
android:allowBackup="true"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</manifest>
Run Code Online (Sandbox Code Playgroud)
对不起有些编辑错误.
我找到了解决方案.它不是关于片段和SHA1键.这是关于包名.当我创建项目时,IDE使包目录像"somepackage.app",这不是我需要的,我改为"somepackage".一切都很好,除了谷歌api.Gradle在其配置文件中保存了"applicationId somepackage.app",我相信它会覆盖manifest的包名.没有错误,没有连接,没有.我花了大约15个小时,我相信我是愚蠢的.
| 归档时间: |
|
| 查看次数: |
3264 次 |
| 最近记录: |