【问题标题】:How to initialize a Drive object in Android to perform requests to Google Drive? [duplicate]如何在 Android 中初始化 Drive 对象以执行对 Google Drive 的请求? [复制]
【发布时间】:2012-09-19 03:23:07
【问题描述】:

可能重复:
Android Open and Save files to/from Google Drive SDK

我正在尝试在我的 Android 应用程序中从 Google Drive 上传和下载文件。

我能够使用 AccountManager.getAuthToken() 为用户的 Google 帐户获取授权令牌字符串。

现在,我不知道如何使用该令牌来初始化 Drive 对象以开始向 Google Drive 发送请求。我使用了 Google Drive 稀缺文档中的示例中的以下代码,但它不起作用。显然,调用成功了,但是当我尝试上传文件时,它只是挂断,直到 Android 告诉我应用程序没有响应。

GoogleCredential credential = new GoogleCredential().setAccessToken(authToken);
HttpTransport httpTransport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
Drive drive = null;

drive = Drive.builder(httpTransport, jsonFactory)
    .setApplicationName(APP_NAME)
    .setHttpRequestInitializer(credential)
    .setJsonHttpRequestInitializer(new GoogleKeyInitializer(API_KEY))
    .build();

要上传某个文件,我执行以下操作:

try
{
    // local file
    java.io.File fileLocal = new java.io.File(FILE_NAME);

    // remote file

    File remoteFile = new File();
    remoteFile.setTitle(fileLocal.getName());
    remoteFile.setDescription("Text File");
    remoteFile.setMimeType("text/plain");

    // load the content of the local file into the remote content
    FileContent remoteFileContent = new FileContent("text/plain", fileLocal);

    // upload the file into Google Drive
    File resultFile = drive.files().insert(remoteFile, remoteFileContent).execute();

    lblStatus.setText("Upload successful with file ID: " + resultFile.getId());
}
catch (Exception e)
{
    // error handling
    ...
}

我已经阅读了 Google API 文档,它们一团糟,而且这些类没有适当的文档。他们给的样品很乱。

知道为什么应用程序没有响应吗?我究竟做错了什么?任何帮助表示赞赏。谢谢。

【问题讨论】:

    标签: java android google-drive-api


    【解决方案1】:

    几周前我遇到了这个问题。看看这里,我在那里写了一篇关于如何使 Google Drive 主要在 Android 上运行的非常广泛的解释。 (我说主要是因为文件下载似乎没有得到适当的授权......)

    Android Open and Save files to/from Google Drive SDK

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-02
      • 1970-01-01
      • 1970-01-01
      • 2023-03-06
      • 2014-06-06
      • 2017-03-21
      • 1970-01-01
      相关资源
      最近更新 更多