Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Picker => How to download the file

C# Here if it matters. What I am trying to do is allow users on mobile phones (or desktops) to upload files to my web server. I have the picker built, got the auth all figured out for the scope drive.readonly.

But what I keep seeing in the examples when it comes time to download the file is they download it using Javascript and then upload it again to the server. I would like to get it from my server. I have this working fine with Dropbox, but google is a problem.

I followed the guide here: https://developers.google.com/picker/docs/

But then I send the fileId and token to the server:

 function pickerCallback(data) {
    if (data.action == google.picker.Action.PICKED) {
        var datastring = "url=" + data.docs[0].url + "&name=" + data.docs[0].name + "&mimetype=" + data.docs[0].mimetype + "&fileId=" + data.docs[0].id + "&token=" + oauthToken;
            $.ajax({
                type: "POST",
                url: "/FileUpload/GetGoogleDriveFile/",
                data: datastring,
                dataType: "json",
                success: function (data) {
                    if (data.success != null && data.success == true) {
                        window.location.href = "@Html.Raw(AfterUploadDestination)";
                    }
                    else
                    {
                        alert("Error: " + data.ErrorMsg);
                    }
                },
                error: function (xhr, textStatus, errorThrown) {
                    console.log(xhr.responseText);
                }
            });


        googlepicker.setVisible(false);
    }
}

And then on the server I have something like...

 var client = new System.Net.WebClient();
            client.Headers.Set(HttpRequestHeader.Authorization, "Bearer " + file.token);

then something to consume the file:

doc.Contents = client.DownloadData(string.Format("https://www.googleapis.com/drive/v3/files/{0}?alt=media", file.fileId));

The problem is that the file is not just the binary stream of the file but the webpage itself that google displays when you go to the site with that link.

How in the world do I just download the file?

Thanks in advance and I hope this wasn't so long that no one reads it.

like image 943
Paul Perrick Avatar asked Oct 28 '25 18:10

Paul Perrick


2 Answers

You may check this documentation on how to open files using the Google Picker API. When a user selects a file from the list, the file ID is returned, and the ID may be used by your app to access the file. After obtaining the file ID from the picker when opening files, an application can then fetch the file metadata and download the file content as described in the reference documentation for files.get.

like image 148
abielita Avatar answered Oct 30 '25 08:10

abielita


It was me.

I asked Google what the problem could be and support came back with, you need to create a Client ID of Type "Other". So when it didn't work I assumed it was my code.

All I needed to do was use the came Client ID as the Website (Type of Web Application) and create a specific API Key for the picker to use.

For anyone who has issues, this is the meat of the code:

So my Client code is:

function pickerCallback(data) {
        if (data.action == google.picker.Action.PICKED) {
            var datastring = "url=" + data.docs[0].url + "&name=" + data.docs[0].name + "&mimetype=" + data.docs[0].mimetype + "&fileId=" + data.docs[0].id + "&token=" + oauthToken;
                $.ajax({
                    type: "POST",
                    url: "/FileUpload/GetGoogleDriveFile/",
                    data: datastring,
                    dataType: "json",
                    success: function (data) {
                        if (data.success != null && data.success == true) {
                            window.location.href = "@Html.Raw(AfterUploadDestination)";
                        }
                        else
                        {
                            alert("Error: " + data.ErrorMsg);
                        }
                    },
                    error: function (xhr, textStatus, errorThrown) {
                        console.log(xhr.responseText);
                    }
                });
            googlepicker.setVisible(false);
        }
    }

Server Code is:

 public class GoogleFileDetails
        {
            public string url { get; set; }
            public string name { get; set; }
            public int mimetype { get; set; }
            public string fileId { get; set; }
            public string token { get; set; }
        }

[HttpPost]
 public ActionResult GetGoogleDriveFile(GoogleFileDetails file) {

            byte[] contents = null;
            try
            {
                var client = new System.Net.WebClient();
                client.Headers.Set(HttpRequestHeader.Authorization, "Bearer " + file.token);
                contents = client.DownloadData(string.Format("https://www.googleapis.com/drive/v2/files/{0}?alt=media", file.fileId));
            } catch etc...
like image 34
Paul Perrick Avatar answered Oct 30 '25 08:10

Paul Perrick



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!