1

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.

  • 404 is resource not found. Most probably it is something with url is not correct. Try to access same url in browser and see you are getting correct data or not. – kosa Jul 20 '12 at 15:44
  • what is the value of myFileId - you seem to have a hardcoded String in line 1 of the java you've given. – Dave Richardson Jul 20 '12 at 15:51
  • Yes 404 is resource not found, but I'm using the exact downloadUrl that comes in the meta data of the file, so I think it's correct. – Emerson Takahashi Jul 20 '12 at 16:54
  • myFileId is the file id of my file in Google Drive. I got it in File/Share. I also think it's correct because when I use it to retrive the meta data it returns the correct information. For instance: "originalFilename": "zzzz.txt", "fileSize": "51" – Emerson Takahashi Jul 20 '12 at 16:57
  • When I try to use the downloadUrl in browser (Opera) it returns: "The server requested a login authentication method that is not supported. Check that the address is spelled correctly, or try searching for the site." If I remove the last part of the url "&gd=true" it opens the download dialog and the file is downloaded normally. If I use the downloadUrl without "&gd=true" in my android app, it returns Error 501 - Not implemented. – Emerson Takahashi Jul 20 '12 at 17:04
  • By the way. The downloadUrl in the meta data has the format: https://doc-0k-28-docs.googleusercontent.com/docs/securesc/xxxxx/yyyyy/12345/12345/12345/abc12345?h=12345&e=download&gd=true – Emerson Takahashi Jul 20 '12 at 17:12
  • Just in case since you seem to be using lots of custom code: How do you authorize the request with the access token? Do you use the Authorization header or the access_token URL parameter? Currently the later is **not** supported. – Nicolas Garnier Jul 21 '12 at 01:53
  • Hi @Nivco I put the access token in the factory I use to create the http request. Am i missing something??? – Emerson Takahashi Jul 21 '12 at 14:07
  • I've put the authorization token in the head with same results (404 - Not Found). `headers = httpReq.getHeaders(); headers.setAuthorization(accessTokenResponse.getAccessToken());`. – Emerson Takahashi Jul 21 '12 at 14:22
  • That's good then :) the facory shold use teh Authorizarion header automatically. It's only if ou were creating the HTTP request yourself that you might have used the URL parameter to do so. – Nicolas Garnier Jul 23 '12 at 18:10
  • You code seem correct (thought I'm only very confortable with Java). One of the way I could see a 404 happening in that case is if you are authenticating with a different user that the one owning/have read access to the file.but it;s strage because if you read the metadata successfully then it should be all right... :/ – Nicolas Garnier Jul 23 '12 at 18:12
  • Hi @Nivco, as I put before, if I use the downloadUrl from the metadata in a browser (removing the last part of it: "&gd=true") it works, it opens the download file dialog and downloads the file. – Emerson Takahashi Jul 23 '12 at 21:43
  • 1
    It seems to be related to this other StackOverflow [question](http://stackoverflow.com/a/11526699/1106381). – Alain Jul 23 '12 at 21:45
  • Hi @Alain what's this "?key=" parameter you refer to in that question? The downloadUrl doesn't have it. I'm already using `https://www.googleapis.com/auth/drive` scope. – Emerson Takahashi Jul 24 '12 at 17:18

0 Answers0