0

I've been trying for quite a while to download a settings file from a user's google drive for a web app of mine. I'm able to successfully upload a file using javascript, but now I cannot get the google example download code to work to obtain back the contents of the file. Here's the current code. I'm able to obtain the file's metadata.

   function downloadFile(file) {
    if (file.downloadUrl) {
        var accessToken = gapi.auth.getToken().access_token; <-Error
        var xhr = new XMLHttpRequest();
        xhr.open('GET', file.downloadUrl);
        xhr.setRequestHeader('Authorization', 'Bearer ' + accessToken);
        xhr.onload = function() {
            console.log(xhr.responseText);
        };
        xhr.onerror = function() {
            console.log("ERROR");
        };
        xhr.send();
    } else {
        console.log("No file.downloadUrl");
    }
}

The console error says "Cannot read property 'access_token' of null" which I think it may be because it requires an API key but I'm using a client id to sign people in. Is there a way to do this without using the API key and with the client id?

exiledwolf
  • 80
  • 8
  • possible duplicate - http://stackoverflow.com/questions/23654749/google-app-engine-authorization-returning-null. Your request is not being process since `getToken` is null (which indicates that the application is not authorized properly) – adjuremods Feb 28 '16 at 03:21

1 Answers1

0

You need to have an API key to work with any of Google's APIs. https://developers.google.com/loader/signup

Is there any specific reason why you don't want to sign up for one?

S. Thrall
  • 19
  • 3
  • I mean I don't know much about this, but I am already interacting with the google api. I can upload a file into a user's Drive using my client ID. I didn't think I needed both an API key and a client id. I also wasn't sure how to integrate both into my webapp – exiledwolf Feb 28 '16 at 00:53
  • That was my mistake, you shouldn't need an API key for Drive. The specific error is more than likely because of an authentication error. I'm assuming you're authenticating before this? – S. Thrall Feb 28 '16 at 01:19
  • You may also want to look at exporting the file. https://developers.google.com/drive/v3/reference/files/export – S. Thrall Feb 28 '16 at 01:25
  • Yes I'm using a sign in button from their examples https://developers.google.com/identity/sign-in/web/build-button And that looks like it's for google docs. I'm trying to just upload a simple txt file using V2. – exiledwolf Feb 28 '16 at 02:17