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

PowerBuilder 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

 

 

 

(PowerBuilder) Twitter OAuth1 Authorization (3-legged)

Demonstrates 3-legged OAuth1 authorization for Twitter.

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

integer li_rc
string ls_ConsumerKey
string ls_ConsumerSecret
string ls_RequestTokenUrl
string ls_AuthorizeUrl
string ls_AccessTokenUrl
string ls_CallbackUrl
integer li_CallbackLocalPort
oleobject loo_Http
integer li_Success
oleobject loo_Req
oleobject loo_Resp
oleobject loo_HashTab
string ls_RequestToken
string ls_RequestTokenSecret
oleobject loo_SbUrlForBrowser
string ls_Url
oleobject loo_ListenSock
integer li_BackLog
integer li_MaxWaitMs
oleobject loo_Task
oleobject loo_Sock
string ls_StartLine
string ls_RequestHeader
oleobject loo_SbResponseHtml
oleobject loo_SbResponse
oleobject loo_SbStartLine
integer li_NumReplacements
string ls_AuthVerifier
string ls_AccessToken
string ls_AccessTokenSecret
string ls_UserId
string ls_ScreenName
oleobject loo_Json
oleobject loo_Fac

ls_ConsumerKey = "TWITTER_CONSUMER_KEY"
ls_ConsumerSecret = "TWITTER_CONSUMER_SECRET"

ls_RequestTokenUrl = "https://api.twitter.com/oauth/request_token"
ls_AuthorizeUrl = "https://api.twitter.com/oauth/authorize"
ls_AccessTokenUrl = "https://api.twitter.com/oauth/access_token"

// The port number is picked at random. It's some unused port that won't likely conflict with anything else..
ls_CallbackUrl = "http://localhost:3017/"
li_CallbackLocalPort = 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
loo_Http = create oleobject
// Use "Chilkat_9_5_0.Http" for versions of Chilkat < 10.0.0
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
if li_rc < 0 then
    destroy loo_Http
    MessageBox("Error","Connecting to COM object failed")
    return
end if

loo_Http.OAuth1 = 1
loo_Http.OAuthConsumerKey = ls_ConsumerKey
loo_Http.OAuthConsumerSecret = ls_ConsumerSecret

loo_Req = create oleobject
// Use "Chilkat_9_5_0.HttpRequest" for versions of Chilkat < 10.0.0
li_rc = loo_Req.ConnectToNewObject("Chilkat.HttpRequest")

loo_Req.AddParam("oauth_callback",ls_CallbackUrl)

loo_Resp = loo_Http.PostUrlEncoded(ls_RequestTokenUrl,loo_Req)
if loo_Http.LastMethodSuccess <> 1 then
    Write-Debug loo_Http.LastErrorText
    destroy loo_Http
    destroy loo_Req
    return
end if

// If successful, the resp.BodyStr contains something like this:  
// oauth_token=-Wa_KwAAAAAAxfEPAAABV8Qar4Q&oauth_token_secret=OfHY4tZBX2HK4f7yIw76WYdvnl99MVGB&oauth_callback_confirmed=true
Write-Debug loo_Resp.BodyStr

if loo_Resp.StatusCode <> 200 then
    Write-Debug "Failed response status code: " + string(loo_Resp.StatusCode)
    destroy loo_Resp
    destroy loo_Http
    destroy loo_Req
    return
end if

loo_HashTab = create oleobject
// Use "Chilkat_9_5_0.Hashtable" for versions of Chilkat < 10.0.0
li_rc = loo_HashTab.ConnectToNewObject("Chilkat.Hashtable")

loo_HashTab.AddQueryParams(loo_Resp.BodyStr)

ls_RequestToken = loo_HashTab.LookupStr("oauth_token")
ls_RequestTokenSecret = loo_HashTab.LookupStr("oauth_token_secret")
loo_Http.OAuthTokenSecret = ls_RequestTokenSecret

destroy loo_Resp

Write-Debug "oauth_token = " + ls_RequestToken
Write-Debug "oauth_token_secret = " + ls_RequestTokenSecret

// ---------------------------------------------------------------------------
// 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.
loo_SbUrlForBrowser = create oleobject
// Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0
li_rc = loo_SbUrlForBrowser.ConnectToNewObject("Chilkat.StringBuilder")

loo_SbUrlForBrowser.Append(ls_AuthorizeUrl)
loo_SbUrlForBrowser.Append("?oauth_token=")
loo_SbUrlForBrowser.Append(ls_RequestToken)
ls_Url = loo_SbUrlForBrowser.GetAsString()

