1

我正在使用google api dot net client,我有点困惑。

以下代码返回特定文件夹的完整列表(ID:XXXXXXXXXXXXXXXXXXXXX)。

List<File> result = new List<File>();
FilesResource.ListRequest request = service.Files.List();
request.Q = "'XXXXXXXXXXXXXXXXXXXXX' in parents";
request.MaxResults = 35;
FileList files = request.Fetch();

例如,此 FileList 的一项是

"id": "0B5XhOfl0NZ2cZ1M1aVBPcVlTUTA",
"title": "87BC1CAE-A01C-43A1-BB21-BE3DA6D6C7DC"

如果我更改查询,搜索标题,我总是收到一个空的 FileList。

List<File> result = new List<File>();
FilesResource.ListRequest request = service.Files.List();
request.Q = "title = '87BC1CAE-A01C-43A1-BB21-BE3DA6D6C7DC'";
request.MaxResults = 35;
FileList files = request.Fetch();

运算符是 '=' 还是 'contains' 或仅搜索目录名称的一部分都没有关系:

title contains '87BC1CAE'

我只有通过搜索 ID 才能收到正确的结果。以下代码完美无缺:

File file = service.Files.Get(fileId).Fetch();

Console.WriteLine("Title: " + file.Title);
Console.WriteLine("Description: " + file.Description);
Console.WriteLine("MIME type: " + file.MimeType);

请帮忙 :)

@AliAfshar 原始 HTTP 响应:第一种情况(标题 = '87BC1CAE-A01C-43A1-BB21-BE3DA6D6C7DC')

request.Fetch()
TestGoogleApi.vshost.exe Information: 0 : DotNetOpenAuth, Version=4.0.0.11165, Culture=neutral, PublicKeyToken=2780ccd10d57b246 (official)
TestGoogleApi.vshost.exe Information: 0 : Preparing to send AssertionFlowMessage (2.0) message.
TestGoogleApi.vshost.exe Information: 0 : Sending AssertionFlowMessage request.
TestGoogleApi.vshost.exe Information: 0 : HTTP POST https://accounts.google.com/o/oauth2/token
TestGoogleApi.vshost.exe Information: 0 : The following required parameters were missing from the DotNetOpenAuth.OAuth2.Messages.AccessTokenFailedResponse message: {error,}
   TestGoogleApi.vshost.exe Information: 0 : Received UnauthorizedResponse response.
   {Google.Apis.Drive.v2.Data.FileList}
_etag: "\"Q0cVodxX8sh4vfxZTlOyWcmmc0k/vyGp6PvFo4RvsFtPoIWeCReyIC8\""
_items: Count = 0
_kind: null
_nextLink: null
_nextPageToken: null
_selfLink: null
ETag: "\"Q0cVodxX8sh4vfxZTlOyWcmmc0k/vyGp6PvFo4RvsFtPoIWeCReyIC8\""
Items: Count = 0
Kind: null
NextLink: null
NextPageToken: null
SelfLink: null

第二种情况(父母中的'XXXXXXXXXXXXXXXXXXXXXXXX')

request.Fetch()
TestGoogleApi.vshost.exe Information: 0 : DotNetOpenAuth, Version=4.0.0.11165, Culture=neutral,  PublicKeyToken=2780ccd10d57b246 (official)
TestGoogleApi.vshost.exe Information: 0 : Preparing to send AssertionFlowMessage (2.0) message.
TestGoogleApi.vshost.exe Information: 0 : Sending AssertionFlowMessage request.
TestGoogleApi.vshost.exe Information: 0 : HTTP POST https://accounts.google.com/o/oauth2/token
TestGoogleApi.vshost.exe Information: 0 : The following required parameters were missing from the  DotNetOpenAuth.OAuth2.Messages.AccessTokenFailedResponse message: {error,}
TestGoogleApi.vshost.exe Information: 0 : Received UnauthorizedResponse response.
{Google.Apis.Drive.v2.Data.FileList}
_etag: "\"Q0cVodxX8sh4vfxZTlOyWcmmc0k/Jyx7utsp71-_JwU5RHnI_VZmL5o\""
_items: Count = 1575
_kind: null
_nextLink: null
_nextPageToken: null
_selfLink: null
ETag: "\"Q0cVodxX8sh4vfxZTlOyWcmmc0k/Jyx7utsp71-_JwU5RHnI_VZmL5o\""
Items: Count = 1575
Kind: null
NextLink: null
NextPageToken: null
SelfLink: null

更新于 2013 年 2 月 2 日 好的,请原谅我的延迟。我使用了提琴手,这是捕获的结果:


第一个请求:

POST https://accounts.google.com/o/oauth2/token HTTP/1.1
Content-Type: application/x-www-form-urlencoded; charset=utf-8
User-Agent: DotNetOpenAuth/4.0.0.11165
Host: accounts.google.com
Cache-Control: no-store,no-cache
Pragma: no-cache
Content-Length: 603
Connection: Keep-Alive

