Chilkat Examples

ChilkatHOME.NET Core C#Android™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi ActiveXDelphi DLLGoJavaLianjaMono C#Node.jsObjective-CPHP ActiveXPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

AutoIt Web API Examples

Primary Categories

ABN AMRO
AWS Secrets Manager
AWS Security Token Service
AWS Translate
Activix CRM
Adyen
Alibaba Cloud OSS
Amazon Cognito
Amazon DynamoDB
Amazon MWS
Amazon Pay
Amazon Rekognition
Amazon SP-API
Amazon Voice ID
Aruba Fatturazione
Azure Maps
Azure Monitor
Azure OAuth2
Azure Storage Accounts
Backblaze S3
Banco Inter
Belgian eHealth Platform
Bitfinex v2 REST
Bluzone
BrickLink
Bunny CDN
CallRail
CardConnect
Cerved
ClickBank
Clickatell
Cloudfare
Constant Contact
DocuSign
Duo Auth MFA
ETrade
Ecwid
Egypt ITIDA
Egypt eReceipt
Etsy
Facebook
Faire
Frame.io
GeoOp
GetHarvest
Global Payments
Google People
Google Search Console
Google Translate
Google Vision
Hungary NAV Invoicing
IBM Text to Speech
Ibanity
IntakeQ
JCC Cyprus
Jira
Lightspeed
MYOB
Magento
Mailgun
Malaysia MyInvois

Mastercard
MedTunnel
MercadoLibre
MessageMedia
Microsoft Calendar
Microsoft Group
Microsoft Tasks and Plans
Microsoft Teams
Moody's
Okta OAuth/OIDC
OneLogin OIDC
OneNote
OpenAI ChatGPT
PRODA
PayPal
Paynow.pl
Peoplevox
Populi
QuickBooks
Rabobank
Refinitiv
Royal Mail OBA
SCiS Schools Catalogue
SII Chile
SMSAPI
SOAP finkok.com
Salesforce
SendGrid
Shippo
Shopify
Shopware
Shopware 6
SimpleTexting
Square
Stripe
SugarCRM
TicketBAI
TikTok Shop
Trello
Twilio
Twitter API v2
Twitter v1
UPS
UniPin
VoiceBase
Vonage
WaTrend
Walmart v3
Wasabi
WhatsApp
WiX
WooCommerce
WordPress
Xero
Yahoo Mail
Yapily
Yousign
ZATCA
Zendesk
Zoom
_Miscellaneous_
eBay
effectconnect
hacienda.go.cr

 

 

 

(AutoIt) Xero OAuth1 Authorization (3-legged)

Demonstrates 3-legged OAuth1 authorization for Xero

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

Local $sConsumerKey = "XERO_CONSUMER_KEY"
Local $sConsumerSecret = "XERO_CONSUMER_SECRET"

Local $sRequestTokenUrl = "https://api.xero.com/oauth/RequestToken"
Local $sAuthorizeUrl = "https://api.xero.com/oauth/Authorize"
Local $sAccessTokenUrl = "https://api.xero.com/oauth/AccessToken"

; The port number is picked at random. It's some unused port that won't likely conflict with anything else..
Local $sCallbackUrl = "http://localhost:3017/"
Local $iCallbackLocalPort = 3017

; The 1st step in 3-legged OAuth1.0a is to send a POST to the request token URL to obtain an OAuth Request Token
$oHttp = ObjCreate("Chilkat.Http")
Local $bSuccess

$oHttp.OAuth1 = True
$oHttp.OAuthConsumerKey = $sConsumerKey
$oHttp.OAuthConsumerSecret = $sConsumerSecret
$oHttp.OAuthCallback = $sCallbackUrl

$oReq = ObjCreate("Chilkat.HttpRequest")
Local $oResp = $oHttp.PostUrlEncoded($sRequestTokenUrl,$oReq)
If ($oHttp.LastMethodSuccess <> True) Then
    ConsoleWrite($oHttp.LastErrorText & @CRLF)
    Exit
EndIf

; If successful, the resp.BodyStr contains something like this:  
; oauth_token=-Wa_KwAAAAAAxfEPAAABV8Qar4Q&oauth_token_secret=OfHY4tZBX2HK4f7yIw76WYdvnl99MVGB&oauth_callback_confirmed=true
ConsoleWrite($oResp.BodyStr & @CRLF)

$oHashTab = ObjCreate("Chilkat.Hashtable")
$oHashTab.AddQueryParams($oResp.BodyStr)

