Getting 401 Unauthorized from Google API Resumable Update

有些话、适合烂在心里 提交于 2019-11-29 16:14:06

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