Example 2 (using HTTPClient)
Step 1: Get the RESTful server access token.
Step 2: Get the RESTful server resource.
The complete code example is as below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
HttpClient lhc_Client CoderObject lco_Code Jsonpackage ljpg_json String ls_ClientID, ls_Sercet, ls_Auth, ls_Url, ls_PostData, ls_UserName, ls_Password, ls_scope, ls_Body, ls_Error String ls_Token, ls_TokenType, ls_AccessToken Blob lblb_data Long ll_return lhc_Client = Create HttpClient lco_Code = Create CoderObject ljpg_json = Create Jsonpackage <span><strong>//Step 1: Get the RESTful server access token.</strong></span> //Url //The following line is fake code. Replace it with settings //from your OAuth 2.0 authorization server provider. ls_Url = "https://xxx.xxx.xxx/oauth2/token" //Authorization ls_ClientID = "367c4163ddc1427d96655cd220c6714b" ls_Sercet = "4079f8749939446cbc81fd0c27709187" lblb_data = Blob ( ls_ClientID + ":" + ls_Sercet, EncodingUTF8! ) ls_Auth = lco_Code.Base64Encode( lblb_data ) lhc_Client.SetRequestHeader( "Authorization", "Basic " + ls_Auth ) lhc_Client.SetRequestHeader( "Content-Type", "application/x-www-form-urlencoded" ) //PostData ls_UserName = "username" ls_Password = "password123" ls_scope = "testcode" ls_PostData = "grant_type=password&username="+ls_UserName+"&password="+ls_Password+"&scope=" + lco_Code.UrlEncode( Blob(ls_scope,EncodingUTF8!)) ll_return = lhc_Client.SendRequest( "POST", ls_Url, ls_PostData ) If ll_return = 1 And lhc_Client.GetResponsestatusCode() = 200 Then lhc_Client.GetResponseBody ( ls_body ) ls_Error = ljpg_json.loadString ( ls_body ) If ls_Error = "" then ls_TokenType = ljpg_json.GetValue("token_type") ls_Token = ljpg_json.GetValue("access_token") ls_AccessToken = ls_TokenType + " " + ls_Token <span><strong>//Step 2: Get the RESTful server resource.</strong></span> //The following line is fake code. Replace it with settings //from your OAuth 2.0 authorization server provider. ls_Url = "https://xxx.xxx.xxx/order/getall" lhc_Client.ClearRequestHeaders() lhc_Client.SetRequestHeader( "Authorization", ls_AccessToken ) ll_return = lhc_Client.SendRequest( "GET", ls_Url ) If ll_return = 1 And lhc_Client.GetResponsestatusCode() = 200 Then lhc_Client.GetResponseBody ( ls_body ) MessageBox ( "Resource", ls_body ) Else MessageBox( "ResourceResponse Falied", "Return :" + String ( ll_return ) + "~r~n" + lhc_Client.GetResponsestatusText() ) End If Else MessageBox( "Error", ls_Error ) End If Else MessageBox( "AccessToken Falied", "Return :" + String ( ll_return ) + "~r~n" + lhc_Client.GetResponsestatusText() ) End If If IsValid ( lco_Code ) Then DesTroy ( lco_Code ) If IsValid ( ljpg_json ) Then DesTroy ( ljpg_json ) If IsValid ( lhc_Client ) Then DesTroy ( lhc_Client ) |
Document get from Powerbuilder help
Thank you for watching.
Subscribe
Login
0 Comments
Oldest