1. Ong Chin Keat
  2. PowerBuilder
  3. Sunday, 21 April 2024 00:41 AM UTC

hi everybody,

My script below do not get required token as it return error -21. Even I have commented out the "secureprotocal" or set it to lower version, it's still not work. Please help

 

OAuthClient   loac_Client
TokenRequest  ltr_Request
TokenResponse ltr_Response
OAuthRequest  loar_Request
ResourceResponse lrr_Response
String        ls_AccessToken
String        ls_Body, ls_type, ls_description, ls_uri, ls_state, ls_rtn
Long          ll_return, ll_rtn

loac_Client = Create OAuthClient

//Step 1: Get the RESTful server access token.
//The following line is fake code. Replace it with settings
//from your OAuth 2.0 authorization server provider.
ltr_Request.tokenlocation = "https://preprod-api.myinvois.hasil.gov.my/connect/token" 
ltr_Request.Method = "POST"
//ltr_Request.secureprotocol = 0
ltr_Request.clientid = "93e....ae" 
ltr_Request.clientsecret = "3ed....f4a"
ltr_Request.granttype = "client_credentials" 
ltr_Request.scope = "InvoicingAPI"   

ltr_Request.ClearHeaders()
//ltr_Request.SetHeader("User-Agent", "PostmanRuntime/7.37.3")
//ltr_Request.SetHeader("Accept", "*/*")
//ltr_Request.SetHeader("Postman-Token", "00fd85e7-38bf-408a-a846-aaa073436928")
//ltr_Request.SetHeader("Host", "preprod-api.myinvois.hasil.gov.my")
//ltr_Request.SetHeader("Accept-Encoding", "gzip, deflate, br")
//ltr_Request.SetHeader("Connection", "keep-alive")
//ltr_Request.SetHeader("Content-Type", "application/json;charset=UTF-8" )   //"application/x-www-form-urlencoded")
ltr_Request.SetHeader("Content-Length", "2048")  //"146")


ll_Return = loac_Client.AccessToken( ltr_Request, ltr_Response )
If ll_Return = 1 Then  
		
		ll_rtn =  ltr_Response.GetStatusCode ()
		ls_rtn = ltr_Response.getstatustext( )
		
         ll_Return = ltr_Response.GetBody(ls_Body)
         If ll_Return = 1 Then
                   ls_AccessToken = ltr_Response.GetAccessToken()
				 IF ls_AccessToken = '' THEN RETURN -1;
				 
                   //Step 2: Get the RESTful server resource.
                   loar_Request.Method = "GET"
                   //The following line is fake code. Replace it with settings
                   //from your OAuth 2.0 authorization server provider.
                   loar_Request.Url = "https://preprod-api.myinvois.hasil.gov.my/api/v1.0/documenttypes"   //"https://xxx.xxx.xxx/order/getall"
                   loar_Request.SetAccessToken( ls_AccessToken )
                   ll_Return = loac_Client.RequestResource( loar_Request, lrr_Response )
                   If ll_Return = 1 Then
                            ll_Return = lrr_Response.GetBody(ls_Body)
                            If ll_Return = 1 Then
                                     MessageBox ( "Resource", ls_Body )
                            End If
                   Else
                            MessageBox( "Requestresource Falied", "Return :" + String ( ll_return ) + "~r~n" + lrr_Response.GetStatusText() )
                   End If
                   
         End If
Else
         ll_rtn = ltr_Response.GetTokenError(ls_type, ls_description, ls_uri, ls_state)
         MessageBox( "AccessToken Falied", "Return :" + String ( ll_return ) + "~r~n" + ls_description )
End If

If IsValid ( loac_Client ) Then DesTroy ( loac_Client )

 

regards,

Ong

 

OAuthClient   loac_Client
TokenRequest  ltr_Request
TokenResponse ltr_Response
OAuthRequest  loar_Request
ResourceResponse lrr_Response
String        ls_AccessToken
String        ls_Body, ls_type, ls_description, ls_uri, ls_state, ls_rtn
Long          ll_return, ll_rtn

loac_Client = Create OAuthClient

//Step 1: Get the RESTful server access token.
//The following line is fake code. Replace it with settings
//from your OAuth 2.0 authorization server provider.
ltr_Request.tokenlocation = "https://pr...t/token" 
ltr_Request.Method = "POST"
//ltr_Request.secureprotocol = 0
ltr_Request.clientid = "93e....ae" 
ltr_Request.clientsecret = "3ed....f4a"
ltr_Request.granttype = "client_credentials" 
ltr_Request.scope = "InvoicingAPI"   

