Android Drive Api获得400 Bad Request

时间:2013-03-21 13:16:46

标签: java android google-drive-api google-oauth

我努力尝试在Android应用程序中集成G​​oogle Drive。但这是一个陷阱 - 谷歌总是感到惊讶 - 从某种意义上说。这一切都始于我依赖Google的说明(firstfirst)。我完成了所有条件 - 创建了OAuth 2.0,为应用程序,应用程序注册,导入库创建了一个密钥...通常会做他们要求的所有内容并重新检查100次。但正如预期的那样无效 - 在第一种情况下做了所有的例子,我收到了“403 Forbidden Access Not Configured”,第二种无法实现,因为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);       
  } 
}

1 个答案:

答案 0 :(得分:0)

已解决:我正在尝试发送大小为0 kB的文件。

相关问题