部署在SAP Cloud Platform CloudFoundry环境的应用如何消费SAP Leonardo机器学习API
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机器学习的服务,单击该服务的超链接进入明细页面:

创建一个新的服务实例:

Service Plan就选默认的standard:

给这个服务实例取个名字:

单击这个创建好的服务实例,然后创建一个新的Service Key:


给Service Key也取个名字。

我们通过创建Service instance进而创建Service Key的目的,是为了得到下图的clientid和clientsecret。

而我们拿到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里:

注意观察,此时的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/


把这个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控制台:

点击try out,从本地选择一张图片,我还是选择的这张图片:

再将之前通过clientid和client secret换取的access token填到控制台对应字段里,点击execute:

得到结果:

至于如何在nodejs和Java应用里通过编程动态地获取access Token以及发送HTTP post请求,因为网上例子很多,在下面更多阅读的文章里我也讲过,所以这里我就不再重复了,感谢阅读。
更多阅读