1. 概要¶
富岳WebAPI SDKは、富岳WebAPIを利用するシンプルなPythonライブラリです。
Python3.7で動作します。
2. インストール¶
富岳WebAPI SDKは、pipコマンドでインストールできます。
$ pip install fugaku-x.y.z.tar.gz
Requirements: >= Python 3.7
Project dependencies installed by pip:
requests
python-magic
six
3. Getting Started¶
WebAPIの実行には、OAuth 2.0の認可コード付与のフローとOpenID Connectプロトコルに基づいたアクセストークンが必要です。
SDKを使うには、WebAPI利用手引書 の認可についてに従って、認可コードを取得してください。
認可コードを用いてクライアントを作成する例を以下に示します。
""" client_id, client_secret, redirect_url
"""
CLIENT_CONFIG = {
"client_id": "CLINET_ID",
"client_secret": "CIENT_SECRET",
"redirect_uri": "REDIRECT_RUI",
}
metadata_url = "https://idp.fugaku.r-ccs.riken.jp/auth/realms/op/.well-known/openid-configuration"
proxies = {
"http": None,
"https": None
}
client = fugaku.Client(auth_code=authorization_code, client_config=CLIENT_CONFIG,
provider_metadata_url=metadata_url,
request_config={"proxies": proxies})
if not client or not client._access_token:
print(f"No access token.")
client = None
else:
print(f"Access token. {client._access_token}")
パラメータ
パラメータ
型
説明
auth_code
String
(必須)auth_codeは、ブラウザを利用して取得してください。
client_config
Dictionary
(必須)client_configはアクセストークンの取得に使います。
provider_metadata_url
String
IdPのメタデータを取得するURL
authorization_endpoint
String
認可エンドポイントURL
token_endpoint
String
アクセストークンエンドポイントURL
request_config
Dictionary
HTTP(s)リクエストに用いるその他の設定
認証コードを取得する簡単な例を次に示します。
print("Please access the following URL.")
print(f"https://idp.fugaku.r-ccs.riken.jp/auth/realms/op/protocol/openid-connect/auth?response_type=code&scope=openid&client_id={CLIENT_CONFIG['client_id']}&redirect_uri={quote(CLIENT_CONFIG['redirect_uri'])}")
print("Paste Browser URL:")
line = sys.stdin.readline().strip()
pos = line.find("&code=")
if pos > 0:
authorization_code = line[pos+6:]
pos = authorization_code.find("&")
if pos > 0:
authorization_code = authorization_code[:pos-1]
4. Error handling¶
Fugaku WebAPI SDKの例外は、fugaku.errorパッケージに定義されています。クライアントは同じ例外クラスを利用します。例外の基底クラスは、FugakuClientErrorです。
try:
client = fugaku.Client(any_params)
except fugaku.NoTokenEndpoint as e:
# Neither provider_metadata_url nor token_endpoint is not set to constructor parameter.
logger.exception("exeption:{0}".format(e))
try:
response = client.any_api(any_params)
except fugaku.NoAccessCode as e:
# access_code is not set, or expired.
logger.exception("exeption:{0}".format(e))