错误400请求地址b时,参数无效

2024-04-19 15:21:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在设置Google联系人carddavapi客户端。你知道吗

OAuth 2.0使用oauth2client
使用requests请求。你知道吗

from oauth2client import file, client, tools
import requests

SCOPES = 'https://www.googleapis.com/auth/carddav'
store = file.Storage('credentials.json')
creds = store.get()
if not creds or creds.invalid:
    flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
    creds = tools.run_flow(flow, store)
print(creds.access_token)

hed = {'Authorization': 'Bearer ' + creds.access_token}

response = requests.request('PROPFIND', 'https://www.googleapis.com/.well-known/carddav', headers=hed, allow_redirects=False)

if response.status_code == 301:
    location = response.headers['location']
    response = requests.request('PROPFIND', 'https://www.googleapis.com' + location, headers=hed)
    print(response.text)

但是当我请求获取地址簿的url时(我从第一个请求的Location头获取),它返回错误:

{
  "error": {
    "code": 400,
    "message": "Request contains an invalid argument.",
    "status": "INVALID_ARGUMENT"
  }
}

完整请求信息

第一个请求

requests.request('PROPFIND', 'https://www.googleapis.com/.well-known/carddav', headers=hed, allow_redirects=False)

REQUEST
=======
endpoint: PROPFIND https://www.googleapis.com/.well-known/carddav
headers:
  User-Agent: python-requests/2.22.0
  Accept-Encoding: gzip, deflate
  Accept: */*
  Connection: keep-alive
  Authorization: Bearer ya29.***********************************************
  Content-Length: 0
=======
RESPONSE
========
status_code: 301
headers:
  Content-Type: text/plain; charset=UTF-8
  X-XSS-Protection: 1; mode=block
  X-Content-Type-Options: nosniff
  Expires: Mon, 01 Jan 1990 00:00:00 GMT
  Cache-Control: no-cache, no-store, max-age=0, must-revalidate
  X-Frame-Options: SAMEORIGIN
  Location: /carddav/v1/principals/<my_email>/lists/default/
  Pragma: no-cache
  Vary: Origin, X-Origin, Referer
  Date: Fri, 21 Jun 2019 11:43:23 GMT
  Server: ESF
  Content-Length: 0
  Alt-Svc: quic=":443"; ma=2592000; v="46,44,43,39"
========

第二次请求

response = requests.request('PROPFIND', 'https://www.googleapis.com' + location, headers=hed)

REQUEST
=======
endpoint: PROPFIND https://www.googleapis.com/carddav/v1/principals/<my_email>/lists/default/
headers:
  User-Agent: python-requests/2.22.0
  Accept-Encoding: gzip, deflate
  Accept: */*
  Connection: keep-alive
  Authorization: Bearer ya29.***********************************************
  Content-Length: 0
=======
RESPONSE
========
status_code: 400
headers:
  Vary: Origin, X-Origin, Referer
  Content-Type: application/json; charset=UTF-8
  Date: Fri, 21 Jun 2019 11:43:23 GMT
  Server: ESF
  Content-Length: 127
  X-XSS-Protection: 0
  X-Frame-Options: SAMEORIGIN
  X-Content-Type-Options: nosniff
  Alt-Svc: quic=":443"; ma=2592000; v="46,44,43,39"
body:
  {
    "error": {
      "code": 400,
      "message": "Request contains an invalid argument.",
      "status": "INVALID_ARGUMENT"
    }
  }
========

Tags: storehttpscomresponsewwwstatuscodecontent
1条回答
网友
1楼 · 发布于 2024-04-19 15:21:29

简而言之,PROPFIND方法是泛型的,必须包含一个指定服务器应该返回什么信息的主体。您必须在请求体中传递一个XML负载,该负载标识您请求的属性。你知道吗

获取地址簿URI

根据Google's CardDav API docs,您的第一个请求是完美的,它会将您重定向到当前用户的地址簿资源。以下是谷歌对下一步的描述:

Your client program can then discover the principal address book by performing a PROPFIND on the addressbook-home-set and looking for the addressbook and collection resources.

让我解开这个:您的第二个请求应该查询在您从第一个请求获得的位置找到的用户资源的列表。要正确执行此查询,您需要传递带有PROPFIND请求的XML正文,如下所示:

    PROPFIND https://www.googleapis.com/carddav/v1/principals/<my_email>/lists/default/
    Authorization: Bearer ya29.***********************************************
    Depth: 1
    Content-Type: application/xml; charset=utf-8

    <D:propfind xmlns:D="DAV:">
      <D:prop>
         <D:resourcetype />
         <D:displayname />
      </D:prop>
    </D:propfind>

在这里,您可以指定希望服务器响应的属性。指定resourcetype属性是因为您只对包含联系人的addressbookcollection资源感兴趣。你知道吗

此请求将返回资源的uri列表,您可以从中选择资源类型为addressbookcollection的任何uri。你知道吗

此时,您没有任何联系人,甚至没有联系人的uri。您拥有用户通讯簿或联系人集合的URI列表。(通常只有一个,但也可能有很多。)

您没有询问如何获取用户的联系人,但我假设这是您的最终目标,并将继续后续步骤。

获取联系人URI

您的下一组请求将查询每个地址簿uri,以查找其联系人的uri。循环上一个查询的每个结果,并使用此负载对URI发出另一个PROPFIND请求:

    REPORT <addressbook_uri>
    Authorization: Bearer ya29.***********************************************
    Content-Type: application/xml; charset=utf-8

    <D:propfind xmlns:D="DAV:">
      <D:prop>
        <D:getetag />
        <D:getcontenttype />
      </D:prop>
    </D:propfind>

这里我们查询每个项目的内容类型,以便确定它是否是VCard类型。电子名片是合法的联系记录。你知道吗

现在您可以通过contenttype == 'text/vcard'过滤这组结果,以获得指向用户通讯簿中每个联系人的uri的新列表。你知道吗

哦,天哪,我们就快到了。

获取联系人vCard

最后,将uri列表组合成实际的联系人数据,并从服务器查询数据。你知道吗

在这里,您将发出addressbook-multigetREPORT请求来检索列表中的一批联系人。谷歌没有说明你的请求中可能包含多少联系人URI。我通常一次只能提出几百个请求。你知道吗

例如

    REPORT <addressbook_uri>
    Authorization: Bearer ya29.***********************************************
    Content-Type: application/xml; charset=utf-8

    <C:addressbook-multiget xmlns:D="DAV:" xmlns:C="urn:ietf:params:xml:ns:carddav">
      <D:prop>
        <D:getetag/>
        <C:address-data>
          <C:allprop/>
        </C:address-data>
      </D:prop>
      <D:href>/carddav/v1/principals/<my_email>/lists/default/contact1.vcf</D:href>
      <D:href>/carddav/v1/principals/<my_email>/lists/default/contact2.vcf</D:href>
      ...
    </C:addressbook-multiget>

响应将包含每个联系人的VCard数据,打包在XML中。解析出XML文本,然后解析VCard数据,最终检索您的联系方式。你知道吗

完成!


资源:

相关问题 更多 >