1

我正在尝试使用 Android 应用程序中的 HTTP 获取请求来检索 Google Drive 文件的内容。我已经对自己进行了身份验证并获得了令牌,得到了下面的文件元数据(只是将“敏感信息”替换为 <“敏感信息”>):

    {
    “种类”:“驱动器#文件”,
    "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",
    “标题”:“文件名.txt”,
    “mimeType”:“文本/纯文本”,
    “标签”: {
     “加星标”:错误,
     “隐藏”:错误,
     “垃圾”:错误,
     “受限”:错误,
     “已查看”:真实
    },
    "createdDate": "2012-07-13T17:53:29.589Z",
    “修改日期”:“2012-07-13T18:58:36.729Z”,
    "modifiedByMeDate": "2012-07-13T17:53:29.584Z",
    "lastViewedByMeDate": "2012-07-19T20:40:03.823Z",
    “父母”: [
     {
      “种类”:“驱动器#parentReference”,
      “ID”: ””,
      "selfLink": "https://www.googleapis.com/drive/v2/files/"myFileId"/parents/"id"",
      "parentLink": "https://www.googleapis.com/drive/v2/files/"id"",
      “是根”:真
     }
    ],
    "downloadUrl": "https://doc-0k-28-docs.googleusercontent.com/docs/securesc/"bigCode"/"anotherBigCode"/"moreNumbers"/"moreNumbers"/"moreNumbers"/"myFileId"?h ="moreNumbers"&e=下载&gd=true",
    “用户权限”:{
     "kind": "drive#permission",
     "etag": ""this_etag"",
     “id”:“当前”,
     "selfLink": "https://www.googleapis.com/drive/v2/files/"myFileId"/permissions/current",
     “角色”:“所有者”,
     “类型”:“用户”
    },
    "originalFilename": "zzzz.txt",
    "文件扩展名": "txt",
    "md5Checksum": "da22f14e635aa54e97e0e09b5d03a485",
    “文件大小”:“51”,
    "quotaBytesUsed": "51",
    “所有者名称”:[
     “爱默生.yt”
    ],
    "lastModifyingUserName": "Emerson.yt",
    “可编辑”:是的,
    “writersCanShare”:是的
    }

当我尝试使用元数据中返回的 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();

怎么了?

谢谢,爱默生。

4

0 回答 0