gpt4 book ai didi

java - Android 的 Google+ 登录无法正常工作,错误代码 4

转载 作者:行者123 更新时间:2023-11-29 07:47:42 25 4
gpt4 key购买 nike

我已经关注了 guide关于如何设置 google+ 登录。我完成了每一步,基本上复制并粘贴了代码。

这是场景。我在两台不同的计算机上开发。我的控制台中有两个不同的客户端 ID。一台用于计算机 A,一台用于计算机 B。

当我安装应用程序并启动它时,它会尝试登录并失败,并显示来自 logcat 的以下错误。如果我退出应用程序并重新启动,它将正常登录。当它失败时,它似乎会尝试启动一个 ActivityActivity 永远不会启动。这是 logcat。

06-04 10:14:57.801  19948-19948/carbon.android.game.legions D/AccountFragment﹕ ResolveSignInError ErrorCode:4
06-04 10:14:57.801 602-823/? I/ActivityManager﹕ START u0 {cmp=com.google.android.gms/.plus.activity.AccountSignUpActivity (has extras)} from pid -1
06-04 10:14:57.811 178-646/? D/audio_hw_primary﹕ select_devices: out_snd_device(2: speaker) in_snd_device(0: )
06-04 10:14:57.811 178-646/? D/ACDB-LOADER﹕ ACDB -> send_afe_cal
06-04 10:14:57.821 602-2816/? I/ActivityManager﹕ START u0 {act=com.google.android.gms.common.account.CHOOSE_ACCOUNT pkg=com.google.android.gms cmp=com.google.android.gms/.common.account.AccountPickerActivity (has extras)} from pid 20027
06-04 10:14:57.941 20027-20031/? D/dalvikvm﹕ GC_CONCURRENT freed 601K, 7% free 9304K/9940K, paused 2ms+2ms, total 19ms
06-04 10:14:58.071 949-959/? W/GLSUser﹕ GoogleAccountDataService.getToken()

我做错了什么?我逐字逐句地遵循了指南,基本上复制并粘贴了代码。唯一的区别是我在 Fragment 而不是 Activity 中。但是,这应该无关紧要。

代码如下:

public class AccountFragment extends Fragment implements View.OnClickListener,
ConnectionCallbacks,
OnConnectionFailedListener {

private static final int RC_SIGN_IN = 1524;

private GoogleApiClient googleApiClient;
private boolean intentInProgress;
private boolean signInClicked;
private ConnectionResult connectionResult;

private SignInButton signInButton;

public AccountFragment() {}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.d(TAG, "The connection failed: " + connectionResult.getErrorCode());

if (!this.intentInProgress) {
this.connectionResult = connectionResult;

if (this.signInClicked) {
this.resolveSignInError();
}
}
}

@Override
public void onStart() {
super.onStart();

this.googleApiClient.connect();
}

@Override
public void onStop() {
super.onStop();

if (this.googleApiClient.isConnected()) {
this.googleApiClient.disconnect();
}
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

this.googleApiClient = new GoogleApiClient.Builder(this.getActivity())
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(Plus.API)
.addScope(Plus.SCOPE_PLUS_PROFILE)
.build();
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.sign_in_button:
if (!this.googleApiClient.isConnecting() && !this.googleApiClient.isConnected()) {
this.signInClicked = true;
this.resolveSignInError();
} else {
Log.d(TAG, "OnClick else");
}
break;
default:
break;
}
}

@Override
public void onConnected(Bundle bundle) {
this.signInClicked = false;
Person currentPerson = Plus.PeopleApi.getCurrentPerson(this.googleApiClient);
Log.d(TAG, "User connected: " + currentPerson.getDisplayName());
Log.d(TAG, "User id: " + currentPerson.getId());
Toast.makeText(this.getActivity(), "User connected: " + currentPerson.getDisplayName(), Toast.LENGTH_SHORT).show();
}

@Override
public void onConnectionSuspended(int i) {
this.googleApiClient.connect();
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RC_SIGN_IN) {

if (resultCode != Activity.RESULT_OK) {
this.signInClicked = false;
}

this.intentInProgress = false;

if (!this.googleApiClient.isConnecting()) {
this.googleApiClient.connect();
}
}
}

private void resolveSignInError() {
if (GooglePlayServicesUtil.isGooglePlayServicesAvailable(this.getActivity()) != ConnectionResult.SUCCESS) {
Log.e(TAG, "Google Play Services is not available.");
}

Log.d(TAG, "ResolveSignInError ErrorCode:" + this.connectionResult.getErrorCode());

if (this.connectionResult.hasResolution()) {
this.intentInProgress = true;
try {
this.connectionResult.startResolutionForResult(this.getActivity(), RC_SIGN_IN);
} catch (SendIntentException e) {
e.printStackTrace();
this.intentInProgress = false;
this.googleApiClient.connect();
}
}
}
}

最佳答案

我想出了我的问题。

有几个问题。

  1. 在我所有的测试中,我一直在尝试登录。有时是成功的。不知何故,即使我的应用程序未登录,我仍保持身份验证状态。我添加了退出和 revoke access 的功能。 .在revoking access之后并尝试再次登录,Activity 将启动以解决所有错误。
  2. 我也遇到了正在讨论的问题 here .我的主机 Activity 正在使用我的 connectionResult.startResolutionForResult(...); 中的 onActivityResult(...)。因此,经过反复试验,我终于找到了解决该问题的方法。链接的问题有所帮助,但并未完全解决问题。请查看my answer我是如何解决这个问题的。

经验教训,确保您在测试时注销并撤销访问权限。如果您遇到这些问题,请尝试 revoking access然后重新登录。

关于java - Android 的 Google+ 登录无法正常工作,错误代码 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24041462/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com