1
votes

When I request the downloadUrl field of a Google Drive file, I'm receiving a 401 Unauthorized response. What am I doing wrong?

EDIT: To clarify, the file permissions are set to public (test.txt), and I can access the file's metadata to get the downloadUrl (as is shown in the example code below). So why am I able to use the /files/{id}?key={api_key} endpoint but not the downloadUrl link? According to https://developers.google.com/console/help/new/#usingkeys, you should be able to use an api key when "Data that the data owner has identified as public, such as a public calendar or blog."

https://developers.google.com/drive/v2/reference/files#downloadUrl

var xhr = new XMLHttpRequest();
xhr.open("get", "https://www.googleapis.com/drive/v2/files/0Bzim72JjnlIWRFlURmRrX3N6NEE?key=AIzaSyDo66jOFuHllu3r3PwKcZaUd7N9ho9l9WU");
xhr.onreadystatechange = function(){
    if(xhr.readyState === 4){
        var download = new XMLHttpRequest();
        download.open("get", JSON.parse(xhr.responseText)['downloadUrl']);
        download.onreadystatechange = function(){
            if(download.readyState === 4){
                console.log(download.status);
            }
        };
        download.send();
    }
};
xhr.send();
1

1 Answers

-1
votes

You need to use OAuth and get permission from the user, in order to authenticate your requests and get access to the Drive files. Check our the documentation here.