1
votes

I'm trying to get the contents of a file stored in Google Drive through JavaScript. I have been able to download the file manually just fine by pasting the URL (the webContentLink from the JSON response to a list query) in my browser. Their SDK doesn't support this natively (the get command only gives you the metadata), so I'm trying using jQuery, and running into a CORS issue even though I've set my site as the origin when I created my Google OAuth client ID & secret. My code is like:

$.ajax({
          url: "https://docs.google.com/uc?id=0B-9B62bXdQ1nTFlwY0pveVgzOVU&export=download",
          headers: {"Authorization": "Bearer " + gapi.auth.getToken().access_token}
        })

I get a 405 Method not allowed error: https://docs.google.com/uc?id=0B-9B62bXdQ1nTFlwY0pveVgzOVU&export=download 405 (Method Not Allowed) jquery-2.1.1.min.js:4 XMLHttpRequest cannot load https://docs.google.com/uc?id=0B-9B62bXdQ1nTFlwY0pveVgzOVU&export=download. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://mydomain.com' is therefore not allowed access.

According to this video, Google Drive should support CORS requests. But according to this bug, it is not working. Is there any way to do this without using a server-side proxy or some iframe hack?

1
use the downloadUrl (for an alient file type) or exportLinks (for a google doc type) - pinoyyid
I tried switching to the downloadUrl (for a text file I uploaded) but I now get a 403 forbidden error. Am I sending the auth token correctly? - skunkwerk

1 Answers

0
votes

Once I switched to using the latest downloadUrl (this URL changes frequently) and sent the authorization header, I was able to get the contents of the file perfectly.