ltr_Request.ClearHeaders()
//ltr_Request.SetHeader("User-Agent", "PostmanRuntime/7.37.3")
//ltr_Request.SetHeader("Accept", "*/*")
//ltr_Request.SetHeader("Postman-Token", "00fd85e7-38bf-408a-a846-aaa073436928")
//ltr_Request.SetHeader("Host", "pr...")
//ltr_Request.SetHeader("Accept-Encoding", "gzip, deflate, br")
//ltr_Request.SetHeader("Connection", "keep-alive")
//ltr_Request.SetHeader("Content-Type", "application/json;charset=UTF-8" )   //"application/x-www-form-urlencoded")
ltr_Request.SetHeader("Content-Length", "2048")  //"146")


ll_Return = loac_Client.AccessToken( ltr_Request, ltr_Response )
If ll_Return = 1 Then  
		
		ll_rtn =  ltr_Response.GetStatusCode ()
		ls_rtn = ltr_Response.getstatustext( )
		
         ll_Return = ltr_Response.GetBody(ls_Body)
         If ll_Return = 1 Then
                   ls_AccessToken = ltr_Response.GetAccessToken()
				 IF ls_AccessToken = '' THEN RETURN -1;
				 
                   //Step 2: Get the RESTful server resource.
                   loar_Request.Method = "GET"
                   //The following line is fake code. Replace it with settings
                   //from your OAuth 2.0 authorization server provider.
                   loar_Request.Url = "https://p.../documenttypes"   //"https://xxx.xxx.xxx/order/getall"
                   loar_Request.SetAccessToken( ls_AccessToken )
                   ll_Return = loac_Client.RequestResource( loar_Request, lrr_Response )
                   If ll_Return = 1 Then
                            ll_Return = lrr_Response.GetBody(ls_Body)
                            If ll_Return = 1 Then
                                     MessageBox ( "Resource", ls_Body )
                            End If
                   Else
                            MessageBox( "Requestresource Falied", "Return :" + String ( ll_return ) + "~r~n" + lrr_Response.GetStatusText() )
                   End If
                   
         End If
Else
         ll_rtn = ltr_Response.GetTokenError(ls_type, ls_description, ls_uri, ls_state)
         MessageBox( "AccessToken Falied", "Return :" + String ( ll_return ) + "~r~n" + ls_description )
End If

If IsValid ( loac_Client ) Then DesTroy ( loac_Client )
Logan Liu @Appeon Accepted Answer Pending Moderation
  1. Monday, 22 April 2024 06:44 AM UTC
  2. PowerBuilder
  3. # 1

Hi Ong,

Your API server doesn't support reading client_id and client_secret from the auto-generated HTTP Request Header "Authorization".

E.g.: Authorization: Basic OTNlNDE0OTgtODcyOS00NTMwLWI0ZWQtZDdmMTAxY2QwYWFlOjNlZDZhNmQyLWY1YTQtNGVmOS04NTIyLWIzODEyNzZmY2Y0YQ==

Solution 1:

Add the client_id and client_secret to the HTTP Request Body:

OAuthClient   loac_Client
TokenRequest  ltr_Request
TokenResponse ltr_Response
OAuthRequest  loar_Request
ResourceResponse lrr_Response
String        ls_AccessToken
String        ls_Body, ls_type, ls_description, ls_uri, ls_state, ls_rtn
Long          ll_return, ll_rtn

loac_Client = Create OAuthClient

ltr_Request.ClearHeaders()


ltr_Request.tokenlocation = "https://preprod-api.myinvois.hasil.gov.my/connect/token" 
ltr_Request.Method = "POST"

ltr_Request.clientid = "93e41498-8729-4530-b4ed-d7f101cd0aae" 
ltr_Request.clientsecret = "3ed6a6d2-f5a4-4ef9-8522-b381276fcf4a"

ltr_Request.granttype = "client_credentials" 
ltr_Request.scope = "InvoicingAPI"   

// Specify client_id and client_secret in Request Body
ltr_Request.appendparam( "client_id", "93e41498-8729-4530-b4ed-d7f101cd0aae")
ltr_Request.appendparam( "client_secret", "3ed6a6d2-f5a4-4ef9-8522-b381276fcf4a")


ltr_Request.SetHeader("Accept", "*/*")
ltr_Request.SetHeader("Cache-Control", "no-cache")
ltr_Request.SetHeader("Host", "preprod-api.myinvois.hasil.gov.my")


ltr_Request.SetHeader("Accept-Encoding", "gzip, deflate, br")
ltr_Request.SetHeader("Connection", "keep-alive")


ll_Return = loac_Client.AccessToken( ltr_Request, ltr_Response )
If ll_Return = 1 Then  

...

 

Solution 2:

Use HttpClient instead of OAuthClient to get the token from this API server.

Regards,

Logan

Comment
  1. Ong Chin Keat
  2. Monday, 22 April 2024 07:19 AM UTC
Thank you, Logan, it's work like a champ. 谢谢.
  1. Helpful
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.