[编辑] - 赏金GoogleApiClient连接的StatusCode SIGN_IN_REQUIRED
我试图连接我与谷歌云端硬盘Android应用程序同步,MP3音频文件。需要使用GoogleDriveAndroidApi和GoogleApiClient连接Google云端硬盘的工作代码/示例(除了下面的链接)。
这个问题已被问了许多次,但他们都没有解决这个问题。所以,我再次问这个。
我第一次使用Google Drive Android API,无法通过“为APP名称选择帐户”屏幕。以下是我做截至目前:
- 试图按照以下网址:
https://developers.google.com/drive/android/get-started
https://github.com/googledrive/android-quickstart
https://www.numetriclabz.com/integrate-google-drive-in-android-tutorial/
使用compile'com.google.android.gms:play-services-drive: 10.0.1' 在我的build.gradle(如果有什么差别)
代码试图至今:
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]
如果我添加
Plus.API
在构建googleApiClient,它解决了被陷在一个循环中的问题选择帐户。但是,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(); }
还在寻找实现正确的方式...
“由于没有通过帐户名称,用户会被提示选择” - 是否会发生?如果你获得了这个屏幕,那么它就可以工作。你的错误是,你需要登录到有效的帐户 –
@ cricket_007,是的,我得到的选择帐户屏幕。一旦我选择了该帐户,同一个屏幕就会一次又一次地显示为一个循环。 –
@ cricket_007,你可以检查我编辑的代码,并让我知道什么是正确实施GoogleApiClient的最佳方式? –