RESTful API
RESTful API
In this blog post I am going to show how to create a resource server api. First we need to understand how it works.

We can use exiting authorization sever or like there you can create your own authorization sever and resource server both in a single api.
Sample code is uploaded to my github
LINK : " "
This is written using node.js. In order to run this on your computer you have to have node.js installed on your comouter.
app.js
This app tuns on port 4000. You can give any port number here.There are two endpoints which I have created in this. One to get the access token which is "/oauth/token" and the other one is to get resources which is "/profile". As resources I have hard-coded one value which is name ("pasan") and this comes as a JSON object
model.js
(username = test, password = test)
All the functions that handle requests from client are written here.
Run
We use RESTclient Mozilla Firefox Add on. You can use other similar products such as Postman for this.
First We have to make a POST request to get the access token from the authorization server, for that we have to send the authorization key in the header.
Authorization : Bearer ***************
And also we have to mention the content type in the header.
Content-Type : application/x-www-form-urlencoded
Then mention these 3 parameters in the body.
username=test
password=test
grant_type=client_credentials
The URL should be the endpoint that gives us the access token.
http://localhost:4000/oauth/token
Then we send the GET response with access token which has an expiration time.
after that we make GET request to retrieve the resources we need
Now our URL is different because we have to call a different endpoint to get these resources which is
"http://localhost:4000/profile".
In the request header we should send the access token we got in the previous step.
Authization: Bearer ****************
Make sure that the access token is not expired. Otherwise you will get an error message saying that it has expired.
When you sent this request you get a response that contains the resources we specified in the code.
{"name":"pasan","id":"set"}
Thnak you for reading My Blog !!!!
:) :)
Comments
Post a Comment