Local $sRequestToken = $oHashTab.LookupStr("oauth_token")
Local $sRequestTokenSecret = $oHashTab.LookupStr("oauth_token_secret")
$oHttp.OAuthTokenSecret = $sRequestTokenSecret

ConsoleWrite("oauth_token = " & $sRequestToken & @CRLF)
ConsoleWrite("oauth_token_secret = " & $sRequestTokenSecret & @CRLF)

; ---------------------------------------------------------------------------
; The next step is to form a URL to send to the authorizeUrl
; This is an HTTP GET that we load into a popup browser.
$oSbUrlForBrowser = ObjCreate("Chilkat.StringBuilder")
$oSbUrlForBrowser.Append($sAuthorizeUrl)
$oSbUrlForBrowser.Append("?oauth_token=")
$oSbUrlForBrowser.Append($sRequestToken)
Local $sUrlForBrowser = $oSbUrlForBrowser.GetAsString()

; When the urlForBrowser is loaded into a browser, the response from Xero will redirect back to localhost:3017
; We'll need to start a socket that is listening on port 3017 for the callback from the browser.
$oListenSock = ObjCreate("Chilkat.Socket")

Local $iBackLog = 5
$bSuccess = $oListenSock.BindAndListen($iCallbackLocalPort,$iBackLog)
If ($bSuccess <> True) Then
    ConsoleWrite($oListenSock.LastErrorText & @CRLF)
    Exit
EndIf

