IBM Bluemix身份验证令牌始终无效

时间:2017-02-10 12:11:49

标签: python docker python-requests containers ibm-cloud

我编写了一个Python脚本,它应该使用Python的requests模块访问描述为day的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和身份验证令牌--oauth-token:APi本身的文档告诉我按照我的方式来实现它们。我也试过通过copy&amp; amp;粘贴我使用的子命令的输出,并从脚本打印它们,只是为了确保它们都进入脚本。确实如此。

我从API文档中获得了--request-url参数值,使用“试一试”按钮并复制显示的curl命令中的URL,这是用来尝试的,所以应该也是对的。

但是,在每个需要身份验证的请求中,例如用于列出空间中所有图像的请求,我从API获得401响应,告诉我身份验证令牌无效且我应该尝试再次执行cf logincf 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

我还使用here解码了JSON网络令牌。它显示我输入的用户名确实也是令牌包含的用户名。

编辑#2

我还(再次)检查了可用空间:

https://jwt.io/

如您所见,两个地区都有一个名为dev的空间。

1 个答案:

答案 0 :(得分:1)

看起来你正在混合英国和美国地区 - 我看到了eu-gb.bluemix.net和美国API服务器的证书。这可能是问题所在。我在美国只有一个空间,所以我无法测试这个,但是......

如果您正在使用英国的空间,请使用containers-api.eu-gb.bluemix.net证书和英国地区的api服务器:https://containers-api.eu-gb.bluemix.net

如果您正在使用美国的空间,请使用cert for containers-api.ng.bluemix.net并使用该API服务器。