概要
本記事では、Watson Discovery APIをcurlを用いて叩くサンプルをご紹介します。
サンプルは、VPC内のインスタンスよりclient2siteのopenVPN経由でコマンド実行しています。
(さまざま試行錯誤してしまったので、合わせて記載します。。)
結論
以下のように記載します。
(GET/POSTはコマンドによって異なります。後述のドキュメントに従ってください。)
$ curl -X GET -u “apikey”:”[APIキー]" "https://api.jp-tok.discovery.watson.cloud.ibm.com/instances/xxxxxxxxxxxxxxxxxxxxxx"
※ []はいらないです
注意
バージョンが異なる
最終的に解決したのがバージョンミスでした。
Watson Discoveryは、バージョンが2種類あり、それぞれのAPIが微妙に異なります。本記事のWDはバージョン2です。
バージョン1の場合のAPI docsはこちら
バージョン2の場合のAPI docsはこちら
IBM Cloudポータルからは確認できないようで、サポートのチャットにEndpointのURLを共有したら調べてくれました。
401が返ってくる
{
"code" : 401,
"error" : "Invalid credentials"
}
以下を参考にしても分からず、、
https://cloud.ibm.com/docs/watson?topic=watson-authorization-error
415が返ってくる
試しに-X POST
を追加して試した例です。
(その後"Content-Type: Application/json"
形式で試してみましたが、401に逆戻り、、)
"status_code" : 415,
"errors" : [ {
"code" : "discovery_request_failed",
"message" : "Unsupported Media Type",
"more_info" : "https://cloud.ibm.com/docs/discovery-data"
} ],
"trace" : "xxxxxxxxxxxxxxxxxxxxxxxxx"
}
API サンプル実行
プロジェクトのリスト
$ curl -X GET -u "apikey":"[APIキー]" "https://api.jp-tok.discovery.watson.cloud.ibm.com/instances/xxxxxxxxxxxxxxxxxxxxxx/v2/projects?version=2020-08-30"
{
"projects" : [ {
"project_id" : "xxx-xxx-xxx-xxx",
"type" : "document_retrieval",
"name" : "test-project",
"collection_count" : 1
}, {
.
.
.
}
collectionsのリスト
$ curl -X GET -u "apikey":"[APIキー]" "https://api.jp-tok.discovery.watson.cloud.ibm.com/instances/xxxxxxxxxxxxxxxxxxxxxx/v2/projects/[プロジェクト ID]/collections?version=2020-08-30"
{
"collections" : [ {
"name" : "Sample Collection",
"collection_id" : "xxx-xxx-xxx-xxx"
}
}
結論
いち早くバージョンを確認しましょう。
2022年6月現在、IBM Cloudポータルでは確認ができません。
すでに作成しておりバージョンを確認できない場合は、サポートチャットにてエンドポイントURLなどを共有しますと調べてくれます。