midworld / unity-googledrive

Google Drive for Unity3D
Apache License 2.0
121 stars 28 forks source link

Exporting a google doc file #29

Closed sidhants closed 7 years ago

sidhants commented 7 years ago

Hi, I am trying to add functionality to export a google doc file as a pdf as mentioned in the docs here: https://developers.google.com/drive/v2/reference/files/export

On creating the request, it gives a "unity google drive exception : required parameter mimetype" HTTP/1.1 400 Bad Request How do I specify mimetype correctly in my request? This is what I have so far.

public IEnumerator ExportFile(File file) { var request = new UnityWebRequest("https://www.googleapis.com/drive/v2/files/" + file.ID + "/export"); request.method = "GET"; request.headers["Authorization"] = "Bearer " + AccessToken; request.headers["Content-Type"] = "application/pdf"; request.body = new byte[0]; // with no data

    var response = new UnityWebResponse(request);
    while (!response.isDone)
        yield return null;

    JsonReader reader = new JsonReader(response.text);
    var json = reader.Deserialize<Dictionary<string, object>>();

    if (json == null)
    {
        yield return new Exception(-1, "ExportFile response parsing failed.");
        yield break;
    }
    else if (json.ContainsKey("error"))
    {
        yield return GetError(json);
        yield break;
    }

    yield return new AsyncSuccess(new File(json));
}

Still learning. Will appreciate help. Thanks, Sidhant.

midworld commented 7 years ago

You should provide the mime type like: .../export?mimeType=application%2Fpdf

On Thu, Sep 21, 2017 at 9:58 AM sidhants notifications@github.com wrote:

Hi, I am trying to add functionality to export a google doc file as a pdf as mentioned in the docs here: https://developers.google.com/drive/v2/reference/files/export

On creating the request, it gives a "unity google drive exception : required parameter mimetype" HTTP/1.1 400 Bad Request How do I specify mimetype correctly in my request? This is what I have so far.

public IEnumerator ExportFile(File file) { var request = new UnityWebRequest("https://www. googleapis.com/drive/v2/files/" + file.ID + "/export"); request.method = "GET"; request.headers["Authorization"] = "Bearer " + AccessToken; request.headers["Content-Type"] = "application/pdf"; request.body = new byte[0]; // with no data

var response = new UnityWebResponse(request); while (!response.isDone) yield return null;

JsonReader reader = new JsonReader(response.text); var json = reader.Deserialize<Dictionary<string, object>>();

if (json == null) { yield return new Exception(-1, "ExportFile response parsing failed."); yield break; } else if (json.ContainsKey("error")) { yield return GetError(json); yield break; }

yield return new AsyncSuccess(new File(json)); }

Still learning. Will appreciate help. Thanks, Sidhant.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/midworld/unity-googledrive/issues/29, or mute the thread https://github.com/notifications/unsubscribe-auth/ABnn7AfXuq2etwlX08yDEySaWCS2qHOKks5skbTSgaJpZM4Peqia .

sidhants commented 7 years ago

Ok that worked and I am able to get the bytes in the web response.

How do I transform that into a type of GoogleDrive.File so that I can use it for uploading or passing to my interface which accepts a pdf file path for downloading pdf's?
There is no download url in the response (only bytes) so I am assuming I will have to export and upload it on the fly back to my drive so that it is available for download further. Any advice is appreciated. Thanks.

sidhants commented 7 years ago

I verified that the pdf file created is correct by saving it locally: System.IO.FileStream fs = new System.IO.FileStream("C:\Users\CyberGlove\Desktop\ppt\pptTemplatePDF.pdf",System.IO.FileMode.OpenOrCreate); fs.Write(bytes,0,bytes.Length); fs.Close();

But I am still not sure how I can use the export bytes obtained to makes a GoogleDrive.File object for further use of uploading/downloading or if there is any download url? Thanks, Sidhant.

midworld commented 7 years ago

Hi again, Google doesn't support a download link for the exported file. You should upload the byte stream with content type application/pdf to your Google drive if you want a download link of the converted PDF file. Thanks.

sidhants commented 7 years ago

Got it. thanks. closing the thread.