Search code examples
.netgoogle-drive-apihttpwebrequestgoogle-picker

Google Picker Client with Server .Net File Download 403 Forbidden


I am using Google picker in a web client to allow a user to authorize my application and select a file for download. I retrieve the fileId and oauthToken for the selected file and pass it to my back end similar to what I found here (Google picker and backend file download).

The back-end process is .Net and I am using the code shown below to submit the request for the file. However, I receive a 403 Forbidden error even though the same Get request works fine in Postman when I send though the same Url, FileId, and oAuthToken information.

Any ideas on what might be wrong with the setup of my HttpWebRequest?

public void Download(string pOAuthToken, string pFileId, string pFileName) {
    HttpWebRequest request;
    HttpWebResponse response;

    bool result = false;
    string url = "";

    try {
        url = "https://www.googleapis.com/drive/v3/files/" + pFileId + "?alt=media";
        request = WebRequest.Create(url);
        request.Headers.Add("Authorization", ("Bearer " + pOAuthToken));

        response = request.GetResponse();

        //  Insert code to download file here

        result = true;
    }
    catch (Exception ex) {
        LogError("Download exception.", ex);
    }
    finally {
        response.Close();
    }

    return result;
}

Solution

  • I was able to resolve the issue by using version 2 of the Google Drive REST API:

    url = "https://www.googleapis.com/drive/v2/files/" + pFileId + "?alt=media";

    The docs indicate that this should also work with version 3 (https://developers.google.com/drive/v3/web/manage-downloads) but not in my case.