4

我想我的应用程序连接到谷歌播放服务来访问谷歌驱动器,但它显示我连接用的StatusCode SIGN_IN_REQUIRED.GoogleApiClient onConnectionFailed用的StatusCode = SIGN_IN_REQUIRED

失败但似乎会那样,短短3天前。我也检查了谷歌的开发者控制台。他们可能已经改变了我无法弄清的API。您的帮助将受到欢迎。

我的代码:

/** 
* Create a new file and save it to Drive. 
*/ 
    private void saveFileToDrive(final byte[] data) { 
    // Start by creating a new contents, and setting a callback. 
    Log.i(TAG, "Creating new contents."); 
    //final Bitmap image = mBitmapToSave; 
    Drive.DriveApi.newDriveContents(mGoogleApiClient) 
      .setResultCallback(new ResultCallback<DriveApi.DriveContentsResult>() { 

       @Override 
       public void onResult(DriveApi.DriveContentsResult result) { 
        // If the operation was not successful, we cannot do anything 
        // and must 
        // fail. 
        if (!result.getStatus().isSuccess()) { 
         Log.i(TAG, "Failed to create new contents."); 
         return; 
        } 
        // Otherwise, we can write our data to the new contents. 
        Log.i(TAG, "New contents created."); 
        // Get an output stream for the contents. 
        OutputStream outputStream = result.getDriveContents().getOutputStream(); 
        // Write the bitmap data from it. 
        /* ByteArrayOutputStream bitmapStream = new ByteArrayOutputStream(); 
        image.compress(Bitmap.CompressFormat.PNG, 100, bitmapStream);*/ 
        try { 
         //outputStream.write(bitmapStream.toByteArray()); 
         outputStream.write(data); 
        } catch (IOException e1) { 
         Log.i(TAG, "Unable to write file contents."); 
        } 
        // Create the initial metadata - MIME type and title. 
        // Note that the user will be able to change the title later. 
        MetadataChangeSet metadataChangeSet = new MetadataChangeSet.Builder() 
          .setMimeType("application/pdf").setTitle(filename).build(); 
        // Create an intent for the file chooser, and start it. 
        IntentSender intentSender = Drive.DriveApi 
          .newCreateFileActivityBuilder() 
          .setInitialMetadata(metadataChangeSet) 
          .setInitialDriveContents(result.getDriveContents()) 
          .build(mGoogleApiClient); 
        try { 
         startIntentSenderForResult(
           intentSender, REQUEST_CODE_CREATOR, null, 0, 0, 0); 
        } catch (IntentSender.SendIntentException e) { 
         Log.i(TAG, "Failed to launch file chooser."); 
        } 
       } 
      }); 
} 

@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. Once connected, the camera is launched. 
    mGoogleApiClient.connect(); 

} 



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


@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    switch (requestCode) { 
     case REQUEST_CODE_CREATOR: 
      // Called after a file is saved to Drive. 
      if (resultCode == RESULT_OK) { 
       Log.i(TAG, "Image successfully saved."); 

       Intent intent = new Intent(); 
       intent.putExtra("FILE_PATH",file_path); 
       PreviewActivity.this.setResult(RESULT_OK, intent); 
       finish(); 
       progressDialog.hide(); 

      } 
      break; 
    } 
} 

@Override 
public void onConnected(Bundle bundle) { 

    Log.i(TAG, "API client connected."); 
    //saveFileToDrive(); 
} 

@Override 
public void onConnectionSuspended(int i) { 
    Log.i(TAG, "GoogleApiClient connection suspended"); 
} 

