Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(PureBasic) AzureWebsites OAuth2 Password FlowDemonstrates how to do OAuth 2.0 password flow for azurewebsites.net.
IncludeFile "CkHttpResponse.pb" IncludeFile "CkHttp.pb" IncludeFile "CkStringBuilder.pb" IncludeFile "CkJsonObject.pb" IncludeFile "CkHttpRequest.pb" Procedure ChilkatExample() ; This example requires the Chilkat API to have been previously unlocked. ; See Global Unlock Sample for sample code. http.i = CkHttp::ckCreate() If http.i = 0 Debug "Failed to create object." ProcedureReturn EndIf success.i req.i = CkHttpRequest::ckCreate() If req.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkHttpRequest::setCkHttpVerb(req, "POST") CkHttpRequest::setCkPath(req, "/token") CkHttpRequest::setCkContentType(req, "application/x-www-form-urlencoded") CkHttpRequest::ckAddParam(req,"grant_type","password") CkHttpRequest::ckAddParam(req,"username","your_username") CkHttpRequest::ckAddParam(req,"password","your_password") tokenEndpoint.s = "https://your_api.azurewebsites.net/token" resp.i = CkHttp::ckPostUrlEncoded(http,tokenEndpoint,req) If CkHttp::ckLastMethodSuccess(http) = 0 Debug CkHttp::ckLastErrorText(http) CkHttp::ckDispose(http) CkHttpRequest::ckDispose(req) ProcedureReturn EndIf sbResponseBody.i = CkStringBuilder::ckCreate() If sbResponseBody.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkHttpResponse::ckGetBodySb(resp,sbResponseBody) jResp.i = CkJsonObject::ckCreate() If jResp.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkJsonObject::ckLoadSb(jResp,sbResponseBody) CkJsonObject::setCkEmitCompact(jResp, 0) Debug "Response Body:" Debug CkJsonObject::ckEmit(jResp) ; Sample JSON response: ; { ; "access_token": "NQGHn ... xTS", ; "token_type": "bearer", ; "expires_in": 1209599, ; "userName": "your_username", ; ".issued": "Mon, 27 Apr 2020 23:49:35 GMT", ; ".expires": "Mon, 11 May 2020 23:49:35 GMT" ; } respStatusCode.i = CkHttpResponse::ckStatusCode(resp) Debug "Response Status Code = " + Str(respStatusCode) If respStatusCode >= 400 Debug "Response Header:" Debug CkHttpResponse::ckHeader(resp) Debug "Failed." CkHttpResponse::ckDispose(resp) CkHttp::ckDispose(http) CkHttpRequest::ckDispose(req) CkStringBuilder::ckDispose(sbResponseBody) CkJsonObject::ckDispose(jResp) ProcedureReturn EndIf CkHttpResponse::ckDispose(resp) ; ---------------------------------- ; Use the OAuth2 token in a request. ; For example... sbXml.i = CkStringBuilder::ckCreate() If sbXml.i = 0 Debug "Failed to create object." ProcedureReturn EndIf success = CkStringBuilder::ckLoadFile(sbXml,"c:/someDir/someXmlFile.xml","utf-8") If success = 0 Debug "Failed to load the XML file." CkHttp::ckDispose(http) CkHttpRequest::ckDispose(req) CkStringBuilder::ckDispose(sbResponseBody) CkJsonObject::ckDispose(jResp) CkStringBuilder::ckDispose(sbXml) ProcedureReturn EndIf ; Get the OAuth2 token and use it for authentication CkHttp::setCkAuthToken(http, CkJsonObject::ckStringOf(jResp,"token")) destUrl.s = "https://your_api.azurewebsites.net/destinationUrl" resp = CkHttp::ckPostXml(http,destUrl,CkStringBuilder::ckGetAsString(sbXml),"utf-8") If CkHttp::ckLastMethodSuccess(http) = 0 Debug CkHttp::ckLastErrorText(http) CkHttp::ckDispose(http) CkHttpRequest::ckDispose(req) CkStringBuilder::ckDispose(sbResponseBody) CkJsonObject::ckDispose(jResp) CkStringBuilder::ckDispose(sbXml) ProcedureReturn EndIf respStatusCode = CkHttpResponse::ckStatusCode(resp) Debug "Response Status Code = " + Str(respStatusCode) If respStatusCode >= 400 Debug "Response Header:" Debug CkHttpResponse::ckHeader(resp) Debug "Failed." CkHttpResponse::ckDispose(resp) CkHttp::ckDispose(http) CkHttpRequest::ckDispose(req) CkStringBuilder::ckDispose(sbResponseBody) CkJsonObject::ckDispose(jResp) CkStringBuilder::ckDispose(sbXml) ProcedureReturn EndIf ; Examine the response body Debug CkHttpResponse::ckBodyStr(resp) CkHttpResponse::ckDispose(resp) CkHttp::ckDispose(http) CkHttpRequest::ckDispose(req) CkStringBuilder::ckDispose(sbResponseBody) CkJsonObject::ckDispose(jResp) CkStringBuilder::ckDispose(sbXml) ProcedureReturn EndProcedure |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.