IBM Bluemix认证令牌始终无效

Zelphir冷钢

我编写了一个小的Python脚本,该脚本应该使用Python的模块访问此处描述的IBM Bluemix容器API requests

这是脚本:

"""
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本身的文档告诉我按照我的方式获得它们。我还尝试通过复制和粘贴所用子命令的输出来提供它们,并从脚本中打印它们,以确保所有内容都进入脚本中。是的

--request-url参数值我也得到了来自API文档,通过使用“试用”按钮,然后复制该网址curl显示的命令,这是用来努力,这样也应该是正确的。

然而,在每一个请求,这需要身份验证,例如一个在空间列出所有图片,我收到了401来自API响应,告诉我的身份验证令牌是无效的,我应该尝试做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

我还使用https://jwt.io/解码了JSON Web令牌它表明我输入的用户名的确确实是令牌也包含的用户名。

编辑#2

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

IBM Bluemix仪表板空间概述

如您所见,dev在两个区域中都有一个命名的空间

约翰·麦克米金

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

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

如果您在美国使用空格,请对container-api.ng.bluemix.net使用cert并使用该API服务器。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章