I am relatively new to Apigee and want to use the "apigee.access_token" generated by Apigee and append it to one of my existing variables called “tokenPayLoad”. After that, I want to use this newly formed access_token for future calls to my API.
I have tried using SetOAuthV2Info policy but it still does not work as subsequent calls requiring access_token still check with the originally generated access_token.
<ProxyEndpoint name="token">
...
<Flow name="token">
<Request>
<Step>
<Name>Token-BuildRequest</Name>
</Step>
</Request>
<Response>
<Step>
<Name>GenerateAccessToken</Name>
</Step>
<Step>
<Name>Token-BuildResponse</Name>
</Step>
<Step>
<Name>AM-TokenResponse</Name>
</Step>
</Response>
</Flow>
...
<ProxyEndpoint name="verification">
...
<Flow name="verify">
<Request>
<Step>
<Name>OAuthv2Verify</Name>
</Step>
<Step>
<Name>RF-InvalidToken</Name>
<Condition>error.status.code = "401"</Condition>
</Step>
</Request>
</Flow>
GenerateAccessToken
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<OAuthV2 async="false" continueOnError="false" enabled="true" name="GenerateAccessToken">
<DisplayName>GenerateAccessToken</DisplayName>
<Operation>GenerateAccessToken</Operation>
<ExpiresIn>900000</ExpiresIn>
<Algorithm>RS512</Algorithm>
<GenerateResponse enabled="true"/>
<Scope>scope</Scope>
<SupportedGrantTypes>
<GrantType>client_credentials</GrantType>
</SupportedGrantTypes>
<GrantType>grant_type</GrantType>
<TokenType>Bearer</TokenType>
<TokenLength>6000</TokenLength>
</Attributes>
</OAuthV2>
TokenBuildResponse.js
var tokenPayLoad = JSON.parse(context.getVariable('tokenResponsePayload'));
const accessToken = context.getVariable('apigee.access_token');
tokenPayLoad.access_token = tokenPayLoad.access_token + "." + accessToken
context.setVariable('response.content', JSON.stringify(tokenPayLoad));
OAuth2Verify
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<OAuthV2 async="false" continueOnError="false" enabled="true" name="OAuthv2Verify">
<DisplayName>OAuthv2Verify</DisplayName>
<Operation>VerifyAccessToken</Operation>
<Scope>TestApp</Scope>
<SupportedGrantTypes/>
<GenerateResponse enabled="true"/>
<Tokens/>
</OAuthV2>