部署在SAP Cloud Platform CloudFoundry環境的應用如何消費SAP Leonardo機器學習API

i042416發表於2019-07-14

Jerry的前一篇文章  如何在Web應用裡消費SAP Leonardo的機器學習API  裡介紹的例子是Neo測試環境的Web應用消費sandbox版本的機器學習API,url如下:

https://sandbox.api.sap.com/ml

本文介紹一個部署在SAP Cloud Platform CloudFoundry環境下的應用,如何消費SAP Leonardo上的機器學習API。

登入SAP Cloud Platform Cockpit,進入CloudFoundry環境的Service Marketplace,找到SAP Leonardo機器學習的服務,單擊該服務的超連結進入明細頁面:

部署在SAP Cloud Platform CloudFoundry環境的應用如何消費SAP Leonardo機器學習API

建立一個新的服務例項:

部署在SAP Cloud Platform CloudFoundry環境的應用如何消費SAP Leonardo機器學習API

Service Plan就選預設的standard:

部署在SAP Cloud Platform CloudFoundry環境的應用如何消費SAP Leonardo機器學習API

給這個服務例項取個名字:

部署在SAP Cloud Platform CloudFoundry環境的應用如何消費SAP Leonardo機器學習API

單擊這個建立好的服務例項,然後建立一個新的Service Key:

部署在SAP Cloud Platform CloudFoundry環境的應用如何消費SAP Leonardo機器學習API
部署在SAP Cloud Platform CloudFoundry環境的應用如何消費SAP Leonardo機器學習API

給Service Key也取個名字。

部署在SAP Cloud Platform CloudFoundry環境的應用如何消費SAP Leonardo機器學習API

我們通過建立Service instance進而建立Service Key的目的,是為了得到下圖的clientid和clientsecret。

部署在SAP Cloud Platform CloudFoundry環境的應用如何消費SAP Leonardo機器學習API

而我們拿到clientid和clientsecret,是為了用它們換取OAuth2.0協議裡的access token.

關於更多clientid和clientsecret基於OAuth2.0換取access token 的細節,請參考Jerry的文章: OAuth 2.0協議在SAP產品中的應用

除了clientid和clientsecret外,我們還能在service key裡得到當前這個SAP Cloud Platform CloudFoundry環境上所有可用的機器學習API的url,它們全部維護在節點serviceurls裡:

部署在SAP Cloud Platform CloudFoundry環境的應用如何消費SAP Leonardo機器學習API

注意觀察,此時的IMAGE_CLASSIFICATION這個API 的url,路徑已經從上一篇文章的 https://sandbox.api.sap.com/ml ,變成了包含CloudFoundry環境資訊的:

https://mlftrial-image-classifier.cfapps.eu10.hana.ondemand.com/api/v2/image/classification .

接下來通過clientid和client secret換取access token,使用下面這個連結裡的應用獲得access Token:

https://generate_ml_token.cfapps.eu10.hana.ondemand.com/

部署在SAP Cloud Platform CloudFoundry環境的應用如何消費SAP Leonardo機器學習API
部署在SAP Cloud Platform CloudFoundry環境的應用如何消費SAP Leonardo機器學習API

把這個Bearer開頭的access Token記錄下來。

之前建立service key時,我們已經從建立結果裡獲得了基於產品圖片識別類別的API url:

https://mlftrial-image-classifier.cfapps.eu10.hana.ondemand.com/api/v2/image/classification

瀏覽器裡訪問這個url,得到一個採用Swagger(一個採用RESTFUL介面,基於YAML和JSON語言的API文件線上自動生成工具)實現的API控制檯:

部署在SAP Cloud Platform CloudFoundry環境的應用如何消費SAP Leonardo機器學習API

點選try out,從本地選擇一張圖片,我還是選擇的這張圖片:

部署在SAP Cloud Platform CloudFoundry環境的應用如何消費SAP Leonardo機器學習API

再將之前通過clientid和client secret換取的access token填到控制檯對應欄位裡,點選execute:

部署在SAP Cloud Platform CloudFoundry環境的應用如何消費SAP Leonardo機器學習API

得到結果:

部署在SAP Cloud Platform CloudFoundry環境的應用如何消費SAP Leonardo機器學習API

至於如何在nodejs和Java應用裡通過程式設計動態地獲取access Token以及傳送HTTP post請求,因為網上例子很多,在下面更多閱讀的文章裡我也講過,所以這裡我就不再重複了,感謝閱讀。

更多閱讀


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/24475491/viewspace-2650473/,如需轉載,請註明出處,否則將追究法律責任。

相關文章