Ubuntu環境でIBM Watson OpenScale APIに接続する際に、SSL証明書の検証エラーが発生することがあります。その際の解決策として、REQUESTS_CA_BUNDLE
環境変数を設定する方法について紹介します。
エラーの発生
以下のコードを実行すると、SSLCertVerificationError
が発生しました。
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
from ibm_watson_openscale import APIClient
CLOUD_API_KEY = 'your_api_key_here'
authenticator = IAMAuthenticator(apikey=CLOUD_API_KEY)
client = APIClient(authenticator=authenticator)
print(client.version)
発生したエラー
SSLCertVerificationError: HTTPSConnectionPool(host='api.aiopenscale.cloud.ibm.com', port=443):
Max retries exceeded with url: /openscale/... (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate')))
このエラーは、ローカルのSSL証明書が正しく設定されていないことが原因です。
解決策: REQUESTS_CA_BUNDLE
の設定
Pythonの certifi パッケージを利用して、適切な証明書バンドルを指定します。
修正後のコード
import certifi
import os
os.environ["REQUESTS_CA_BUNDLE"] = certifi.where()
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
from ibm_watson_openscale import APIClient
CLOUD_API_KEY = 'your_api_key_here'
authenticator = IAMAuthenticator(apikey=CLOUD_API_KEY)
client = APIClient(authenticator=authenticator)
print(client.version)
解説
-
certifi.where()
をREQUESTS_CA_BUNDLE
に設定することで、Pythonのrequests
ライブラリが適切な証明書を使用するようになります。 -
certifi
はPythonの標準的なSSL証明書バンドルを提供するライブラリです。 -
os.environ["REQUESTS_CA_BUNDLE"] = certifi.where()
を実行することで、環境変数を適切に設定できます。
まとめ
IBM Watson OpenScale API を Ubuntu 環境で利用する際に SSLCertVerificationError
が発生した場合、certifi
を利用して REQUESTS_CA_BUNDLE
を設定することで解決できます。
この方法を活用して、スムーズに Watson OpenScale API を利用しましょう!