Reputation: 842
The Google Drive API change from V2 to V3, remove the revision.items from response. The items, had a list of urls, being the files in .txt, .pdf, etc. (I'm using straight HTTP protocol, no wrappers). In V3 there seems to be no way to do this. There is however an ?alt=media parameter, which is supposed to return a file, but I get a 403 error trying it.
Any clues?
https://developers.google.com/drive/v3/reference/revisions/get
Upvotes: 1
Views: 3352
Reputation: 1
I used this sample app to handle the OAUTH2 tasks.
Then I added this function to make a drive call. It doesn't matter if exportLinks is empty or not; just build the URL as below.
private async Task GetRevisionRedirect(string accessToken, string fileId, string revId)
{
Log("Making API Call to get revision binary stream...");
// builds the request
string userinfoRequestUri = "https://docs.google.com/feeds/download/documents/export/Export?id=" + fileId + "&revision=" + revId + "&exportFormat=docx";
// sends the request
HttpWebRequest userinfoRequest = (HttpWebRequest)WebRequest.Create(userinfoRequestUri);
userinfoRequest.Method = "GET";
userinfoRequest.Headers.Add(string.Format("Authorization: Bearer {0}", accessToken));
userinfoRequest.ContentType = "application/x-www-form-urlencoded";
userinfoRequest.Accept = "Accept=text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
// gets the response
WebResponse userinfoResponse = await userinfoRequest.GetResponseAsync();
using (StreamReader userinfoResponseReader = new StreamReader(userinfoResponse.GetResponseStream()))
{
// reads response body
System.IO.Stream streamDoc = userinfoResponseReader.BaseStream;
var fileStream = File.Create("d:\\test.docx");
streamDoc.CopyTo(fileStream);
fileStream.Flush();
fileStream.Close();
}
}
Upvotes: 0
Reputation: 201338
For Google Docs, since the revision files would not be able to be retrieved using Drive API v3, I modified this using new workaround. The new workaround is to use Drive API v2. drive.revisions.get
of Drive API v2 can retrieve not only the revision list, but also the export links. I thought of the use of the export links. This became the new workaround for the current situation.
This sample downloads spreadsheet using revision ID.
curl -sSLG \
-H 'Authorization: Bearer ### Access token ###' \
"https://www.googleapis.com/drive/v2/files/### FileID ###/revisions?fields=items(exportLinks%2Cid)"
curl -sSLG \
-H 'Authorization: Bearer ### Access token ###' \
"https://docs.google.com/spreadsheets/export?id=### FileID ###&revision=### revision number ###&exportFormat=xlsx" \
-o outputfilename.xlsx
Reference : https://developers.google.com/drive/v2/reference/revisions/get
In the case of except for Google Docs, the revision ID is just file ID. So you can download (Pattern 1) not only using revision ID, but also as a normal file (Pattern 2).
Pattern 1 :
curl -sSLG \
-H 'Authorization: Bearer ### Access token ###' \
"https://www.googleapis.com/drive/v3/files/### FileID ###/revisions/### RevisionID ###?alt=media" \
-o outputfilename
Pattern 2 :
curl -sSLG \
-H 'Authorization: Bearer ### Access token ###' \
"https://www.googleapis.com/drive/v3/files/### RevisionID ###?alt=media" \
-o outputfilename
Reference : https://developers.google.com/drive/v3/reference/revisions/get
Upvotes: 2
Reputation:
It doesn't appear to be currently possible on drive v3.
Revisions API will work for non google docs/slides/sheets (binaries) as above. For native google docs files, if you try it it will complain to use the export endpoint (file/id/export). Trying the export end point results in errors such as
"error": {
"errors": [
{
"domain": "global",
"reason": "invalidParameter",
"message": "Invalid field selection revisions=11175",
"locationType": "parameter",
"location": "fields"
}
],
"code": 400,
"message": "Invalid field selection revisions=11175"
}
I tried fields like "rev", "revision", "revisions" with no luck. I opened a feature request for this here https://issuetracker.google.com/u/1/issues/62825716
Upvotes: 0