我正在设置 Google Contacts CardDAV API 客户端。
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"
}
}
========