Example 3 (using OAuthClient and 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 |
OAuthClient loac_Client TokenRequest ltr_Request TokenResponse ltr_Response HttpClient lhc_Client String ls_AccessToken, ls_TokenType String ls_Body, ls_type, ls_description, ls_uri, ls_state, ls_Url Long ll_return loac_Client = Create OAuthClient lhc_Client = Create HttpClient <span><strong>//Step 1: Get the RESTful server access token.</strong></span> //The following line is fake code. Replace it with settings //from your OAuth 2.0 authorization server provider. ltr_Request.tokenlocation = "https://xxx.xxx.xxx/oauth2/token" ltr_Request.Method = "POST" ltr_Request.secureprotocol = 0 ltr_Request.clientid = "367c4163ddc1427d96655cd220c6714b" ltr_Request.clientsecret = "4079f8749939446cbc81fd0c27709187" ltr_Request.UserName = "username" ltr_Request.Password = "password123" ltr_Request.scope = "testcode" ltr_Request.granttype = "password" ll_Return = loac_Client.AccessToken( ltr_Request, ltr_Response ) If ll_Return = 1 and ltr_Response.GetStatusCode () = 200 Then ls_AccessToken = ltr_Response.GetAccessToken() ls_TokenType = ltr_Response.GetTokenType( ) <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_TokenType + " " + 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 ll_Return = 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 ) If IsValid ( lhc_Client ) Then DesTroy ( lhc_Client ) |
Document get from Powerbuilder help
Thank you for watching.
Subscribe
Login
0 Comments
Oldest