我正在尝试在python脚本中调用Bitbucket API
来检索一些数据。
我使用key/secret
对Bitbucket OAuth
。
data = { 'grant_type': 'client_credentials'}
response = requests.post('https://bitbucket.org/site/oauth2/access_token', data=data, auth=(key, secret))
print(response.json())
access_token = response.json()['access_token']
print(access_token)
groups = requests.get("https://api.bitbucket.org/1.0/groups/myaccount/", headers={"Bearer %s" %access_token})
print(groups.json())
这给了我一个错误:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1997, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1985, in wsgi_app
response = self.handle_exception(e)
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1540, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.6/dist-packages/flask/_compat.py", line 33, in reraise
raise value
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.6/dist-packages/flask/_compat.py", line 33, in reraise
raise value
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/vagrant/projects/tools/axb-dsi-api/app/bitbucket_connector.py", line 35, in get_groups
groups = requests.get("https://api.bitbucket.org/1.0/groups/myaccount/", headers={"Bearer %s" %access_token})
File "/usr/local/lib/python3.6/dist-packages/requests/api.py", line 72, in get
return request('get', url, params=params, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/requests/api.py", line 58, in request
return session.request(method=method, url=url, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/requests/sessions.py", line 494, in request
prep = self.prepare_request(req)
File "/usr/local/lib/python3.6/dist-packages/requests/sessions.py", line 437, in prepare_request
hooks=merge_hooks(request.hooks, self.hooks),
File "/usr/local/lib/python3.6/dist-packages/requests/models.py", line 306, in prepare
self.prepare_headers(headers)
File "/usr/local/lib/python3.6/dist-packages/requests/models.py", line 438, in prepare_headers
for header in headers.items():
AttributeError: 'set' object has no attribute 'items'
我们如何使用访问令牌来调用BitBucket API REST?
编辑 将标头更正为json后:
现在是说
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1997, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1985, in wsgi_app
response = self.handle_exception(e)
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1540, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.6/dist-packages/flask/_compat.py", line 33, in reraise
raise value
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.6/dist-packages/flask/_compat.py", line 33, in reraise
raise value
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/vagrant/projects/tools/axb-dsi-api/app/bitbucket_connector.py", line 36, in get_groups
print(groups.json())
File "/usr/local/lib/python3.6/dist-packages/requests/models.py", line 892, in json
return complexjson.loads(self.text, **kwargs)
File "/usr/lib/python3.6/json/__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.6/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.6/json/decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
答案 0 :(得分:0)
requests.get()的headers参数应该是一个字典,并且您要发送集合而不是字典,请检查您的代码是headers={"Bearer %s" %access_token}
,将其视为集合,您可以在python上执行此代码并检查。 / p>
您可能需要在下面的示例中使用key:value检查:
groups = requests.get("https://api.bitbucket.org/1.0/groups/myaccount/", headers={'access_token' : "Bearer %s" %access_token})
答案 1 :(得分:0)
通过这样传递访问令牌,我成功地从Bitbucket API中检索了数据:
groups = requests.get("https://api.bitbucket.org/1.0/groups/myaccount/access_token={%s}"%access_token)
答案 2 :(得分:0)
在标题中使用“授权”作为密钥,而不是“ acess_token”,请尝试下面的代码。
groups = requests.get("https://api.bitbucket.org/1.0/groups/myaccount/", headers={'Authorization' : "Bearer %s" %access_token})