java - Android Drive Api getting 400 Bad Request -
I worked long and hard to integrate Google Drive into the Android application. But this is a trap - Google is always surprised - a bad way It all started with the fact that I trusted the instructions (,) from Google, I completed all the circumstances - created OAuth 2.0, created a key for the application, app reg., Imported libraries ... usually they were Asked anything and 100 times tested again. But as the expected thing does not work - all the examples were created in the first case, I received a "403 prohibited login not configured" , the other could not be applied, because Google has The secret key is not released even throughout the week, I was looking for alternate solutions to this problem - spend many exams, lots of effort and time of information - but the result is 0. The ray of hope came through general questions. But the code which he proposed and which allegedly works, I do not work - I allow the application to use the account, after which its connection fails due to "communication error with Google" Repeated attempts result in the same result. As a result, I drew "my" code to try to communicate with the drive. Since I am writing here - so it does not work. Returns the code shown below to "400 Bad Request Invalid Upload Request", . Do you have an idea about solving this problem with authorization, or other ways of successful login? Is pleased that creative dialogue resolution : I am trying to send I am a file whose size is 0 KB.
package com.example.hiv. Import java.io.IOException; import android.accounts.AccountManager; import android.accounts.AccountManagerCallback; import android.accounts .AccountManagerFuture; Import android.accounts.OperationCanceledException; Import android.app.Activity; Import android.os.bundle; Import android.util.log; Import com.google.api.client.extensions.andro Id.http.AndroidHttp; Import com.google.api .client.googleapis.auth.oauth2.GoogleCredential; Import com.google.api.client.googleapis.extensions.android.gms.auth.UserRecoverableAuthIOException; Import com.google.api .client.googleapis.services.GoogleKeyInitializer; Import Com.apple.client.http.FileContent; Com.google.api.client.json.gson.GsonFactory; Import com.google.api.services.drive.Drive; Public class activity increases traffic activity (crude on public work) (bundled saved instenstate) {super.onCreate (savedInstanceState); SetContentView (R.layout.layout_activity_start); String AUTH_TOKEN_TYPE = "oauth2: https: //www.googleapis.com/auth/drive"; Account Manager Account Manager = Account Manager. Gate (this); AccountManager.invalidateAuthToken (accountManager.getAccounts () [0]. Type, faucet); accountManager.getAuthToken (accountManager.getAccountsByType ( "com.google") [0], AUTH_TOKEN_TYPE, null, this new AccountManagerCallback & LT; bundle & gt; () {public void run (last AccountManagerFuture & LT; bundle & gt; future } {Try {string authToken = future.getResult () GetString (AccountManager.KEY_AUTHTOKEN); Last HttpTransport Transportation = AndroidHttp.newCompatibleTransport (); Last JsonFactory jsonFactory = New GsonFactory (); GoogleCredential Credentials = New GoogleCredential (); credential.setAccessToken (AuthToken); Last Drive Drive = New Drive.Builder (Transport, jsonFactory, Credential) .setApplicationName ("HIV") .setGoogleClientRequestInitializer (New GoogleKeyInitializer ("***** yDaPx1EtTHpMMMULzYlxjc 3xdwsf ****** ")) .build (); Thread t = new Thread (new Runnable () {@Override public void run () {try {final com.google.api.services.drive.model.File body = new com.google.api.services.drive.model.File (); body .setTitle ( "my test file"); body.setDescription ( "a test file"); Body.setMimeType ("tex t / plain"); Java.io.file fileContent = new java.io.File ("data / data / com.example.hiv / document.txt"); FileContent Media Content = New File Resource ("Text / Plain", File Content); Com.google.api.services.drive.model.File file = drive.files (). Insert (body, media content). Execute (); } Hold (UserRecoverableAuthIOException e) {e.printStackTrace (); } Hold (IOException e) {e.printStackTrace (); }}}); T.start (); } Catch (OperationCanceledException exception) {// TODO} catch (Exception exception) {Log.d (this.getClass () getName (), exception.getMessage ().); } } }, Zero); }}
Comments
Post a Comment