【发布时间】:2017-02-10 12:11:48
【问题描述】:
我编写了一个小 Python 脚本,它应该使用 Python 的 requests 模块访问描述的 IBM Bluemix 容器 API here。
这是脚本:
"""
You can run this script for example as follows:
python test-script.py \
--user <YOUR IBM BLUEMIX USER NAME> \
--ca-certificate <PATH TO YOUR ca.pem> \
--oauth-token "$(cf oauth-token)" \
--space-id "$(cf space dev --guid)" \
--request-url https://containers-api.ng.bluemix.net/v3/containers/version \
--method GET
The request-url is an EXAMPLE!
"""
import argparse
import getpass
import requests
import json
# for parsing arguments provided on command line
parser = argparse.ArgumentParser()
parser.add_argument(
'-u', '--user',
required=True,
help='Specify the username for IBM Bluemix.',
)
parser.add_argument(
'-c', '--ca-certificate',
required=True,
help='Specify the location of the certificate of the trusted certification authority (ca.pem).'
)
parser.add_argument(
'-t', '--oauth-token',
required=True,
help='Specify your IBM Bluemix oauth-token.'
)
parser.add_argument(
'-s', '--space-id',
required=True,
help='Specify your IBM Bluemix space id (cf space <your space> --guid). Beware, the space needs to be available in the region you are logged in to (US South, United Kindom).'
)
parser.add_argument(
'-l', '--request-url',
required=True,
default='https://containers-api.ng.bluemix.net/v3/containers/version',
help='Specify the URL you want to send the request to.'
)
parser.add_argument(
'-m', '--method',
default='GET',
help='Specify the HTTP method.'
)
args = parser.parse_args()
def run_script():
password = getpass.getpass(prompt='IBM Bluemix password:')
# for now hard coded
headers = {
'Accept': 'application/json',
'X-Auth-Token': args.oauth_token,
'X-Auth-Project-Id': args.space_id
}
# first we get the appropriate HTTP request method
request_method = getattr(requests, args.method.lower())
# then we try to make the request with that method
try:
response = request_method(
args.request_url,
auth=(args.user, password),
data=None, # expects a dict or json.dumps return type
verify=args.ca_certificate,
headers=headers
)
print(response)
print(response.json())
except Exception as exc:
print('ERROR!')
def main():
try:
run_script()
except KeyboardInterrupt as exc:
exit('Interrupted, exiting ...')
main()
我调用的这个脚本:
python script.py \
--user <IBM Bluemix login name> \
--ca-certificate /home/<USER>/.ice/certs/containers-api.eu-gb.bluemix.net/<ID>/ca.pem \
--oauth-token "$(cf oauth-token)" \
--space-id "$(cf space dev --guid)" \
--request-url https://containers-api.ng.bluemix.net/v3/images/json \
--method GET
对于--user 参数我尝试了几件事:
- IBM Bluemix 仪表板页面中的名字和姓氏,中间有一个空格作为两个
"之间的字符串 - 我注册时使用的完整电子邮件,当我注册
cf login时也标记为User: - 组织名称,当我
cf login时显示
至于--space-id 和身份验证令牌--oauth-token:API 本身的文档告诉我按照我的方式获取它们。我还尝试通过复制和粘贴我使用的子命令的输出来提供它们,并从脚本中打印它们,以确保它们都在脚本中。确实如此。
--request-url 参数值我也是从 API 文档中获得的,通过使用“试用”按钮并从显示的 curl 命令中复制 URL,该命令用于尝试,所以这也应该是正确的。
但是,在每个需要身份验证的请求中,例如列出空间中所有图像的请求,我从 API 收到 401 响应,告诉我身份验证令牌无效,我应该尝试再次执行cf login 和cf ic init。这样做,不会更改我的令牌或任何东西,也不会修复错误。
这是我得到的示例响应:
<Response [401]>
{'code': 'IC5097E',
'description': "Authentication was not successful: bearer <SOME ID> Please login via 'cf login ...' + 'cf ic init ...' and try again.",
'environment': 'prod-dal09',
'host_id': '176',
'incident_id': '<SOME ID>',
'name': 'InvalidToken',
'rc': '401',
'type': 'Infrastructure'}
(我打破了界限,使其在 SO 上更具可读性)
所以我想知道我在使用令牌时做错了什么。如何使用身份验证令牌创建正确的请求?
编辑#1
我还使用 https://jwt.io/ 解码了 JSON Web 令牌。它表明我输入的用户名确实也是令牌包含的用户名。
编辑#2
我还(再次)检查了可用空间:
如您所见,两个区域都有一个名为 dev 的空间。
【问题讨论】:
标签: python docker python-requests containers ibm-cloud