错误404未找到尝试使用http get从Google云端硬盘检索文件内容

时间:2012-07-20 15:43:42

标签: java android google-drive-api

我正在尝试使用Android应用中的HTTP get请求检索Google云端硬盘文件的内容。我已经对自己进行了身份验证并获得了令牌,获得了下面的文件元数据(仅使用<“敏感信息”>替换为“敏感信息”):

    {
    "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
    }

当我尝试使用元数据中返回的downloadUrl来获取内容时,会出现错误

"W/System.err(612): com.google.api.client.http.HttpResponseException: 404 Not Found" 

我正在使用的相关java代码:

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();

怎么了?

谢谢,艾默生。

0 个答案:

没有答案