2016-12-30 75 views
1

[编辑] - 赏金GoogleApiClient连接的StatusCode SIGN_IN_REQUIRED

我试图连接我与谷歌云端硬盘Android应用程序同步,MP3音频文件。需要使用GoogleDriveAndroidApi和GoogleApiClient连接Google云端硬盘的工作代码/示例(除了下面的链接)。


这个问题已被问了许多次,但他们都没有解决这个问题。所以,我再次问这个。

我第一次使用Google Drive Android API,无法通过“为APP名称选择帐户”屏幕。以下是我做截至目前:

  1. 试图按照以下网址:

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'c​​om.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(); 
        } 
    
  • 还在寻找实现正确的方式...

    +0

    “由于没有通过帐户名称,用户会被提示选择” - 是否会发生?如果你获得了这个屏幕,那么它就可以工作。你的错误是,你需要登录到有效的帐户 –

    +0

    @ cricket_007,是的,我得到的选择帐户屏幕。一旦我选择了该帐户,同一个屏幕就会一次又一次地显示为一个循环。 –

    +0

    @ cricket_007,你可以检查我编辑的代码,并让我知道什么是正确实施GoogleApiClient的最佳方式? –

    回答

    3

    它看起来像你的问题是因为

    @Override 
    protected void onPause() { 
        if (mGoogleApiClient != null) { 
         mGoogleApiClient.disconnect(); 
        } 
        super.onPause(); 
    } 
    

    让我解释一下。当帐户选择器出现时,它使您的活动调用onPause()。在onPause()中,你可以调用mGoogleApiClient.disconnect()

    因此,您在帐户选择后(当调用onResume和onActivityResult时)有新的GoogleApiClient。

    要解决该问题只需将mGoogleApiClient.connect()mGoogleApiClient.disconnect()到在onStart()/的onStop()的活动的方法或使用enableAutoManage(活动,OnConnectionFailedListener)你GoogleApiClient 。

    快乐编码! :)

    +0

    将代码移动到onStart()和onStop()解决了问题。 –

    0

    你的GET_ACCOUNTS添加权限?像这样

    <uses-permission android:name="android.permission.GET_ACCOUNTS" /> 
    
    1

    嗯,我经历了谷歌推动的问题在github上,我发现类似的问题,他们的经历@iuribtt答案

    这里是链接

    Google Drive Issues

    在这个链接已过期的情况下我发布他的回答:

    尝试使用“keytool -exportcert -alias androiddebugkey -keysto在C:\ Users \ XXXXX.android \ debug.keystore -list -v“ 而不是您生成的密钥库之后,您需要调试模式。

    为“debug.keystore”和密码的路径是“机器人” https://support.google.com/cloud/answer/6158849?hl=en#android

    而在这之后创建https://developers.google.com/mobile/add

    项目最后,能够在https://console.developers.google.com

    驱动器API

    希望这有助于!!!干杯!

    +0

    我想你必须在控制台中设置生产和调试密钥。 – danny117

    相关问题