我正在尝试将Google云端硬盘集成到我的应用程序中,但收效甚微.使用快速启动存储库中的代码,每次选择登录用户帐户时,都会收到以下错误消息:
I/DriveDemo: Googleapiclient connection Failed: ConnectionResult{statusCode=SIGN_IN_required,resolution=PendingIntent{867e070: android.os.BinderProxy@13cdfe9},message=null}
我按照以下网址的指示:
> https://developers.google.com/drive/android/get-started
> https://github.com/googledrive/android-quickstart
并按照视频:
> https://youtu.be/RezC1XP6jcs
我也看了很多Stack Overflow问题和答案,不知道我做错了什么.
manifest.xml文件:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.lightkeeper54.chuck.drivedemo"> <uses-permission android:name="android.permission.GET_ACCOUNTS" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
码:
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. Once connected,the camera is launched. 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_CAPTURE_IMAGE: // Called after a photo has been taken. if (resultCode == Activity.RESULT_OK) { // Store the image data as a bitmap for writing later. mBitmapToSave = (Bitmap) data.getExtras().get("data"); } break; case REQUEST_CODE_CREATOR: // Called after a file is saved to Drive. if (resultCode == RESULT_OK) { Log.i(TAG,"Image successfully saved."); mBitmapToSave = null; // Just start the camera again for another photo. startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE),REQUEST_CODE_CAPTURE_IMAGE); } break; } } @Override public void onConnectionFailed(ConnectionResult result) { // Called whenever the API client fails to connect. Log.i(TAG,"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_RESOLUTION); } catch (IntentSender.SendIntentException e) { Log.e(TAG,"Exception while starting resolution activity",e); } } @Override public void onConnected(Bundle connectionHint) { Log.i(TAG,"API client connected."); if (mBitmapToSave == null) { // This activity has no UI of its own. Just start the camera. startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE),REQUEST_CODE_CAPTURE_IMAGE); return; } saveFiletoDrive(); } @Override public void onConnectionSuspended(int cause) { Log.i(TAG,"Googleapiclient connection suspended"); }
解决方法
基于文档 –
SIGN_IN_REQUIRED
The client attempted to connect to the service but the user is not signed in. The client may choose to continue without using the API. Alternately,if 07001 returns true the client may call 07002 to prompt the user to sign in. After the sign in activity returns with 07003 further attempts should succeed.
您也可以查看@ Cheok的问题,SO post,建议按要求正确添加SHA-1和包名称,并在项目凭据全部设置后启用API.
希望这些信息有所帮助