DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Beyond Manual Annotation: Engineering Self-Correcting Pseudo-Labeling Pipelines
  • Building Threat Intelligence Pipelines Using Python, APIs, and Elasticsearch
  • Implementing Secure API Gateways for Microservices Architecture
  • Contract-First Integration: Building Scalable Systems With Flyway, OpenAPI, and Kafka

Trending

  • Lambda-Driven API Design: Building Composable Node.js Endpoints With Functional Primitives
  • When Snowflake Lies to You: Understanding False Failures in dbt Pipelines
  • MuleSoft IDP: Enhancing Efficiency and Accuracy in Data Extraction
  • No More Cheap Claude: 4 First Principles of Token Economics in 2026
  1. DZone
  2. Data Engineering
  3. Databases
  4. WSO2 Token API Invocation From a Mediation Sequence in WSO2 APIM 3.1.0.

WSO2 Token API Invocation From a Mediation Sequence in WSO2 APIM 3.1.0.

In this article, I am going to show how we can invoke WSO2’s token API from a mediation sequence to get the access/bearer token.

By 
Suman Mohan user avatar
Suman Mohan
·
Aug. 24, 20 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
6.9K Views

Join the DZone community and get the full member experience.

Join For Free

In this article, I am going to show how we can invoke WSO2’s token API from a mediation sequence to get the access/bearer token which is used to invoke APIs’ secured using Oauth in WSO2. Here I have created an API in WSO2 APIM for achieving this functionality.

1. Create an API named “InvokeTokenAPI” in WSO2 API Publisher as below.

creating API

2. Go to Resources tab and create a GET resource with the name “gettoken” as shown below and click the plus icon. Then disable security for this resource and click Save at the bottom.

resources

gettoken

3. Go to the Endpoints tab and create an HTTP/REST Endpoint. Enter the token API URL in Production and Sandbox endpoints (in my case its “https://localhost:8243/token”) and click save.

general endpoint configurations

4. Create a mediation sequence (name: InvokeTokenAPI_INSequence.xml) with the below content in a text editor to invoke token API and save it. Here I am trying to get the access/bearer token of the Default Application in WSO2 APIM Dev Portal. So, while invoking the token API for this application we will pass the basic authorization header. This header value will be obtained by getting the consumer-key and consumer-secret for the particular application from Dev Portal. Its value should be in the form “Basic Base64(consumer-key:consumer-secret)”. In the content given below, I am passing the same header with the name “Authorization”.

Also, to invoke the token API we need to pass grant_type within the request body which I have done using the payload factory mediator as seen in the below content. Also, we need to pass another header called messageType=”application/x-www-form-urlencoded” for invoking the token API.

Sequence: 

XML
x
28
 
1
<sequence name="InvokeTokenAPI_IN_SEQ" trace="disable" xmlns="http://ws.apache.org/ns/synapse">
2
     <log level="full">
3
       <property name="InvokeTokenAPI_IN_SEQ" value="Start of InvokeTokenAPI_IN_SEQ"></property>
4
     </log>
5
     <property description="SwitchExpression" expression="fn:concat(get-property('axis2', 'HTTP_METHOD'),'##',get-property('axis2','REST_URL_POSTFIX'))" name="SwitchExpression" scope="default" type="STRING"></property>
6
     <log level="full">
7
        <property expression="get-property('PathSwitchExpression')" name="SwitchExp"></property>
8
     </log>
9
     <switch source="get-property('SwitchExpression')">
10
        <case regex=".*GET##\/gettoken.*">
11
            <log level="full">
12
               <property name="LogMessage" value="GET resource - /gettoken called."></property>
13
            </log>
14
            <header name="Authorization" scope="transport" value="Basic VEVzdjJERmVhaUZsblJ1Snp1YzJMWndxQ3h3YTpFQ1dSZzZkRWl0eDcybGplQmo5TDlHS1hwcWth"></header>
15
            <payloadFactory media-type="json">
16
               <format>{"grant_type":"client_credentials"}</format>
17
               <args></args>
18
            </payloadFactory>
19
            <property name="messageType" scope="axis2" type="STRING" value="application/x-www-form-urlencoded"></property>
20
            <property name="HTTP_METHOD" scope="axis2" type="STRING" value="POST"></property>
21
         </case>
22
         <default></default>
23
     </switch>
24
     <log level="full">
25
        <property name="InvokeTokenAPI_IN_SEQ" value="End of InvokeTokenAPI_IN_SEQ"></property>
26
     </log>
27
</sequence>
28

          
XML
 




xxxxxxxxxx
1



1
<payloadFactory media-type="json">
2
   <format>{"grant_type":"password",
3
            "username": "admin",
4
            "password": "admin"}
5
   </format>
6
   <args/>
7
</payloadFactory>



For getting the consumer-key and consumer-secret for an application, log in to Dev Portal, go to the application, then production keys and obtain the values.

defaultapplication

5. Now, go to Runtime Configurations tab, click on edit in the request message mediation, select Custom Policies, and choose the mediation sequence created in the above step and click select. Click save at the bottom.

mediation policy

6. Now, go to the Lifecycle tab and Publish the API.

lifecycle

7. Test this API from WSO2 APIM Dev Portal or any Rest API testing tool like Postman. I have used Postman. Just give the API URL, choose the GET method, and invoke. No headers are required.

GET method

As seen in the above screenshot, the token API is getting invoked through the API we created in API Publisher and returning the access/bearer token in response.

The below 3 lines of code are the ones that are needed to invoke token API from a WSO2 mediation sequence. The same 3 lines can be used in a WSO2 ESB sequence as well to invoke the token API.

XML
 




xxxxxxxxxx
1
11
9



1
<header name="Authorization" scope="transport" value="Basic VEVzdjJERmVhaUZsblJ1Snp1YzJMWndxQ3h3YTpFQ1dSZzZkRWl0eDcybGplQmo5TDlHS1hwcWth"/>
2
<payloadFactory media-type="json">
3
  <format>{"grant_type":"client_credentials"}</format>
4
  <args/>
5
</payloadFactory>
6
<property name="messageType" scope="axis2" type="STRING" value="application/x-www-form-urlencoded"/>



API

Opinions expressed by DZone contributors are their own.

Related

  • Beyond Manual Annotation: Engineering Self-Correcting Pseudo-Labeling Pipelines
  • Building Threat Intelligence Pipelines Using Python, APIs, and Elasticsearch
  • Implementing Secure API Gateways for Microservices Architecture
  • Contract-First Integration: Building Scalable Systems With Flyway, OpenAPI, and Kafka

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook