Creating Endpoint REST Service instance for a REST api which needs authorization bearer token

 

This post is in continuation to my previous post K2 Integration with REST Api Services which explains how to integrate K2 with REST Api Services. In my previous post, I have integrated with an Api Service which doesn’t require any Authorization Token that needs to be passed. But there is a chance that you find some REST api Services that needs an authorization bearer token to be passed to execute its methods. Now in this post we will see how to integrate to one of those REST Api services which needs the Authorization Bearer Tokens.

To get the swagger file follow the steps that were mentioned in my previous post which is using RESTUnited.com. There will not be any changes in steps to generate swagger except in authentication tab you will have to mention authentication token details and add it which will be as shown in below screenshots.

1AddAuthentication

 

2AddedAuthRESTUnited.jpg

 

Now once we get the Swagger file generated proceed to create Service Instance using EndPoints REST option in Smart Objects – Services – Tester Utility.

Now in Service Keys Section, Default HTTP Request Headers field you need to add this Authorization Token key in below format without any line breaks

{“$type”:”SourceCode.SmartObjects.Services.Endpoints.Common.HttpHeader[], SourceCode.SmartObjects.Services.Endpoints.Common, Version=4.0.0.0, Culture=neutral, PublicKeyToken=null”,”$values”:[{“$type”:”SourceCode.SmartObjects.Services.Endpoints.Common.HttpHeader, SourceCode.SmartObjects.Services.Endpoints.Common, Version=4.0.0.0, Culture=neutral, PublicKeyToken=null”,”Name”:”Authorization”,”Value”:”Bearer eyJhbGciOiJub25lIn0.eyJVc2V==”}]}

*Replace the Values with the token that needs to be passed. Also we can pass multiple header Tokens here.

Now give the path of the Swagger file in the descriptor location field and leave rest of the options with default values

3.jpg

That’s It, Now just create a SmartObject using this Service instance and execute to see the result.

If you get any error with message like “Unable to Deserialize…” then its issue with the format of token that’s passed in HTTP Request Headers section…Just ensure that it is in above mentioned format without any line breaks . That’s It.

Also please note that this procedure should be used only for development purpose as per K2. Don’t ask me the reason (B’cuz I too don’t know :))

Note: Recently, when one of my friend tried to create a SmartObject over a REST Service instance that he created for a REST service which needs authorization bearer token, he was constantly getting 401, 404, 0 as response code but the same service he was able to run from Postman and fiddler without any issue. He tried all above mentioned things but still we couldn’t figure out what is the issue with REST when we are trying to connect with K2. so we approached K2 Team for help and when they investigated they found that its the issue with REST Service Api URL which has spaces in it and K2RESTBroker is trying to encode the URL which is already encoded…so as a fix we have removed spaces in REST service endpoint URL and the k2 is still investigating the actually issue behind the k2 encoding the already encoded URL. So just ensure that your REST Service URL doesn’t have any spaces in it.

Below are the community posts which will give you related information regarding this.

http://community.k2.com/t5/K2-blackpearl/How-toHandle-Authorization-Token-while-Creating-Endpoints-REST/m-p/101521#M33759

Author: Vijay

I'm a K2 Certified Developer, helping teams to automate, design, develop & maintain business process applications connecting across multiple platforms with multiple technologies.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s