I am trying to retrieve the content of a Google Drive file using HTTP get request in a Android app. I've already authenticated myself and got the token, got the file metadata below (just replaced "sensitive info" with <"sensitive info">):
{
"kind": "drive#file",
"id": ""myFileId"",
"etag": ""this_etag"",
"selfLink": "https://www.googleapis.com/drive/v2/files/"myFileId"",
"alternateLink": "https://docs.google.com/file/d/"myFileId"/edit",
"thumbnailLink": "https://lh4.googleusercontent.com/thumbnailCode",
"permissionsLink": "https://www.googleapis.com/drive/v2/files/"myFileId"/permissions",
"title": "filename.txt",
"mimeType": "text/plain",
"labels": {
"starred": false,
"hidden": false,
"trashed": false,
"restricted": false,
"viewed": true
},
"createdDate": "2012-07-13T17:53:29.589Z",
"modifiedDate": "2012-07-13T18:58:36.729Z",
"modifiedByMeDate": "2012-07-13T17:53:29.584Z",
"lastViewedByMeDate": "2012-07-19T20:40:03.823Z",
"parents": [
{
"kind": "drive#parentReference",
"id": "",
"selfLink": "https://www.googleapis.com/drive/v2/files/"myFileId"/parents/"id"",
"parentLink": "https://www.googleapis.com/drive/v2/files/"id"",
"isRoot": true
}
],
"downloadUrl": "https://doc-0k-28-docs.googleusercontent.com/docs/securesc/"bigCode"/"anotherBigCode"/"moreNumbers"/"moreNumbers"/"moreNumbers"/"myFileId"?h="moreNumbers"&e=download&gd=true",
"userPermission": {
"kind": "drive#permission",
"etag": ""this_etag"",
"id": "current",
"selfLink": "https://www.googleapis.com/drive/v2/files/"myFileId"/permissions/current",
"role": "owner",
"type": "user"
},
"originalFilename": "zzzz.txt",
"fileExtension": "txt",
"md5Checksum": "da22f14e635aa54e97e0e09b5d03a485",
"fileSize": "51",
"quotaBytesUsed": "51",
"ownerNames": [
"Emerson.yt"
],
"lastModifyingUserName": "Emerson.yt",
"editable": true,
"writersCanShare": true
}
When I try to get the contents using downloadUrl returned in the metadata it gives the error
"W/System.err(612): com.google.api.client.http.HttpResponseException: 404 Not Found"
The relevant java code I'm using:
JsonFactory jsonFactory = new JacksonFactory();
HttpTransport transport = new NetHttpTransport();
// prefs is a SharedPreferences instance
// acquired throught PreferenceManager.getDefaultSharedPreferences(this);
CredentialStore credentialStore = new SharedPreferencesCredentialStore(prefs);
TokenResponse accessTokenResponse = credentialStore.read();
Builder bldr = new Builder();
bldr.setTransport(transport);
bldr.setJsonFactory(jsonFactory);
bldr.setClientSecrets(myGoogleApiClientId, myGoogleApiClientSecret);
GoogleCredential accessProtectedResource = bldr.build();
accessProtectedResource.setAccessToken(accessTokenResponse.getAccessToken());
accessProtectedResource.setExpiresInSeconds(accessTokenResponse.getExpiresInSeconds());
accessProtectedResource.setRefreshToken(accessTokenResponse.getRefreshToken());
// HTTP_TRANSPORT is an instance of NetHttpTransport
HttpRequestFactory httpReqFactory = HTTP_TRANSPORT.createRequestFactory(accessProtectedResource);
GenericUrl url = new GenericUrl("https://www.googleapis.com/drive/v2/files/"myFileId"");
HttpRequest httpReq = httpReqFactory.buildGetRequest(url);
HttpResponse httpResp = httpReq.execute();
String resp = httpResp.parseAsString();
// just convert json string into java attributes of FileResourceMetadata class
FileResourceMetadata frm = new FileResourceMetadata(resp);
String downloadURL = frm.downloadUrl;
url = new GenericUrl(downloadURL);
httpReq = httpReqFactory.buildGetRequest(url);
httpResp = httpReq.execute();
What's wrong?
Thanks, Emerson.