Gain 401 unauthorized access to update Google API

I am trying to integrate uploading arbitrary files into Google Docs into an existing application. This was used to work before using renewable downloads, became mandatory. I am using Java client libraries.

The application downloads in 2 stages: - get the file resourceId file - download data

To get resourceId, I upload a 0-dimensional file (i.e. Content-Length = 0). I am handing over? Convert = false in the revolving URL (i.e. https://docs.google.com/feeds/upload/create-session/default/private/full?convert=false ).

I pass "application / octet-stream" as the content type. It seems to work, although I have different resources. Identifiers are "file: ..." files for things like images, but "pdf: ..." resourceIds for PDF files.

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

After the record is found, ResumableGDataFileUploader is used to update the contents (0-byte file) with the actual data from the downloaded file. This operation failed with a 401 Unauthorized response while creating an instance of ResumableGDataFileUploader.

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

Corresponding code snippet:

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(); 

A β€œclient” is an instance of DocsService configured to use OAuth. It is used to search for "documentListEntry" immediately before a given piece of code.

I had to explicitly specify the type of request, since it seems that the client library code contains an error that throws a NullPointerException for the case "update an existing record".

I have a suspicion that the problem is specifically in the sequence of actions (upload a 0-byte file to get the resourceId and then update using the actual file), but I can not understand why it does not work.

Please, help?

+2
source share
1 answer

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 ID. System.out.println(uploadedEntry.getId()); System.out.println("Upload is done!"); } 
+3
source

All Articles