Getting 401 Unauthorized from Google API Resumable Update

天涯浪子 提交于 2019-11-28 09:43:16

问题


I am trying to integrate upload of arbitrary files to Google Docs into an existing application. This used to work before using resumable upload became mandatory. I am using Java client libraries.

The application is doing the upload in 2 steps: - get the resourceId of the file - upload the data

To get the resourceId I am uploading a 0-size file (i.e. Content-Length=0). I am passing ?convert=false in the resumable URL (i.e. https://docs.google.com/feeds/upload/create-session/default/private/full?convert=false).

I am passing "application/octet-stream" as content-type. This seems to work, though I do get different resourcesIds - "file:..." resourceIds for things like images, but "pdf:...." resourceIds for PDFs.

The second step constructs a URL based on the resourceId obtained previously and performs a search (getEntry). The URL is in the form of https://docs.google.com/feeds/default/private/full/file%3A.....

Once the entry is found the ResumableGDataFileUploader is used to update the content (0-byte file) with the actual data from the file being uploaded. This operation fails with 401 Unauthorized response when building ResumableGDataFileUploader instance.

I've tried with ?convert=false as well as ?new-revision=true and both of these at the same time. The result is the same.

The relevant piece of code:

MediaFileSource mediaFile = new MediaFileSource(
    tempFile, "application/octet-stream");

final ResumableGDataFileUploader.Builder builder = 
    new ResumableGDataFileUploader.Builder(client, mediaFile, documentListEntry);
builder.executor(MoreExecutors.sameThreadExecutor());
builder.requestType(ResumableGDataFileUploader.RequestType.UPDATE);

// This is where it fails
final ResumableGDataFileUploader resumableGDataFileUploader = builder.build();
resumableGDataFileUploader.start();

return tempFile.length();

The "client" is an instance of DocsService, configured to use OAuth. It is used to find "documentListEntry" immediately before the given piece of code.

I had to explicitly specify request type, since it seems the client library code contains a bug causing NullPointerException for "update existing entry" case.

I have a suspicion that the issue is specifically in the sequence of actions (upload 0-byte file to get the resourceId, then update with actual file) but I can't figure out why it doesn't work.

Please help?


回答1:


This code snippet works for me using OAuth 1.0 and OAuth 2.0:

static void uploadDocument(DocsService client) throws IOException, ServiceException,
    InterruptedException {
  ExecutorService executor = Executors.newFixedThreadPool(10);

  File file = new File("<PATH/TO/FILE>");
  String mimeType = DocumentListEntry.MediaType.fromFileName(file.getName()).getMimeType();

  DocumentListEntry documentEntry = new DocumentListEntry();
  documentEntry.setTitle(new PlainTextConstruct("<DOCUMENT TITLE>"));

  int DEFAULT_CHUNK_SIZE = 2 * 512 * 1024;
  ResumableGDataFileUploader.Builder builder =
      new ResumableGDataFileUploader.Builder(
          client,
          new URL(
              "https://docs.google.com/feeds/upload/create-session/default/private/full?convert=false"),
          new MediaFileSource(file, mimeType), documentEntry).title(file.getName())
          .requestType(RequestType.INSERT).chunkSize(DEFAULT_CHUNK_SIZE).executor(executor);

  ResumableGDataFileUploader uploader = builder.build();
  Future<ResponseMessage> msg = uploader.start();
  while (!uploader.isDone()) {
    try {
      Thread.sleep(100);
    } catch (InterruptedException ie) {
      throw ie; // rethrow
    }
  }

  DocumentListEntry uploadedEntry = uploader.getResponse(DocumentListEntry.class);
  // Print the document's ID.
  System.out.println(uploadedEntry.getId());
  System.out.println("Upload is done!");
}


来源:https://stackoverflow.com/questions/10153904/getting-401-unauthorized-from-google-api-resumable-update

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!