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 SSH TunnelThis example demonstrates connecting through an SSH Tunnel w/ 2-legged OAuth for a Xero private application. This example requires Chilkat v9.5.0.64 or later
integer li_rc oleobject loo_Tunnel string ls_SshHostname integer li_SshPort integer li_Success oleobject loo_Rest oleobject loo_Channel integer li_BTls integer li_Port integer li_MaxWaitMs 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 loo_Tunnel = create oleobject li_rc = loo_Tunnel.ConnectToNewObject("Chilkat_9_5_0.Socket") if li_rc < 0 then destroy loo_Tunnel MessageBox("Error","Connecting to COM object failed") return end if ls_SshHostname = "www.my-ssh-server.com" li_SshPort = 22 // Connect to an SSH server and establish the SSH tunnel: li_Success = loo_Tunnel.SshOpenTunnel(ls_SshHostname,li_SshPort) if li_Success <> 1 then Write-Debug loo_Tunnel.LastErrorText destroy loo_Tunnel return end if // Authenticate with the SSH server via a login/password // or with a public key. // This example demonstrates SSH password authentication. li_Success = loo_Tunnel.SshAuthenticatePw("mySshLogin","mySshPassword") if li_Success <> 1 then Write-Debug loo_Tunnel.LastErrorText destroy loo_Tunnel return end if // OK, the SSH tunnel is setup. Now open a channel within the tunnel. // (Any number of channels may be created from the same SSH tunnel. // Multiple channels may coexist at the same time.) // 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") li_BTls = 1 li_Port = 443 li_MaxWaitMs = 7000 // This returns a socket object that is a single channel within the SSH tunnel. loo_Channel = loo_Tunnel.SshOpenChannel("api.xero.com",li_Port,li_BTls,li_MaxWaitMs) if loo_Tunnel.LastMethodSuccess <> 1 then Write-Debug loo_Tunnel.LastErrorText destroy loo_Tunnel destroy loo_Rest return end if // Use the connection. (This connection is a TLS running on an SSH channel through an SSH tunnel. // In other words, TLS is wrapped within the SSH tunnel.) li_Success = loo_Rest.UseConnection(loo_Channel,1) if li_Success <> 1 then Write-Debug loo_Rest.LastErrorText destroy loo_Channel destroy loo_Tunnel destroy loo_Rest return end if // OK, we're connected. // ----------------------------------------------------------------- // 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_Tunnel destroy loo_Rest 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_Tunnel destroy loo_Rest 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_Tunnel destroy loo_Rest 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_Tunnel destroy loo_Rest 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_Tunnel destroy loo_Rest 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_Tunnel destroy loo_Rest destroy loo_Pfx destroy loo_PrivKeyFromPem destroy loo_Oauth1 destroy loo_SbXml destroy loo_Xml |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.