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
(PowerBuilder) Xero API through an SOCKS ProxyThis example demonstrates connecting through a SOCKS proxy w/ 2-legged OAuth for a Xero private application. This example requires Chilkat v9.5.0.64 or later
integer li_rc oleobject loo_Rest oleobject loo_Socket integer li_BTls integer li_Port integer li_MaxWaitMs integer li_Success string ls_ConsumerKey string ls_ConsumerSecret oleobject loo_Pfx oleobject loo_PrivKeyFromPfx oleobject loo_PrivKeyFromPem oleobject loo_Oauth1 oleobject loo_SbXml integer li_BAutoTrim oleobject loo_Xml integer li_RecordCount integer i // This example requires Chilkat v9.5.0.64 or later // This sample code would typically be placed in a subroutine or function // where the rest object is passed by reference. // It does the OAuth1 setup and makes the initial connection. loo_Rest = create oleobject li_rc = loo_Rest.ConnectToNewObject("Chilkat_9_5_0.Rest") if li_rc < 0 then destroy loo_Rest MessageBox("Error","Connecting to COM object failed") return end if // Connect to the Xero server through an HTTP proxy, and then tell the REST object // to use the socket connection. loo_Socket = create oleobject li_rc = loo_Socket.ConnectToNewObject("Chilkat_9_5_0.Socket") // Set the SOCKS proxy domain or IP address, port, and SOCKS version number (4 or 5) loo_Socket.SocksHostname = "192.168.1.100" loo_Socket.SocksPort = 1080 loo_Socket.SocksVersion = 5 // Other properties exist for specifying a SOCKS proxy login and password, // but these are not used in this example. // Connect through the SOCKS proxy.. li_BTls = 1 li_Port = 443 li_MaxWaitMs = 5000 li_Success = loo_Socket.Connect("api.xero.com",li_Port,li_BTls,li_MaxWaitMs) if li_Success <> 1 then Write-Debug "Connect Failure Error Code: " + string(loo_Socket.ConnectFailReason) Write-Debug loo_Socket.LastErrorText destroy loo_Rest destroy loo_Socket return end if // Use the SOCKS proxied TLS connection: li_Success = loo_Rest.UseConnection(loo_Socket,1) if li_Success <> 1 then Write-Debug loo_Rest.LastErrorText destroy loo_Rest destroy loo_Socket return end if // OK, we're connected. // The UseConnection method has an internal reference to the underlying/internal socket. // The application can does not need to keep its socket object in existence. // ----------------------------------------------------------------- // Now setup the OAuth1 authenticator.. ls_ConsumerKey = "XERO_PRIVATE_APP_KEY" ls_ConsumerSecret = "XERO_PRIVATE_APP_SECRET" // Let's get our private key from our PFX (password protected), or the PEM (unprotected). // You can decide which to use. Either is OK, although I would recommend keeping your // private keys in a PFX and not in an unprotected PEM. loo_Pfx = create oleobject li_rc = loo_Pfx.ConnectToNewObject("Chilkat_9_5_0.Pfx") li_Success = loo_Pfx.LoadPfxFile("qa_data/certs/xero_private_app/public_privatekey.pfx","PFX_PASSWORD") if li_Success <> 1 then Write-Debug loo_Pfx.LastErrorText destroy loo_Rest destroy loo_Socket destroy loo_Pfx return end if loo_PrivKeyFromPfx = loo_Pfx.GetPrivateKey(0) if loo_Pfx.LastMethodSuccess <> 1 then Write-Debug loo_Pfx.LastErrorText destroy loo_Rest destroy loo_Socket destroy loo_Pfx return end if // Or we can load from a PEM.. loo_PrivKeyFromPem = create oleobject li_rc = loo_PrivKeyFromPem.ConnectToNewObject("Chilkat_9_5_0.PrivateKey") li_Success = loo_PrivKeyFromPem.LoadPemFile("qa_data/certs/xero_private_app/privatekey.pem") if li_Success <> 1 then Write-Debug loo_PrivKeyFromPem.LastErrorText destroy loo_Rest destroy loo_Socket destroy loo_Pfx destroy loo_PrivKeyFromPem return end if // Note: There are many other means for loading a private key, including // from other formats and directly from memory (i.e. not file-based). loo_Oauth1 = create oleobject li_rc = loo_Oauth1.ConnectToNewObject("Chilkat_9_5_0.OAuth1") loo_Oauth1.ConsumerKey = ls_ConsumerKey loo_Oauth1.ConsumerSecret = ls_ConsumerSecret loo_Oauth1.Token = ls_ConsumerKey loo_Oauth1.TokenSecret = ls_ConsumerSecret loo_Oauth1.SignatureMethod = "RSA-SHA1" loo_Oauth1.SetRsaKey(loo_PrivKeyFromPfx) destroy loo_PrivKeyFromPfx // Install the OAuth1 authenticator. loo_Rest.SetAuthOAuth1(loo_Oauth1,0) Write-Debug "OK, the Xero OAuth1 is initialized and the REST object is ready to make REST API calls.." // ----------------------------------------------------------------- // Make a call to verify that OAuth1 through an HTTP proxy works.. // Get the full list of contacts. loo_SbXml = create oleobject li_rc = loo_SbXml.ConnectToNewObject("Chilkat_9_5_0.StringBuilder") li_Success = loo_Rest.FullRequestNoBodySb("GET","/api.xro/2.0/Contacts",loo_SbXml) if li_Success <> 1 then Write-Debug loo_Rest.LastErrorText destroy loo_Rest destroy loo_Socket destroy loo_Pfx destroy loo_PrivKeyFromPem destroy loo_Oauth1 destroy loo_SbXml return end if // A 200 response is expected for actual success. if loo_Rest.ResponseStatusCode <> 200 then Write-Debug loo_SbXml.GetAsString() destroy loo_Rest destroy loo_Socket destroy loo_Pfx destroy loo_PrivKeyFromPem destroy loo_Oauth1 destroy loo_SbXml return end if // Iterate over the contacts.. li_BAutoTrim = 0 loo_Xml = create oleobject li_rc = loo_Xml.ConnectToNewObject("Chilkat_9_5_0.Xml") loo_Xml.LoadSb(loo_SbXml,li_BAutoTrim) loo_Xml.SaveXml("qa_cache/xero_contacts.xml") // How many records exist? li_RecordCount = loo_Xml.NumChildrenAt("Contacts") Write-Debug "numRecords = " + string(li_RecordCount) i = 0 do while i < li_RecordCount loo_Xml.I = i Write-Debug "ContactID: " + loo_Xml.GetChildContent("Contacts|Contact[i]|ContactID") i = i + 1 loop destroy loo_Rest destroy loo_Socket destroy loo_Pfx destroy loo_PrivKeyFromPem destroy loo_Oauth1 destroy loo_SbXml destroy loo_Xml |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.