GoogleApiClient 连接失败,ConnectionResult 状态码 SIGN_IN_REQUIRED
GoogleApiClient connection failed with ConnectionResult statuscode SIGN_IN_REQUIRED
[编辑] - 赏金
我正在尝试将我的 android 应用程序与 Google Drive 连接起来以同步 MP3 音频文件。需要工作 code/examples(除了下面给出的链接)以使用 GoogleDriveAndroidApi 和 Google[= 连接 Google Drive 65=]客户.
这个问题已经被问过很多次了,但是 none 中有一个问题的解决方案。所以,我再问一次。
我第一次在Google驱动器AndroidAPI上工作,无法通过"Choose account for 'APP NAME'"屏幕。这是我现在所做的:
- 尝试访问以下网址:
https://developers.google.com/drive/android/get-started
https://github.com/googledrive/android-quickstart
https://www.numetriclabz.com/integrate-google-drive-in-android-tutorial/
在我的 build.gradle 中使用编译 'com.google.android.gms:play-services-drive:10.0.1'(如果有任何区别)
到目前为止尝试过的代码:
public class ARCBackupActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
private static final int REQUEST_CODE = 1;
private GoogleApiClient mGoogleApiClient;
private Toolbar mToolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.arc_activity_backup);
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
protected void onResume() {
super.onResume();
if (mGoogleApiClient == null) {
// Create the API client and bind it to an instance variable.
// We use this instance as the callback for connection and connection
// failures.
// Since no account name is passed, the user is prompted to choose.
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Drive.API)
.addScope(Drive.SCOPE_FILE)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
// Connect the client.
mGoogleApiClient.connect();
}
@Override
protected void onPause() {
if (mGoogleApiClient != null) {
mGoogleApiClient.disconnect();
}
super.onPause();
}
@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
switch (requestCode) {
case REQUEST_CODE:
if (resultCode == Activity.RESULT_OK) {
mGoogleApiClient.connect();
}
break;
}
}
@Override
public void onConnectionFailed(ConnectionResult result) {
// Called whenever the API client fails to connect.
Log.i(Const.DEBUG, "GoogleApiClient connection failed: " + result.toString());
if (!result.hasResolution()) {
// show the localized error dialog.
GoogleApiAvailability.getInstance().getErrorDialog(this, result.getErrorCode(), 0).show();
return;
}
// The failure has a resolution. Resolve it.
// Called typically when the app is not yet authorized, and an
// authorization
// dialog is displayed to the user.
try {
result.startResolutionForResult(this, REQUEST_CODE);
} catch (IntentSender.SendIntentException e) {
Log.e(Const.DEBUG, "Exception while starting resolution activity", e);
}
}
@Override
public void onConnected(Bundle connectionHint) {
Log.d(Const.DEBUG, "API client connected.");
//saveFileToDrive();
}
@Override
public void onConnectionSuspended(int cause) {
Log.d(Const.DEBUG, "GoogleApiClient connection suspended");
}
}
我在这里缺少什么?非常感谢任何具有良好解释的完整示例。
如果我需要提供更多信息来解决问题,请告诉我
[编辑 1]
如果我在构建googleApi客户端时添加Plus.API
,它解决了卡在选择帐户循环中的问题。但是,Plus.API 已被弃用,仅通过使用 GoogleApiClient 是否有解决方法?
这是更改后的代码:
@Override
protected void onResume() {
super.onResume();
if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
mGoogleApiClient.disconnect();
}
if (mGoogleApiClient == null) {
// Create the API client and bind it to an instance variable.
// We use this instance as the callback for connection and connection
// failures.
// Since no account name is passed, the user is prompted to choose.
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Drive.API)
.addApi(Plus.API)
.addScope(Drive.SCOPE_FILE)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
// Connect the client.
mGoogleApiClient.connect();
}
仍在寻找正确的实施方式...
好吧,我正在 github 上解决 Google 驱动器的问题,我发现他们通过@iuribtt 回答
遇到了类似的问题
这里是link
如果这个 link 过期了,我将发布他的答案:
尝试使用"keytool -exportcert -alias androiddebugkey -keystore C:\Users\XXXXX.android\debug.keystore -list -v"
而不是您生成的密钥库,一旦您想要调试模式。
"debug.keystore" 的路径和密码是 "android"
https://support.google.com/cloud/answer/6158849?hl=en#android
然后在 https://developers.google.com/mobile/add
中创建项目
最后,在 https://console.developers.google.com
中启用驱动器 API
希望对您有所帮助!!!干杯!!!
看来你的问题是因为
@Override
protected void onPause() {
if (mGoogleApiClient != null) {
mGoogleApiClient.disconnect();
}
super.onPause();
}
让我解释一下。当帐户选择器出现时,它会让您的 activity 调用 onPause()。在 onPause() 你调用 mGoogleApiClient.disconnect().
因此,您在选择帐户后拥有新的 GoogleApiClient(当调用 onResume 和 onActivityResult 时)。
要解决此问题,只需将 mGoogleApiClient.connect() 和 mGoogleApiClient.disconnect() 放置到 onStart() /onStop() 方法 activity 或使用 enableAutoManage(Activity, OnConnectionFailedListener) 为您的 GoogleApiClient.
编码愉快! :)
[编辑] - 赏金
我正在尝试将我的 android 应用程序与 Google Drive 连接起来以同步 MP3 音频文件。需要工作 code/examples(除了下面给出的链接)以使用 GoogleDriveAndroidApi 和 Google[= 连接 Google Drive 65=]客户.
这个问题已经被问过很多次了,但是 none 中有一个问题的解决方案。所以,我再问一次。
我第一次在Google驱动器AndroidAPI上工作,无法通过"Choose account for 'APP NAME'"屏幕。这是我现在所做的:
- 尝试访问以下网址:
https://developers.google.com/drive/android/get-started
https://github.com/googledrive/android-quickstart
https://www.numetriclabz.com/integrate-google-drive-in-android-tutorial/
在我的 build.gradle 中使用编译 'com.google.android.gms:play-services-drive:10.0.1'(如果有任何区别)
到目前为止尝试过的代码:
public class ARCBackupActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener { private static final int REQUEST_CODE = 1; private GoogleApiClient mGoogleApiClient; private Toolbar mToolbar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.arc_activity_backup); mToolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(mToolbar); getSupportActionBar().setDisplayShowTitleEnabled(false); getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true); } @Override protected void onResume() { super.onResume(); if (mGoogleApiClient == null) { // Create the API client and bind it to an instance variable. // We use this instance as the callback for connection and connection // failures. // Since no account name is passed, the user is prompted to choose. mGoogleApiClient = new GoogleApiClient.Builder(this) .addApi(Drive.API) .addScope(Drive.SCOPE_FILE) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .build(); } // Connect the client. mGoogleApiClient.connect(); } @Override protected void onPause() { if (mGoogleApiClient != null) { mGoogleApiClient.disconnect(); } super.onPause(); } @Override protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) { switch (requestCode) { case REQUEST_CODE: if (resultCode == Activity.RESULT_OK) { mGoogleApiClient.connect(); } break; } } @Override public void onConnectionFailed(ConnectionResult result) { // Called whenever the API client fails to connect. Log.i(Const.DEBUG, "GoogleApiClient connection failed: " + result.toString()); if (!result.hasResolution()) { // show the localized error dialog. GoogleApiAvailability.getInstance().getErrorDialog(this, result.getErrorCode(), 0).show(); return; } // The failure has a resolution. Resolve it. // Called typically when the app is not yet authorized, and an // authorization // dialog is displayed to the user. try { result.startResolutionForResult(this, REQUEST_CODE); } catch (IntentSender.SendIntentException e) { Log.e(Const.DEBUG, "Exception while starting resolution activity", e); } } @Override public void onConnected(Bundle connectionHint) { Log.d(Const.DEBUG, "API client connected."); //saveFileToDrive(); } @Override public void onConnectionSuspended(int cause) { Log.d(Const.DEBUG, "GoogleApiClient connection suspended"); } }
我在这里缺少什么?非常感谢任何具有良好解释的完整示例。
如果我需要提供更多信息来解决问题,请告诉我
[编辑 1]
如果我在构建googleApi客户端时添加
Plus.API
,它解决了卡在选择帐户循环中的问题。但是,Plus.API 已被弃用,仅通过使用 GoogleApiClient 是否有解决方法?这是更改后的代码:
@Override protected void onResume() { super.onResume(); if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) { Plus.AccountApi.clearDefaultAccount(mGoogleApiClient); mGoogleApiClient.disconnect(); } if (mGoogleApiClient == null) { // Create the API client and bind it to an instance variable. // We use this instance as the callback for connection and connection // failures. // Since no account name is passed, the user is prompted to choose. mGoogleApiClient = new GoogleApiClient.Builder(this) .addApi(Drive.API) .addApi(Plus.API) .addScope(Drive.SCOPE_FILE) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .build(); } // Connect the client. mGoogleApiClient.connect(); }
仍在寻找正确的实施方式...
好吧,我正在 github 上解决 Google 驱动器的问题,我发现他们通过@iuribtt 回答
遇到了类似的问题这里是link
如果这个 link 过期了,我将发布他的答案:
尝试使用"keytool -exportcert -alias androiddebugkey -keystore C:\Users\XXXXX.android\debug.keystore -list -v" 而不是您生成的密钥库,一旦您想要调试模式。
"debug.keystore" 的路径和密码是 "android" https://support.google.com/cloud/answer/6158849?hl=en#android
然后在 https://developers.google.com/mobile/add
中创建项目最后,在 https://console.developers.google.com
中启用驱动器 API希望对您有所帮助!!!干杯!!!
看来你的问题是因为
@Override
protected void onPause() {
if (mGoogleApiClient != null) {
mGoogleApiClient.disconnect();
}
super.onPause();
}
让我解释一下。当帐户选择器出现时,它会让您的 activity 调用 onPause()。在 onPause() 你调用 mGoogleApiClient.disconnect().
因此,您在选择帐户后拥有新的 GoogleApiClient(当调用 onResume 和 onActivityResult 时)。
要解决此问题,只需将 mGoogleApiClient.connect() 和 mGoogleApiClient.disconnect() 放置到 onStart() /onStop() 方法 activity 或使用 enableAutoManage(Activity, OnConnectionFailedListener) 为您的 GoogleApiClient.
编码愉快! :)