// At this point, your application should load the URL in a browser.
// For example, 
// in C#: System.Diagnostics.Process.Start(url);
// in Java: Desktop.getDesktop().browse(new URI(url));
// in VBScript: Set wsh=WScript.CreateObject("WScript.Shell")
//              wsh.Run url
// 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 Microsoft 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...

// When the url is loaded into a browser, the response from Twitter 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.
loo_ListenSock = create oleobject
// Use "Chilkat_9_5_0.Socket" for versions of Chilkat < 10.0.0
li_rc = loo_ListenSock.ConnectToNewObject("Chilkat.Socket")

li_BackLog = 5
li_Success = loo_ListenSock.BindAndListen(li_CallbackLocalPort,li_BackLog)
if li_Success <> 1 then
    Write-Debug loo_ListenSock.LastErrorText
    destroy loo_Http
    destroy loo_Req
    destroy loo_HashTab
    destroy loo_SbUrlForBrowser
    destroy loo_ListenSock
    return
end if

// 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.
li_MaxWaitMs = 60000
loo_Task = loo_ListenSock.AcceptNextConnectionAsync(li_MaxWaitMs)
loo_Task.Run()

// At this point, your application should load the URL in a browser.
// For example, 
// in C#: System.Diagnostics.Process.Start(url);
// in Java: Desktop.getDesktop().browse(new URI(url));
// in VBScript: Set wsh=WScript.CreateObject("WScript.Shell")
//              wsh.Run url
// 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 Twitter 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(url);

// Wait for the listenSock's task to complete.
li_Success = loo_Task.Wait(li_MaxWaitMs)
if not li_Success OR (loo_Task.StatusInt <> 7) OR (loo_Task.TaskSuccess <> 1) then
    if not li_Success then
        // The task.LastErrorText applies to the Wait method call.
        Write-Debug loo_Task.LastErrorText
    else
        // The ResultErrorText applies to the underlying task method call (i.e. the AcceptNextConnection)
        Write-Debug loo_Task.Status
        Write-Debug loo_Task.ResultErrorText
    end if

    destroy loo_Task
    destroy loo_Http
    destroy loo_Req
    destroy loo_HashTab
    destroy loo_SbUrlForBrowser
    destroy loo_ListenSock
    return
end if

// 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.
loo_ListenSock.Close(10)

// First get the connected socket.
loo_Sock = create oleobject
// Use "Chilkat_9_5_0.Socket" for versions of Chilkat < 10.0.0
li_rc = loo_Sock.ConnectToNewObject("Chilkat.Socket")

loo_Sock.LoadTaskResult(loo_Task)
destroy loo_Task

// Read the start line of the request..
ls_StartLine = loo_Sock.ReceiveUntilMatch("~r~n")
if loo_Sock.LastMethodSuccess <> 1 then
    Write-Debug loo_Sock.LastErrorText
    destroy loo_Http
    destroy loo_Req
    destroy loo_HashTab
    destroy loo_SbUrlForBrowser
    destroy loo_ListenSock
    destroy loo_Sock
    return
end if

// Read the request header.
ls_RequestHeader = loo_Sock.ReceiveUntilMatch("~r~n~r~n")
if loo_Sock.LastMethodSuccess <> 1 then
    Write-Debug loo_Sock.LastErrorText
    destroy loo_Http
    destroy loo_Req
    destroy loo_HashTab
    destroy loo_SbUrlForBrowser
    destroy loo_ListenSock
    destroy loo_Sock
    return
end if

// 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.
loo_SbResponseHtml = create oleobject
// Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0
li_rc = loo_SbResponseHtml.ConnectToNewObject("Chilkat.StringBuilder")

loo_SbResponseHtml.Append("<html><body><p>Chilkat thanks you!</b></body</html>")

loo_SbResponse = create oleobject
// Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0
li_rc = loo_SbResponse.ConnectToNewObject("Chilkat.StringBuilder")

loo_SbResponse.Append("HTTP/1.1 200 OK~r~n")
loo_SbResponse.Append("Content-Length: ")
loo_SbResponse.AppendInt(loo_SbResponseHtml.Length)
loo_SbResponse.Append("~r~n")
loo_SbResponse.Append("Content-Type: text/html~r~n")
loo_SbResponse.Append("~r~n")
loo_SbResponse.AppendSb(loo_SbResponseHtml)

loo_Sock.SendString(loo_SbResponse.GetAsString())
loo_Sock.Close(50)

// The information we need is in the startLine.
// For example, the startLine will look like this:
//  GET /?oauth_token=abcdRQAAZZAAxfBBAAABVabcd_k&oauth_verifier=9rdOq5abcdCe6cn8M3jabcdj3Eabcd HTTP/1.1
loo_SbStartLine = create oleobject
// Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0
li_rc = loo_SbStartLine.ConnectToNewObject("Chilkat.StringBuilder")