@Override 
public void onConnectionFailed(ConnectionResult connectionResult) { 
    // Called whenever the API client fails to connect. 
    Log.i(TAG, "GoogleApiClient connection failed: " + connectionResult.toString()); 
    if (!connectionResult.hasResolution()) { 
     // show the localized error dialog. 
     GoogleApiAvailability.getInstance().getErrorDialog(this, connectionResult.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 { 
     connectionResult.startResolutionForResult(this, REQUEST_CODE_RESOLUTION); 
    } catch (IntentSender.SendIntentException e) { 
     Log.e(TAG, "Exception while starting resolution activity", e); 
    } 
} 

/END OF CODE/

摇篮:

apply plugin: 'com.android.application' 

android { 


dexOptions { 
    incremental true 
    javaMaxHeapSize "2048M" 

} 

compileSdkVersion 23 
buildToolsVersion '23.0.2' 
defaultConfig { 
    multiDexEnabled true 
    applicationId "com.woundcentrics.abxsteward" 
    minSdkVersion 14 
    targetSdkVersion 21 
    versionCode 1 
    versionName "0.4 (Beta)" 

} 
buildTypes { 
    release { 
     minifyEnabled false 
     proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
    } 
} 
packagingOptions { 
    exclude 'META-INF/LICENSE.txt' 
    exclude 'META-INF/LICENSE' 
    exclude 'META-INF/NOTICE.txt' 
    exclude 'META-INF/NOTICE' 
    exclude 'META-INF/DEPENDENCIES' 
} 
productFlavors { 
} 
} 

dependencies { 
compile fileTree(dir: 'libs', include: ['*.jar']) 
testCompile 'org.robolectric:robolectric:3.0' 
compile project(':openCVLibrary310') 
compile files('libs/itextpdf-5.1.0.jar') 
compile project(':scanlibrary') 
apply plugin: 'com.google.gms.google-services' 
compile 'me.dm7.barcodescanner:zbar:1.8.3' 
compile 'com.mcxiaoke.volley:library-aar:1.0.0' 
compile 'com.google.code.gson:gson:2.4' 
compile 'com.android.support:appcompat-v7:23.0.0' 
compile 'com.google.android.gms:play-services:8.3.0' 
compile 'com.google.android.gms:play-services-ads:8.3.0' 
compile 'com.google.android.gms:play-services-identity:8.3.0' 
compile 'com.google.android.gms:play-services-gcm:8.3.0' 

} 
repositories { 
jcenter() 

} 

logcat的消息:

GoogleApiClient connection failed: ConnectionResult{statusCode=SIGN_IN_REQUIRED, resolution=PendingIntent{21d08740: [email protected]}, message=null} 

回答

4

嗯,我得到了在logcat中显示的消息:

GoogleApiClient onConnectionFailed用的StatusCode = SIGN_IN_REQUIRED

登录所需的手段,我们需要启用谷歌云端硬盘API https://console.developers.google.com

enter image description here

下载client_id.json文件,并添加到我们的项目的/app文件夹:

enter image description here

然后我们就可以没有问题访问,让我们期待API client connected.

enter image description here

为了更好地利用的Google Drive API,建议使用此权限:

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.GET_ACCOUNTS" /> 

你可以在这里找到一个完整的例子:

https://github.com/Jorgesys/Android-Google-Drive

enter image description here

+0

为什么这个'client_id.json'文件甚至在构建期间打包在哪里? – mDroidd

+0

感谢这就是我一直在寻找 –

0

Here是您收到错误的信息。我假设你是,但你需要登录才能访问云端硬盘。当我运行示例应用程序时,它最初要求我选择一个帐户。也许你没有与你正在使用的设备同步的帐户?有一个“添加账户”的选项,但是当你拥有零账户时,行为可能会有所不同。文档建议您或者不使用API​​(仅仅因为您不能登录才能继续)或者调用startResolutionForResult(Activity,int)来提示用户登录,但只需添加account to your device就可以了。

+1

感谢您的快速回复。其实它早些时候正在工作。但它似乎就像3天前那样。我也检查了谷歌的开发者控制台。他们可能已经改变了我无法弄清的API。您的帮助将受到欢迎。再次感谢。 –

+0

我只想添加一些指示,以跟进@克里希纳的答案。您只需将处理程序添加到Google Drive API的连接和授权中即可。这可以通过'GoogleApiClient'完成。这个可以在OnCreate()方法中找到。您可以尝试阅读[连接授权]的实施指南(https://developers.google.com/drive/android/auth#connecting_and_authorizing_the_google_drive_android_api)以获得澄清。这可能会解决您的问题,因为GoogleApiClient连接失败。我希望这有帮助。 :) –

+0

@AbdulYasin Iam面临同样的问题。我的应用程序工作正常,直到今天,但现在IAM无法登录。选择帐户继续询问,它是返回谷歌API客户端失败结果代码4.我没有改变任何东西。感谢提前:) –

0

此错误表明该帐户被锁定,可能出于安全原因。您应该从桌面浏览器访问drive.google.com,然后登录该帐户,然后尝试通过Android应用程序以编程方式访问该帐户。完成桌面浏览器登录后,您应该可以从Android访问该帐户。

+0

帐户在移动设备和网络浏览器上都正常工作。 –

0

这将在创建您的Google API项目后生效,您已按问题添加了SHA-1和包名称。 另外,请记住,您的包不得与Google中的其他项目发生冲突。 希望这个作品。 注册您的项目中:

Click here to add enable your Google api

,并启用API一旦项目证书都是集。 如果你正在寻找上载或下载你的文件,那么你必须在你的清单文件使用权限:

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.GET_ACCOUNTS" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 

-Surajit。 快乐编码。

0

当我看到大多数的答案以上是正确的,还是如果你能不能够解决问题。这里有事情,请与您的开发环境

  1. 检查您是否已经两个不同的系统(PC /笔记本电脑)之间切换,请产生另一SHA1密钥每个系统和更新谷歌开发者控制台

  2. 如果您不是两个系统之间的切换还是你所面对的问题,那么,请确保您清单应用程序ID包名称匹配你在谷歌开发者CONSOL给Ë

  3. 不要忘记使驱动器API在谷歌开发者控制台

相关问题