SAP cloud platform uses Cloud Foundry to house some of its' services, like HyperLedger etc.
The following works if I do it manually:
I login to a cloud foundry instance with a valid user name / password at https://login.cf.us10.hana.ondemand.com/login
That then redirects me to another host "blockchain-cockpit.cfapps.us10.hana.ondemand.com" where I can successfully call the RESTful API's in question via the "cockpit" provided on the web page.
My goal here is to be able to call the RESTful services on "blockchain-cockpit.cfapps.us10.hana.ondemand.com" using an OAuth token.
So I go to the command line to get an OAuth token using the following command:
curl -v -XPOST -H "Application/json" -u "cf:" --data "username=<username>&password=<password>&client_id=cf&grant_type=password&response_type=token" https://login.cf.us10.hana.ondemand.com/oauth/token
And that happily passes me back what *seems to be a valid" token.
For example:
{"access_token":"eyJhbGciOiJSUzI1NiIsImtpZCI6ImtleS0xIiwidHlwIjoiSldUIn0.eyJqdGkiOiJmZTY1NjYyOTM3YTk0Nzc0YTUyYmM5MzRiYzU5MjRjOCIsInN1YiI6ImMxYzcyZTcyLWI3OGUtNDAyYy1iZTBjLWQ3MDUyNzYzZmZmYiIsInNjb3BlIjpbIm9wZW5pZCIsInVhYS51c2VyIiwiY2xvdWRfY29udHJvbGxlci5yZWFkIiwicGFzc3dvcmQud3JpdGUiLCJjbG91ZF9jb250cm9sbGVyLndyaXRlIl0sImNsaWVudF9pZCI6ImNmIiwiY2lkIjoiY2YiLCJhenAiOiJjZiIsImdyYW50X3R5cGUiOiJwYXNzd29yZCIsInVzZXJfaWQiOiJjMWM3MmU3Mi1iNzhlLTQwMmMtYmUwYy1kNzA1Mjc2M2ZmZmIiLCJvcmlnaW4iOiJsZGFwIiwidXNlcl9uYW1lIjoia2V2aW5AZXJwZ2VuaWUuY29tIiwiZW1haWwiOiJrZXZpbkBlcnBnZW5pZS5jb20iLCJhdXRoX3RpbWUiOjE1MTU4NzE4NTgsInJldl9zaWciOiI2YzIwMjc2YyIsImlhdCI6MTUxNTg3MTg1OCwiZXhwIjoxNTE1ODcyNDU4LCJpc3MiOiJodHRwczovL3VhYS5jZi51czEwLmhhbmEub25k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SKKNKzZaLW9ONip7yBmMhCub48D_zT26bOKUIYenO1LAV5aHjufsut7YxWBadrPSpGF-Oz6VYYeDqCerf7F8-rdA-RZYGUQBK-weTLJAfbQPn6CgTO4","expires_in":599,"scope":"openid uaa.user cloud_controller.read password.write cloud_controller.write","jti":"fe65662937a94774a52bc934bc5924c8"}* Connection #0 to host login.cf.us10.hana.ondemand.com left intact
Assuming I now have a valid token I then try POST using POSTMAN and set the HTTP header:
Authorization: Bearer #token from above#
As mentioned before I am POSTing to "blockchain-cockpit.cfapps.us10.hana.ondemand.com" and not the host I got the token from so not sure if that is a major issue here.
PostMan keeps giving me this error
{ "error": "Not authorized" }
I then try the same using CURL using the following command:
curl -H "Authorization: Bearer <token>" -X POST https://blockchain-cockpit.cfapps.us10.hana.ondemand.com/<remaining rest api> --data "test data"
I get the exact same issue:
{"error":"Not authorized"}
So my issue in my mind is if I login manually and interact with the api's on the site it works perfectly, yet when I apparently get a valid token and try post to the restful api's it keep failing?
Any ideas? I would really appreciate some help on this ;-)
Thanks
Lynton
You don't call your chaincode via the blockchain cockpit API.
Like with every other Service on SAP Cloud Platform (e.g. MongoDB) you have to create a Service Key/Service Binding to get access to your Service Instance.
Create Service Key
and specify a nameYou now see the access information to your service instance
{ "apiKey": "", "serviceUrl": "https://hyperledger-api.cfapps.us10.hana.ondemand.com" }
You can get test the API by opening the service URL: https://hyperledger-api.cfapps.us10.hana.ondemand.com
Regarding your question in the comments, to inject this information in a Java application you deploy you have to create a Service Binding and link the blockchain service instance to your application. The information of your service key will then be injected into the environment variables of your app. More information here: SAP Help