; Wait for the browser's connection in a background thread.
; (We'll send load the URL into the browser following this..)
; Wait a max of 60 seconds before giving up.
Local $iMaxWaitMs = 60000
Local $oTask = $oListenSock.AcceptNextConnectionAsync($iMaxWaitMs)
$oTask.Run()

;  At this point, your application should load the URL in a browser.
;  For example,
;  in C#:  System.Diagnostics.Process.Start(urlForBrowser);
;  in Java: Desktop.getDesktop().browse(new URI(urlForBrowser));
;  in VBScript: Set wsh=WScript.CreateObject("WScript.Shell")
;               wsh.Run urlForBrowser
; in Xojo: ShowURL(url)  (see http://docs.xojo.com/index.php/ShowURL)
; in Dataflex: Runprogram Background "c:\Program Files\Internet Explorer\iexplore.exe" sUrl        
;  The Xero account owner would interactively accept or deny the authorization request.

;  Add the code to load the url in a web browser here...
;  Add the code to load the url in a web browser here...
;  Add the code to load the url in a web browser here...
; System.Diagnostics.Process.Start(urlForBrowser);

; Wait for the listenSock's task to complete.
$bSuccess = $oTask.Wait($iMaxWaitMs)
If (Not $bSuccess Or ($oTask.StatusInt <> 7) Or ($oTask.TaskSuccess <> True)) Then
    If (Not $bSuccess) Then
        ; The task.LastErrorText applies to the Wait method call.
        ConsoleWrite($oTask.LastErrorText & @CRLF)
    Else
        ; The ResultErrorText applies to the underlying task method call (i.e. the AcceptNextConnection)
        ConsoleWrite($oTask.Status & @CRLF)
        ConsoleWrite($oTask.ResultErrorText & @CRLF)
    EndIf

    Exit
EndIf

; If we get to this point, the connection from the browser arrived and was accepted.

; We no longer need the listen socket...
; Stop listening on port 3017.
$oListenSock.Close(10)

; First get the connected socket.
$oSock = ObjCreate("Chilkat.Socket")
$oSock.LoadTaskResult($oTask)

; Read the start line of the request..
Local $startLine = $oSock.ReceiveUntilMatch(@CRLF)
If ($oSock.LastMethodSuccess <> True) Then
    ConsoleWrite($oSock.LastErrorText & @CRLF)
    Exit
EndIf

; Read the request header.
Local $sRequestHeader = $oSock.ReceiveUntilMatch(@CRLF & @CRLF)
If ($oSock.LastMethodSuccess <> True) Then
    ConsoleWrite($oSock.LastErrorText & @CRLF)
    Exit
EndIf

; The browser SHOULD be sending us a GET request, and therefore there is no body to the request.
; Once the request header is received, we have all of it.
; We can now send our HTTP response.
$oSbResponseHtml = ObjCreate("Chilkat.StringBuilder")
$oSbResponseHtml.Append("<html><body><p>Chilkat thanks you!</b></body</html>")

$oSbResponse = ObjCreate("Chilkat.StringBuilder")
$oSbResponse.Append("HTTP/1.1 200 OK" & @CRLF)
$oSbResponse.Append("Content-Length: ")
$oSbResponse.AppendInt($oSbResponseHtml.Length)
$oSbResponse.Append(@CRLF)
$oSbResponse.Append("Content-Type: text/html" & @CRLF)
$oSbResponse.Append(@CRLF)
$oSbResponse.AppendSb($oSbResponseHtml)

$oSock.SendString($oSbResponse.GetAsString())
$oSock.Close(50)

; The information we need is in the startLine.
; For example, the startLine will look something like this:
;  GET /?oauth_token=abcdRQAAZZAAxfBBAAABVabcd_k&oauth_verifier=9rdOq5abcdCe6cn8M3jabcdj3Eabcd&org=mUkIZabcdKEababcd189t0 HTTP/1.1
$oSbStartLine = ObjCreate("Chilkat.StringBuilder")
$oSbStartLine.Append($startLine)
Local $iNumReplacements = $oSbStartLine.Replace("GET /?","")
$iNumReplacements = $oSbStartLine.Replace(" HTTP/1.1","")
$oSbStartLine.Trim()

; oauth_token=abcdRQAAZZAAxfBBAAABVabcd_k&oauth_verifier=9rdOq5abcdCe6cn8M3jabcdj3Eabcd&org=mUkIZabcdKEababcd189t0
ConsoleWrite("startline: " & $oSbStartLine.GetAsString() & @CRLF)

$oHashTab.Clear 
$oHashTab.AddQueryParams($oSbStartLine.GetAsString())

$sRequestToken = $oHashTab.LookupStr("oauth_token")
Local $sAuthVerifier = $oHashTab.LookupStr("oauth_verifier")

; ------------------------------------------------------------------------------
; Finally , we must exchange the OAuth Request Token for an OAuth Access Token.

$oHttp.OAuthToken = $sRequestToken
$oHttp.OAuthVerifier = $sAuthVerifier
$oResp = $oHttp.PostUrlEncoded($sAccessTokenUrl,$oReq)
If ($oHttp.LastMethodSuccess <> True) Then
    ConsoleWrite($oHttp.LastErrorText & @CRLF)
    Exit
EndIf

; Make sure a successful response was received.
If ($oResp.StatusCode <> 200) Then
    ConsoleWrite($oResp.StatusLine & @CRLF)
    ConsoleWrite($oResp.Header & @CRLF)
    ConsoleWrite($oResp.BodyStr & @CRLF)
    Exit
EndIf

; If successful, the resp.BodyStr contains something like this:
; oauth_token=85123455-fF41296Bi3daM8eCo9Y5vZabcdxXpRv864plYPOjr&oauth_token_secret=afiYJOgabcdSfGae7BDvJVVTwys8fUGpra5guZxbmFBZo&oauth_expires_in=1800&xero_org_muid=abcdecNhPKabcdNjz189t0
ConsoleWrite($oResp.BodyStr & @CRLF)

$oHashTab.Clear 
$oHashTab.AddQueryParams($oResp.BodyStr)

Local $sAccessToken = $oHashTab.LookupStr("oauth_token")
Local $sAccessTokenSecret = $oHashTab.LookupStr("oauth_token_secret")
Local $sOrgMuid = $oHashTab.LookupStr("xero_org_muid")
Local $sExpiresIn = $oHashTab.LookupStr("oauth_expires_in")

; The access token + secret is what should be saved and used for
; subsequent REST API calls.
ConsoleWrite("Access Token = " & $sAccessToken & @CRLF)
ConsoleWrite("Access Token Secret = " & $sAccessTokenSecret & @CRLF)
ConsoleWrite("xero_org_muid = " & $sOrgMuid & @CRLF)
ConsoleWrite("oauth_expires_in  = " & $sExpiresIn & @CRLF)

; Save this access token for future calls.
; Just in case we need xero_org_muid and oauth_expires_in, save those also..
$oJson = ObjCreate("Chilkat.JsonObject")
$oJson.AppendString("oauth_token",$sAccessToken)
$oJson.AppendString("oauth_token_secret",$sAccessTokenSecret)
$oJson.AppendString("xero_org_muid",$sOrgMuid)
$oJson.AppendString("oauth_expires_in",$sExpiresIn)

$oFac = ObjCreate("Chilkat.FileAccess")
$oFac.WriteEntireTextFile("qa_data/tokens/xero.json",$oJson.Emit(),"utf-8",False)

ConsoleWrite("Success." & @CRLF)

 

© 2000-2024 Chilkat Software, Inc. All Rights Reserved.