認証

Embedded Python ExecutionのREST APIなどのOracle Machine Learning REST APIでは、トークンを使用してOracle Machine Learningユーザーを認証します。

Oracle Machine Learning for Python REST API Embedded Python Execution関数にはHTTP経由でアクセスするため、認証のために次の情報を指定する必要があります。
  • Oracle Machine Learning Cloud Serviceアカウントのユーザー名とパスワード。

トークンを認証および取得するには、-dオプションを指定してcURLを使用し、Oracle Machine Learning User Management Cloud Service RESTエンドポイント/oauth2/v1/tokenに対してOracle Machine Learningアカウントのユーザー名とパスワードを渡します。

Oracle Machine Learning User Management Cloud Service RESTエンドポイント/oauth2/v1/tokenにリクエストを送信するcURLコマンドの例:

$ curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' 
-d '{"grant_type":"password", "username":"'${username}'", "password":"'${password}'"}' 
"<oml-cloud-service-location-url>/omlusers/api/oauth2/v1/token"

この例では次の値が使用されています。

  • usernameは、OML DEVELOPERロールを持つOracle Machine Learningプラットフォーム・ユーザーのユーザー名です。
  • passwordは、ユーザー名のパスワードです。
  • oml-cloud-service-location-urlは、テナンシID、データベース名、場所名を含むOracle Machine Learning User Management Cloud ServiceインスタンスURLのRESTサーバー部分を含むURLです。omlserver URLは、Oracle Autonomous Databaseインスタンスのサービス・コンソールの「開発」タブから取得できます。

    ノート:

    トークンの有効期限が切れると、OML ServicesのRESTエンドポイントへのすべてのコールは、HTTPエラー: HTTP/1.1 401 Unauthorizedとともにトークンの有効期限が切れたことを示すメッセージを返します。

トークンのリフレッシュ

トークンが生成されると、そのトークンは60分間有効です。その後に期限切れになります。期限切れになる前にトークンをリフレッシュするには、次のコードを実行します:

$ curl -i -k -X POST --header 'Content-Type: application/json' 
--header 'Accept: application/json' --header 
"Authorization: Bearer ${token}" -d '{"grant_type":"refresh_token", "refresh_token":"'${token}'"}' 
"${omlservice}/omlusers/api/oauth2/v1/token"

HTTP/1.0 200 Connection Established

HTTP/1.1 100 Continue

HTTP/1.1 200 OK
Date: Thu, 07 Nov 2024 20:55:54 GMT
Content-Type: application/json
Content-Length: 1266
Connection: keep-alive
Strict-Transport-Security: max-age=31536000; includeSubDomains
X-Content-Type-Options: nosniff
Cache-Control: no-cache, no-store, must-revalidate
Pragma: no-cache
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1;mode=block
Content-Security-Policy: default-src 'none'; connect-src 'self'; font-src 'self' static.oracle.com; img-src 'self' data: static.oracle.com; media-src 'none'; object-src 'none'; script-src 'self' static.oracle.com 'unsafe-inline' 'unsafe-eval'; style-src 'self' static.oracle.com 'unsafe-inline'; frame-ancestors 'none'

{"accessToken":"eyJh........qiUFxJ8Y","tokenType":null,"expiresIn":3600}

トークンの取消し

トークンは、いつでも取り消して無効化し、それ以降のアクセスを防止できます。次のcURLコマンドは、トークンを取り消す方法を示しています:

curl -X POST --header 'Content-Type: application/json' 
--header 'Accept: application/json' -d '{"grant_type":"password", "username":"'${username}'", "password":"'${password}'"}' 
"${omlservice}/omlusers/api/oauth2/v1/token/revoke"