grant_type=assertion&assertion_type=http%3A%2F%2Foauth.net%2Fgrant_type%2Fjwt%2F1.0%2Fbearer&assertion=eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiI0MjYwNjE5OTE1NzktYXEzMHBhcmVsYmpsb3BrMjlqcXFvdjhsdWhic2o3YjdAZGV2ZWxvcGVyLmdzZXJ2aWNlYWNjb3VudC5jb20iLCJzY29wZSI6Imh0dHBzOi8vd3d3Lmdvb2dsZWFwaXMuY29tL2F1dGgvZHJpdmUiLCJhdWQiOiJodHRwczovL2FjY291bnRzLmdvb2dsZS5jb20vby9vYXV0aDIvdG9rZW4iLCJleHAiOjEzNjAwNjA0NDMsImlhdCI6MTM2MDA1Njg0M30.dk7Vdu-................-LIw0sFrVko-VWL7-elhz59VQcU_.........

第一个答案:

HTTP/1.1 200 OK
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: Fri, 01 Jan 1990 00:00:00 GMT
Date: Tue, 05 Feb 2013 09:34:03 GMT
Content-Type: application/json
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
Server: GSE
Content-Length: 128

{
  "access_token" : "ya29.AHES6ZS-...........-a8HRHfMZ-NxXekx",
  "token_type" : "Bearer",
  "expires_in" : 3600
}

第二个请求:

GET https://www.googleapis.com/drive/v2/files?alt=json&fields=items(id%2Ctitle)%2CnextPageToken&prettyPrint=true&maxResults=2000&q=title%20%3D%20'87BC1CAE-A01C-43A1-BB21-BE3DA6D6C7DC' HTTP/1.1
Authorization: Bearer ya29.AHES6ZS-...........-a8HRHfMZ-NxXekx
Content-Type: application/json; charset=utf-8
User-Agent: TestGoogleApi google-api-dotnet-client/ Win32NT/6.1.7600.0 (gzip)
Host: www.googleapis.com
Accept-Encoding: gzip, deflate

第二个答案:

HTTP/1.1 200 OK
Expires: Tue, 05 Feb 2013 09:34:04 GMT
Date: Tue, 05 Feb 2013 09:34:04 GMT
Cache-Control: private, max-age=0, must-revalidate, no-transform
ETag: "Q0cVodxX8sh4vfxZTlOyWcmmc0k/vyGp6PvFo4RvsFtPoIWeCReyIC8"
Content-Type: application/json; charset=UTF-8
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
Content-Length: 17
Server: GSE

{
 "items": []
}

如您所见,项目数为 0。

如果我通过 Web 界面 https://developers.google.com/drive/v2/reference/files/list提出相同的请求,它会给我 2 个项目,应该是。

这是用https://developers.google.com/oauthplayground/

GET /drive/v2/files?alt=json&fields=items(id%2Ctitle)%2CnextPageToken&prettyPrint=true&maxResults=2000&q=title%20%3D%20'87BC1CAE-A01C-43A1-BB21-BE3DA6D6C7DC' HTTP/1.1
Host: www.googleapis.com
Content-length: 0
Authorization: OAuth ya29.AHES6ZSDr7bDFMQxjR........ObzyEhkzQmN

HTTP/1.1 200 OK
Content-length: 220
Via: HTTP/1.1 GWA
Content-location: https://www.googleapis.com/drive/v2/files?alt=json&fields=items(id%2Ctitle)%2CnextPageToken&prettyPrint=true&maxResults=2000&q=title%20%3D%20'87BC1CAE-A01C-43A1-BB21-BE3DA6D6C7DC'
X-content-type-options: nosniff
Etag: "Q0cVodxX8sh4vfxZTlOyWcmmc0k/oUg90Ml6_g3EsaXCogiJOEMkZ-M"
X-google-cache-control: remote-fetch
-content-encoding: gzip
Server: GSE
Reason: OK
X-xss-protection: 1; mode=block
Cache-control: private, max-age=0, must-revalidate, no-transform
Date: Tue, 05 Feb 2013 15:30:09 GMT
X-frame-options: SAMEORIGIN
Content-type: application/json; charset=UTF-8
Expires: Tue, 05 Feb 2013 15:30:09 GMT
{
 "items": [
  {
   "id": "0B5XhOf....VlTUTA",
   "title": "87BC1CAE-A01C-43A1-BB21-BE3DA6D6C7DC"
  },
  {
   "id": "0B5XhO....EbGRLVVVReGM",
   "title": "87BC1CAE-A01C-43A1-BB21-BE3DA6D6C7DC"
  }
 ]
}

非常感谢,非常感谢您的帮助

4

1 回答 1

0

看起来混乱是由于您在代码中使用了服务帐户,但将结果与您在 Web 界面中看到的结果进行了比较。

使用 Web 界面或 OAuth Playground 时,您授权应用程序使用您的个人帐户,而不是服务帐户。服务帐户无权访问您有权访问的相同文件,因为它实际上是应用程序拥有的帐户。

如果您想使用服务帐户来访问您的文件,您应该按照https://developers.google.com/drive/delegation中的说明执行域范围的委派

这仍然无法解释为什么某些查询返回结果而其他查询不返回结果,但请在不使用服务帐户的情况下重试所有操作,如果仍然无法按预期工作,请更新问题。

于 2013-02-13T18:05:27.177 回答