2017-02-10 12 views
0

Pythonのrequestsモジュールを使用してhereと記載されているIBM BluemixコンテナAPIにアクセスするはずの少しのPythonスクリプトを作成しました。ここでIBM Bluemix認証トークンは常に無効

スクリプトです:

""" 
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() 

私が呼んこのスクリプト:

  • 最初の名前と姓:私はいくつかのことを試してみました--userパラメータについて

    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 
    

    両者の間のスペースとしてIBM Bluemixダッシュボード・ページから"

  • また、私は私が行うときに表示されるcf login
  • 組織名、やるときUser:をラベル付けされて、私は登録するために使用完全な電子メール、--space-idおよび認証トークン--oauth-token用としてcf login

: 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ウェブトークンをデコード。これは、入力したユーザー名が実際にトークンに含まれていることを示しています。

編集#2

私はまた、使用可能な領域のために(再び)をチェック:

IBM Bluemix Dashboard spaces overview

あなたは、両方の領域でdevという名前のスペースがある見ることができるように。

答えて

1

あなたは英国と米国の地域を混在させているようです - 私はeu-bb.bluemix.netとUS APIサーバーの証明書を見ます。それは問題かもしれません。私はアメリカにしかスペースがないので、これはテストできませんが...

英国のスペースで作業する場合は、containers-api.eu-gb.bluemix.net certと英国の地域APIサーバー:https://containers-api.eu-gb.bluemix.net

米国のスペースで作業する場合は、containers-api.ng.bluemix.netのcertを使用し、そのAPIサーバーを使用してください。

+0

これで解決しました。私は1地域にログインしていましたが、別の地域のAPIを使用していました。次に、 'cf oauth-token'がリクエストURLのために間違ったトークンを返しました。また、 'cf oauth-token'の完全な出力を使用することはできないが、' bearer '(ベアラスペース)の後ろの全てを使うことができることも知っておく必要があります。 – Zelphir

関連する問題