1. Shenbagam Balakumar
  2. PowerServer
  3. Wednesday, 22 May 2024 05:12 AM UTC

Hi Team,

I'm able to get access token using OAuthClient. Now, I need to get the claims (payload data ) from access token or id token. Please let me know how we can get the payload data in PowerBuilder.

 

//TokenRequest
ltr_Request.Method = "POST"
ltr_Request.tokenlocation = ls_url
ltr_Request.granttype = "authorization_code"
ltr_Request.clientid = ls_ClientID
ltr_Request.secureprotocol = 0
ltr_Request.clientsecret = ls_clientsecret
ltr_Request.scope = ls_Scope
ltr_Request.ClearParams()
ltr_Request.AppendParam("grant_type","authorization_code")
ltr_Request.AppendParam("client_id", ls_ClientID)
ltr_Request.AppendParam("client_secret", ls_clientsecret)
ltr_Request.AppendParam("scope", ls_Scope)
ltr_Request.AppendParam("code", ls_code)
ltr_Request.AppendParam("code_verifier", ls_verify_code)

ltr_Request.ClearHeaders()
ltr_Request.SetHeader("Content-Type","application/x-www-form-urlencoded")

loac_Client = Create OAuthClient
li_Return = loac_Client.AccessToken(ltr_Request, ltr_Response)
If li_Return = 1 and ltr_Response.GetStatusCode() = 200 Then

li_Return = ltr_Response.getbody(ls_token_body)

if li_Return = 1 THEN

JsonParser ljson_Parser
ljson_Parser = Create JsonParser

ljson_Parser.LoadString( ls_token_body )
ls_TokenType = ljson_Parser.GetItemString( "/token_type" )
ls_AccessToken = ljson_Parser.GetItemString( "/access_token" )
ll_Return = Getapplication().SetHttpRequestHeader( "Authorization", ls_TokenType + " " + ls_AccessToken, True )

End if
Else
li_Return = ltr_Response.GetTokenError(ls_type, ls_description, ls_uri, ls_state)
MessageBox("AccessToken Falied", "Return :" + String (li_Return) + "~r~n" + String(ltr_Response.GetStatusCode() )+ "~r~n" + ltr_Response.GetStatustext()+ "~r~n" + ls_description)
End If

 

 

Thanks in Advance!!

Shenbagam Balakumar.

Accepted Answer
Logan Liu @Appeon Accepted Answer Pending Moderation
  1. Thursday, 23 May 2024 01:32 AM UTC
  2. PowerServer
  3. # Permalink

Hi Shenbagam Balakumar,

The following is an example of how the payload can be obtained from the access token:

// Parse AccessToken
// Assuming your access token is stored in ls_access_token
String ls_access_token
String ls_header, ls_payload
String ls_decoded_payload
Long ll_pos
Blob lb_payload
CoderObject lco_Code

ls_access_token = ls_AccessToken

// Find the first dot in the token
ll_pos = Pos(ls_access_token, '.')

// Extract the header
ls_header = Left(ls_access_token, ll_pos - 1)

// Remove the header and the dot from the token
ls_access_token = Mid(ls_access_token, ll_pos + 1)

// Find the next dot in the token
ll_pos = Pos(ls_access_token, '.')

// Extract the payload
ls_payload = Left(ls_access_token, ll_pos - 1)


// At this point, ls_payload is a Base64Url encoded string.
// You need to decode it to see the actual payload.
// Decode from Base64Url
lco_Code = Create CoderObject
lb_payload = lco_Code.Base64UrlDeCode(ls_payload)
Destroy lco_Code

// Convert the blob to a string, now ls_decoded_payload contains the payload of the access token
ls_decoded_payload = String(lb_payload, EncodingUTF8!)

 

Regards, Logan

Comment
  1. Shenbagam Balakumar
  2. Thursday, 23 May 2024 05:05 AM UTC
Hi Logan,



Thanks for the response. Thank you very much for your help. It's worked!!



Thanks,

Shenbgam Balakumar.
  1. Helpful
There are no comments made yet.


There are replies in this question but you are not allowed to view the replies from this question.
We use cookies which are necessary for the proper functioning of our websites. We also use cookies to analyze our traffic, improve your experience and provide social media features. If you continue to use this site, you consent to our use of cookies.