Does anyone have a simple sample of an OAuth2 Web client?

This Q&A forum allows users to post and respond to "How Do I Do ....." questions. Please do not use to report (suspected) errors - you must use your regional help desk for this. The information contained in this forum has not been validated by LANSA and, as such, LANSA cannot guarantee the accuracy of the information.
Post Reply
Taku Izumi
Posts: 52
Joined: Thu Dec 15, 2016 2:03 pm

Does anyone have a simple sample of an OAuth2 Web client?

Post by Taku Izumi » Wed Sep 25, 2024 2:41 pm

Hi,

I got an OAuth client sample from the link below.
https://developer.lansa.com/files/webap ... ibrary.zip

However, I'm having a hard time deciphering the sample because the logic is split up into many methods and properties.

Does anyone have a simpler sample of an OAuth2 Web client?

Regards,
Taku

User avatar
Dino
Posts: 427
Joined: Fri Jul 19, 2019 7:49 am
Location: Robbinsville, NC
Contact:

Re: Does anyone have a simple sample of an OAuth2 Web client?

Post by Dino » Sat Sep 28, 2024 2:23 am

おはよう Taku,

久しぶりです!

I found easier to receive the details of what I need to connect in Postman, then use it to generate curl example (I hope one day we can generate lansa sample from Postman), then use that curl to write the lansa code, like this:

Code: Select all

Define_Com Class(#XPRIM_UriBuilder) Name(#URL)
Define_Com Class(#XPRIM_HttpRequest) Name(#HttpRequest)
Define_Com Class(#XPRIM_JsonWriter) Name(#Writer)
Define_Com Class(#XPRIM_RandomAccessJsonReader) Name(#Reader) Reference(*DEFERRED)
Define_Com Class(#XPRIM_ErrorInfo) Name(#ErrorObject) Reference(*DEFERRED)
Define_Com Class(#PRIM_STR) Name(#WK_BearerToken)

.....

* Lets say you have to make a call to oAuth2 to get the bearer token 
* (you can use a curl example from postman as base to build this)

* ===> First call

* curl --location 'https://login.salesforce.com/services/oauth2/token' \
* --header 'Authorization: Bearer 0001010101bearer0101010101' \
* --form 'grant_type="urn:ietf:params:oauth:grant-type:jwt-bearer"' \
* --form 'assertion="1110101Longinus01010101111"'

#Url.SetScheme( 'https' )
#Url.SetHost( 'login.salesforce.com' )
#Url.SetPath( '/services/oauth2/token' )
#HttpRequest.Options.AddBearerAuthorization Value('0001010101bearer0101010101')
#HttpRequest.Options.AddHeader Name('Cookie') Value('BrowserId=010101browserid010101; CookieConsentPolicy=0:1; LSKey-c$CookieConsentPolicy=0:1')
#HttpRequest.Content.AddUrlEncodedFormValue( 'grant_type' 'urn:ietf:params:oauth:grant-type:jwt-bearer' )
#HttpRequest.Content.AddUrlEncodedFormValue( 'assertion' '1110101Longinus01010101111' )
#HttpRequest.DoPost Url(#Url)
If (#HttpRequest.Response.IsSuccessfulRequest)
If (#HttpRequest.Response.IsSuccessHttpStatusCode)

* Now we got a response something like this:
* {
* "access_token": "01010101accesstoken10010101",
* "scope": "web id api full",
* "instance_url": "https://TerminalDogma.my.salesforce.com",
* "id": "https://login.salesforce.com/id/01010101010Nerv0101010101",
* "token_type": "Bearer"
* }

* lets read the access token from the response
#VF_ELTXTX := #HttpRequest.Response.AsString.AsNativeString
#Reader.SetSourceString String(#VF_ELTXTX) Errorinfo(#ErrorObject)
* if the json is not properly formed
If (*Not #ErrorObject.OK)
Return
Endif
#WK_BearerToken := #Reader.ReadStringWithName( 'access_token' )

* ====> Second Call
* lets get setup for the new call
#HttpRequest.Clear

* This is the body we need to transmit
* {
* "Request":{
* "Name": "TerminalDogmaOutbound",
* "AuthenticationKey": "010101010Ayanami01010101"
* }
* }

* Prepare the json body
#Writer.SetOutputToHttpRequest Httprequest(#HttpRequest)
#Writer.BeginObject
#Writer.BeginObject Name('Request')
#Writer.WriteString Name('Name') Value('TerminalDogmaOutbound')
#Writer.WriteString Name('AuthenticationKey') Value('010101010Ayanami01010101')
#Writer.EndObject
#Writer.EndObject

* Make the call
#Url.SetScheme( 'https' )
#Url.SetHost( 'TerminalDogma.my.salesforce.com' )
#Url.SetPath( '/services/apexrest/DogmaNT/DogmaIntegrationService' )
#HttpRequest.Options.AddHeader Name('Content-Type') Value('application/json')
#HttpRequest.Options.AddBearerAuthorization Value(#WK_BearerToken)
#HttpRequest.Options.AddHeader Name('Cookie') Value('BrowserId=010101browserid010101; CookieConsentPolicy=0:1; LSKey-c$CookieConsentPolicy=0:1')
#HttpRequest.DoPost Url(#Url)

If (#HttpRequest.Response.IsSuccessfulRequest)

#VF_ELTXTX := #HttpRequest.Response.AsString.AsNativeString
#Reader.SetSourceString String(#VF_ELTXTX) Errorinfo(#ErrorObject)
* if the json is not properly formed
If (*Not #ErrorObject.OK)
Return
Endif

* Now lets read all the data
#STD_NUM := #Reader.ReadNumberWithName( 'RecordCount' )

* followed by a lot of endifs.... :)

Taku Izumi
Posts: 52
Joined: Thu Dec 15, 2016 2:03 pm

Re: Does anyone have a simple sample of an OAuth2 Web client?

Post by Taku Izumi » Tue Oct 01, 2024 3:20 pm

こんばんわ DInoさん,
お久しぶりです。

Thank you for a simple and easy to understand example.

I was understading API using OAuth to require two calls, one to get the token and one to get the data.
This example confirmed that my understanding was correct.

Thank you so much for all your help as always.
Taku

Post Reply