簡體   English   中英

Android Drive Api收到400錯誤請求

[英]Android Drive Api getting 400 Bad Request

我努力工作很長時間,試圖將Google Drive集成到android應用程序中。 但這是一個陷阱-糟糕的是,谷歌一如既往的驚訝。 一切始於我依賴於Google的指示( 第一第一 )。 我滿足了所有條件-創建了OAuth 2.0,創建了應用程序密鑰,應用程序注冊表,導入的庫...通常完成了他們要求的所有操作,並進行了100次復查。 但是,按預期的方式行不通-在第一種情況下,所有示例均顯示為“ 403禁止訪問未配置” ,第二項無法實現,因為Google尚未發布密鑰。 在整個星期中,我一直在尋找解決此問題的替代方法-大量的信息檢查,大量的努力和所花費的時間-但結果是0。希望的光芒來自一般問題ArtOfWarfare 但是他提出的代碼(據稱他可以工作)無法工作-我允許應用程序使用該帳戶,然后由於“與Google的通信錯誤”而導致連接失敗,反復嘗試會產生相同的結果。 結果,我畫了“他們的”代碼只是為了嘗試與驅動器進行通信。 由於我在這里寫-因此不起作用。 下面顯示的代碼返回“ 400 Bad Request Invalid Upload Request”,“ 。您對如何解決授權問題有任何想法,或者還有其他成功登錄的方法嗎?我將很高興進行建設性對話。

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.android.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.google.api.client.http.FileContent;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.services.drive.Drive;

public class ActivityStart extends Activity {

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_activity_start);
    String AUTH_TOKEN_TYPE = "oauth2:https://www.googleapis.com/auth/drive";
    AccountManager accountManager = AccountManager.get(this);
    accountManager.invalidateAuthToken(accountManager.getAccounts()[0].type, null);
    accountManager.getAuthToken( accountManager.getAccountsByType("com.google")[0], AUTH_TOKEN_TYPE, null, this,
            new AccountManagerCallback<Bundle>() {

                public void run(final AccountManagerFuture<Bundle> future) {
                    try {
                        String authToken = future.getResult().getString(
                                AccountManager.KEY_AUTHTOKEN);

                            final HttpTransport transport = AndroidHttp.newCompatibleTransport();
                            final JsonFactory jsonFactory = new GsonFactory();
                            GoogleCredential credential = new GoogleCredential();
                            credential.setAccessToken(authToken);
                            final Drive drive = new Drive.Builder(transport, jsonFactory, credential)
                                    .setApplicationName("HIV")
                                    .setGoogleClientRequestInitializer(new GoogleKeyInitializer("*****yDaPx1EtTHpMMMULzYlxjc3xdwsf******"))
                                    .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("text/plain");

                                    java.io.File fileContent = new java.io.File("data/data/com.example.hiv/document.txt");
                                    FileContent mediaContent = new FileContent("text/plain", fileContent);

                                    com.google.api.services.drive.model.File file = drive.files().insert(body, mediaContent).execute();

                                  } catch (UserRecoverableAuthIOException e) {
                                    e.printStackTrace();
                                  } catch (IOException e) {
                                    e.printStackTrace();
                                  }
                                }
                              });
                              t.start(); 

                    } catch (OperationCanceledException exception) {
                        // TODO
                    } catch (Exception exception) {
                        Log.d(this.getClass().getName(), exception.getMessage());
                    }
                }
            }, null);       
  } 
}

解決 :我正在嘗試發送大小為0 kB的文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM