cancel
Showing results for 
Search instead for 
Did you mean: 

Extract Variable policy in SAP APIM - extracting client credentials from request header

snehashah
Explorer
0 Kudos
205

Hello Experts,

I've setup a REST API proxy that enables access via OAuth 2.0 on the ProxyEndpoint side.

On the TargetEndpoint side I have to authenticate using OAuth 2.0. I've developed the following two policies in order to enable authentication against the TargetEndpoint.

However, since the extract variable policy does not get the variable values, the flow goes into error. 

Any help in this regard would be greatly appreciated.

1. Extract Variable policy : It needs to extract the client_id and client_secret into variables which is input  from postman as grant type Client_credentials & client_id and client_secret

2. Basic Authentication : It needs to take the get the variables values from step 1 and create a Authorization header.


Extract Variable Code : 

<!-- Extract content from the request or response messages, including headers, URI paths, JSON/XML payloads, form parameters, and query parameters -->
<ExtractVariables async="true" continueOnError="false" enabled="true" xmlns='http://www.sap.com/apimgmt'>
	<Variable name="clientid" />
	    	<Variable name="clientsecret" />
   
        <Header name="client_id" />
    <Header name="client_secret" />
<Source>header</Source>
</ExtractVariables> 

Basic Authentication Code:

<BasicAuthentication async='true' continueOnError='false' enabled='true' xmlns='http://www.sap.com/apimgmt'>
	<Operation>Encode</Operation>
	<IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
	<User ref='clientid'></User>
	<Password ref='clientsecret'></Password>
	<AssignTo createNew="true">sapapim.Authorization</AssignTo>
</BasicAuthentication>

The extract variable does not extract the client_id and client_secret as expected and an error is returned 

{
    "fault": {
        "faultstring": "Unresolved variable : clientid",
        "detail": {
            "errorcode": "steps.basicauthentication.UnresolvedVariable"
        }
    }
}

API flow screenshot: 

snehashah_0-1726576195959.png

 

View Entire Topic
nageshrao
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hello Sneha, 
Please update the code for Extract Variable Policy as below and you should be able to extract the variable values as required.

 

<ExtractVariables async="false" continueOnError="false" enabled="true" xmlns='http://www.sap.com/apimgmt'>
    <Source>request</Source>
    <Header name="client_id">
        <Pattern ignoreCase="true">{clientid}</Pattern>
    </Header>
    <Header name="client_secret">
        <Pattern ignoreCase="true">{clientsecret}</Pattern>
    </Header>
</ExtractVariables>

 

snehashah
Explorer
0 Kudos

Hello Nagesh,

Thanks for the solution, it has worked.