loo_SbStartLine.Append(ls_StartLine)
li_NumReplacements = loo_SbStartLine.Replace("GET /?","")
li_NumReplacements = loo_SbStartLine.Replace(" HTTP/1.1","")
loo_SbStartLine.Trim()

// oauth_token=abcdRQAAZZAAxfBBAAABVabcd_k&oauth_verifier=9rdOq5abcdCe6cn8M3jabcdj3Eabcd
Write-Debug "startline: " + loo_SbStartLine.GetAsString()

loo_HashTab.Clear()
loo_HashTab.AddQueryParams(loo_SbStartLine.GetAsString())

ls_RequestToken = loo_HashTab.LookupStr("oauth_token")
ls_AuthVerifier = loo_HashTab.LookupStr("oauth_verifier")

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

loo_Http.OAuthToken = ls_RequestToken
loo_Http.OAuthVerifier = ls_AuthVerifier

// We don't need the "Authorization: OAuth ..." header for this POST.
loo_Http.OAuth1 = 0
loo_Req.RemoveParam("oauth_callback")
loo_Req.AddParam("oauth_verifier",ls_AuthVerifier)
loo_Req.AddParam("oauth_token",ls_RequestToken)

loo_Resp = loo_Http.PostUrlEncoded(ls_AccessTokenUrl,loo_Req)
if loo_Http.LastMethodSuccess <> 1 then
    Write-Debug loo_Http.LastErrorText
    destroy loo_Http
    destroy loo_Req
    destroy loo_HashTab
    destroy loo_SbUrlForBrowser
    destroy loo_ListenSock
    destroy loo_Sock
    destroy loo_SbResponseHtml
    destroy loo_SbResponse
    destroy loo_SbStartLine
    return
end if

// Make sure a successful response was received.
if loo_Resp.StatusCode <> 200 then
    Write-Debug loo_Resp.StatusLine
    Write-Debug loo_Resp.Header
    Write-Debug loo_Resp.BodyStr
    destroy loo_Http
    destroy loo_Req
    destroy loo_HashTab
    destroy loo_SbUrlForBrowser
    destroy loo_ListenSock
    destroy loo_Sock
    destroy loo_SbResponseHtml
    destroy loo_SbResponse
    destroy loo_SbStartLine
    return
end if

// If successful, the resp.BodyStr contains something like this:
// oauth_token=85123455-fF41296Bi3daM8eCo9Y5vZabcdxXpRv864plYPOjr&oauth_token_secret=afiYJOgabcdSfGae7BDvJVVTwys8fUGpra5guZxbmFBZo&user_id=85612355&screen_name=chilkatsoft&x_auth_expires=0
Write-Debug loo_Resp.BodyStr

loo_HashTab.Clear()
loo_HashTab.AddQueryParams(loo_Resp.BodyStr)

ls_AccessToken = loo_HashTab.LookupStr("oauth_token")
ls_AccessTokenSecret = loo_HashTab.LookupStr("oauth_token_secret")
ls_UserId = loo_HashTab.LookupStr("user_id")
ls_ScreenName = loo_HashTab.LookupStr("screen_name")

destroy loo_Resp

// The access token + secret is what should be saved and used for
// subsequent REST API calls.
Write-Debug "Access Token = " + ls_AccessToken
Write-Debug "Access Token Secret = " + ls_AccessTokenSecret
Write-Debug "user_id = " + ls_UserId
Write-Debug "screen_name  = " + ls_ScreenName

// Save this access token for future calls.
// Just in case we need user_id and screen_name, save those also..
loo_Json = create oleobject
// Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")

loo_Json.AppendString("oauth_token",ls_AccessToken)
loo_Json.AppendString("oauth_token_secret",ls_AccessTokenSecret)
loo_Json.AppendString("user_id",ls_UserId)
loo_Json.AppendString("screen_name",ls_ScreenName)

loo_Fac = create oleobject
// Use "Chilkat_9_5_0.FileAccess" for versions of Chilkat < 10.0.0
li_rc = loo_Fac.ConnectToNewObject("Chilkat.FileAccess")

loo_Fac.WriteEntireTextFile("qa_data/tokens/twitter.json",loo_Json.Emit(),"utf-8",0)

Write-Debug "Success."


destroy loo_Http
destroy loo_Req
destroy loo_HashTab
destroy loo_SbUrlForBrowser
destroy loo_ListenSock
destroy loo_Sock
destroy loo_SbResponseHtml
destroy loo_SbResponse
destroy loo_SbStartLine
destroy loo_Json
destroy loo_Fac

 

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