IBM Bluemix身份validation令牌始终无效
我已经写了一个小Python脚本,它应该使用Python的requests
模块来访问这里描述的IBM Bluemix容器API。
这是脚本:
""" 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仪表板页面中的名字加上姓,两个
"
- 我用于注册的完整电子邮件,这也被标记为
User:
当我做cf login
- 我做
cf login
时显示的组织名称
至于--space-id
和authentication令牌--oauth-token
: --oauth-token
本身的文档告诉我按照我的方式得到它们。 我也尝试通过复制和粘贴我使用的子命令的输出来提供它们,并且从脚本中打印出来,以确保它们全部进入脚本内部。 它确实。
我还从API文档中获得了--request-url
参数值,通过使用“Try it”button并从显示的curl
命令复制URL,这是用来尝试的,所以也应该是正确的。
但是,在每个需要身份validation的请求中(例如列出空间中所有图像的请求),我都会从API获得401
响应,告诉我身份validation令牌无效,我应该尝试执行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'}
(我打破了界限,使它在这里更具可读性)
所以我想知道我在做什么错误的令牌。 如何使用身份validation令牌创build正确的请求?
编辑#1
我还使用https://jwt.io/解码了JSON Web令牌。 它显示我input的用户名确实是该令牌包含的用户名。
编辑#2
我也检查了(再次)的空间可用:
正如你所看到的,这两个地区都有一个名为dev
的空间。
看起来你正在混合英国和美国的地区 – 我看到了eu-gb.bluemix.net和美国API服务器的证书。 这可能是问题所在。 我在美国只有一个空间,所以我不能testing这个,但是…
如果您在英国使用空间,请使用containers-api.eu-gb.bluemix.net证书和英国地区api服务器: https : //containers-api.eu-gb.bluemix.net
如果您在美国使用空间,请使用containers-api.ng.bluemix.net的证书并使用该API服务器。