我是一名新手安卓开发者。我正在将Google+登录集成到我的应用中,但我对于Google的登录提示反复出现感到有些困惑。我不知道为什么,是我的问题吗?
我正在遵循Google开发者网站上的指导来完成这个任务。我已经使用了多个Google账户测试过我的应用,其中两个账户正常工作,但其他账户没有。
我是一名新手安卓开发者。我正在将Google+登录集成到我的应用中,但我对于Google的登录提示反复出现感到有些困惑。我不知道为什么,是我的问题吗?
我正在遵循Google开发者网站上的指导来完成这个任务。我已经使用了多个Google账户测试过我的应用,其中两个账户正常工作,但其他账户没有。
你尝试过了吗:
如何调试Google+集成?
通过启用日志记录,您可以在使用Google API时诊断网络问题。
要启用日志记录,请运行以下命令:
adb shell setprop log.tag.GooglePlusPlatform VERBOSE
adb shell setprop log.tag.GooglePlusPlatform ""
您需要添加的权限:
访问 Google+ API:
<uses-permission android:name="android.permission.INTERNET" />
为了在登录过程中检索账户名(电子邮件):
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
用于检索OAuth 2.0令牌或使令牌失效以断开用户连接。此断开选项是为了遵守Google+登录开发者政策而必需的:
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
看看是否有帮助。
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
protected void onStop() {
super.onStop();
if (mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
}
onCreate(Bundle bundle){
mGoogleApiClient = new GoogleApiClient.Builder(YourActivity.this)
.addConnectionCallbacks(YourActivity.this)
.addOnConnectionFailedListener(YourActivity.this).addApi(Plus.API)
.addScope(Plus.SCOPE_PLUS_LOGIN).build();
}
btn_gpluslogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
signInWithGplus();
}
});
private void signInWithGplus() {
if (!mGoogleApiClient.isConnecting()) {
mSignInClicked = true;
resolveSignInError();
}
}
private void resolveSignInError() {
if (mConnectionResult.hasResolution()) {
try {
mIntentInProgress = true;
mConnectionResult.startResolutionForResult(this, RC_SIGN_IN);
} catch (IntentSender.SendIntentException e) {
mIntentInProgress = false;
mGoogleApiClient.connect();
}
}
}
@Override
public void onConnectionFailed(ConnectionResult result) {
if (!result.hasResolution()) {
GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), this,
0).show();
return;
}
if (!mIntentInProgress) {
// Store the ConnectionResult for later usage
mConnectionResult = result;
if (mSignInClicked) {
// The user has already clicked 'sign-in' so we attempt to
// resolve all
// errors until the user is signed in, or they cancel.
resolveSignInError();
}
}
}
@Override
public void onConnected(Bundle arg0) {
mSignInClicked = false;
Toast.makeText(this, "User is connected!", Toast.LENGTH_LONG).show();
// Get user's information
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
boolean isSignedUp = pref.getBoolean("isSignedUp", false);
if (!isSignedUp)
getGProfileInformation();
// Update the UI after signin
}
private void updateUI(boolean isSignedIn) {
if (isSignedIn) {
getGProfileInformation();
} else {
btn_gpluslogin.setVisibility(View.VISIBLE);
}
}
/**
* Fetching user's information name, email, profile pic
*/
private void getGProfileInformation() {
try {
if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
Person currentPerson = Plus.PeopleApi
.getCurrentPerson(mGoogleApiClient);
Log.d("Google Info", currentPerson.toString());
String personName = currentPerson.getDisplayName();
String email = Plus.AccountApi.getAccountName(mGoogleApiClient);
} else {
Toast.makeText(getApplicationContext(),
"Person information is null", Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
private boolean mIsResolving = false;
// ...
public void onConnectionFailed(ConnectionResult connectionResult) {
if (connectionResult.hasResolution() && !mIsResolving) {
mIsResolving = true;
connectionResult.startResolutionForResult(this, RC_SIGN_IN);
}
}
然后在onActivityResult
中执行以下操作:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_SIGN_IN) {
if (resultCode == RESULT_OK) {
mIsResolving = false;
mGoogleApitClient.connect();
} else {
// ...
}
}
}
由于您没有提供任何代码,让我来猜测一下。如果您说您在某些帐户上遇到了问题而在其他帐户上没有问题,那么我认为这些有问题的帐户还没有授权您的应用程序。请检查以下配置(在浏览器中登录后):
https://security.google.com/settings/security/permissions?pli=1
还是这个?
https://plus.google.com/u/0/apps
在列表中查找您的应用程序。也许您的权限被拒绝了或者其他原因。也许您只需要再次授权、重置并重试。