【发布时间】:2016-12-30 15:04:29
【问题描述】:
[编辑] - 赏金
我正在尝试将我的 Android 应用程序与 Google Drive 连接以同步 MP3 音频文件。需要使用 GoogleDriveAndroidApi 和 GoogleApiClient 连接 Google Drive 的工作代码/示例(除了下面给出的链接)。
这个问题已经被问过很多次了,但没有一个人能解决这个问题。所以,我再次问这个问题。
我是第一次使用 Google Drive Android API,但无法通过“为 '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 中使用 compile '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]
如果我在构建 googleApiClient 时添加
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(); }
还在寻找正确的实现方式……
【问题讨论】:
-
“由于没有传递帐户名称,因此提示用户选择” - 会发生这种情况吗?如果你得到那个屏幕,那么它就可以工作。您的错误是您需要登录有效帐户
-
@cricket_007,是的,我得到了选择帐户屏幕。一旦我选择了帐户,相同的屏幕就会一次又一次地循环显示。
-
@cricket_007,你能检查我编辑的代码,让我知道正确实现 GoogleApiClient 的最佳方法是什么?
-
我自己从未做过,但如果不推荐使用 plus api,我会阅读有关替代方案的文档
-
试试这个:从您的 gmail 帐户转到 google api 控制台。在这里,您将在 Google drive api 前面看到一个禁用按钮。就在你会看到一个齿轮或设置按钮点击它并生成oAuth令牌。
标签: android google-drive-android-api