Chilkat Examples

ChilkatHOMEAndroid™Classic ASPCC++C#Mono C#.NET Core C#C# UWP/WinRTDataFlexDelphi ActiveXDelphi DLLVisual FoxProJavaLianjaMFCObjective-CPerlPHP ActiveXPHP ExtensionPowerBuilderPowerShellPureBasicCkPythonChilkat2-PythonRubySQL ServerSwift 2Swift 3/4TclUnicode CUnicode C++Visual Basic 6.0VB.NETVB.NET UWP/WinRTVBScriptXojo PluginNode.jsExcelGo

DataFlex Web API Examples

Primary Categories

AWS Translate
Activix CRM
Adyen
Amazon MWS
Azure Maps
Azure Monitor
Azure OAuth2
Azure Storage Accounts
Bluzone
CardConnect
ClickBank
Clickatell
Cloudfare
DocuSign
ETrade
Etsy
Facebook
GeoOp
GetHarvest
Jira
MYOB

Magento
Microsoft Calendar
Microsoft Group
Microsoft Tasks and Plans
Okta OAuth/OIDC
OneLogin OIDC
PayPal
Peoplevox
Populi
QuickBooks
SOAP finkok.com
Shippo
Shopify
Stripe
SugarCRM
Trello
Twitter
VoiceBase
Vonage
Walmart
Walmart v3
Xero
eBay
effectconnect

 

 

 

(DataFlex) Trello OAuth1 Authorization

Demonstrates OAuth1 authentication for Trello.

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

Use ChilkatAx-9.5.0-win32.pkg

Procedure Test
    String sConsumerKey
    String sConsumerSecret
    String sRequestTokenUrl
    String sAuthorizeUrl
    String sAccessTokenUrl
    String sCallbackUrl
    Integer iCallbackLocalPort
    Handle hoHttp
    Boolean iSuccess
    Variant vReq
    Handle hoReq
    Variant vResp
    Handle hoResp
    Handle hoHashTab
    String sRequestToken
    String sRequestTokenSecret
    Handle hoSbUrlForBrowser
    String sUrl
    Handle hoListenSock
    Integer iBackLog
    Integer iMaxWaitMs
    Variant vTask
    Handle hoTask
    Handle hoSock
    String sStartLine
    String sRequestHeader
Html    Handle hoSbResponseHtml
    Handle hoSbResponse
    Handle hoSbStartLine
    Integer iNumReplacements
    String sAuthVerifier
    String sAccessToken
    String sAccessTokenSecret
    Handle hoJson
    Handle hoFac
    String sTemp1
    Integer iTemp1
    Boolean bTemp1

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    Move "TRELLO_CONSUMER_KEY" To sConsumerKey
    Move "TRELLO_CONSUMER_SECRET" To sConsumerSecret

    Move "https://trello.com/1/OAuthGetRequestToken" To sRequestTokenUrl
    Move "https://trello.com/1/OAuthAuthorizeToken" To sAuthorizeUrl
    Move "https://trello.com/1/OAuthGetAccessToken" To sAccessTokenUrl

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

    // The 1st step in 3-legged OAuth1.0a is to send a POST to the request token URL to obtain an OAuth Request Token
    Get Create (RefClass(cComChilkatHttp)) To hoHttp
    If (Not(IsComObjectCreated(hoHttp))) Begin
        Send CreateComObject of hoHttp
    End

    Set ComOAuth1 Of hoHttp To True
    Set ComOAuthConsumerKey Of hoHttp To sConsumerKey
    Set ComOAuthConsumerSecret Of hoHttp To sConsumerSecret
    Set ComOAuthCallback Of hoHttp To sCallbackUrl

    Get Create (RefClass(cComChilkatHttpRequest)) To hoReq
    If (Not(IsComObjectCreated(hoReq))) Begin
        Send CreateComObject of hoReq
    End
    Get pvComObject of hoReq to vReq
    Get ComPostUrlEncoded Of hoHttp sRequestTokenUrl vReq To vResp
    If (IsComObject(vResp)) Begin
        Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
        Set pvComObject Of hoResp To vResp
    End
    Get ComLastMethodSuccess Of hoHttp To bTemp1
    If (bTemp1 <> True) Begin
        Get ComLastErrorText Of hoHttp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // If successful, the resp.BodyStr contains something like this:  
    // oauth_token=c173ff088a09a67389a42b1ee22366a4&oauth_token_secret=717e6015c6749fe050a923516e739dbb&oauth_callback_confirmed=true
    Get ComBodyStr Of hoResp To sTemp1
    Showln sTemp1

    Get Create (RefClass(cComChilkatHashtable)) To hoHashTab
    If (Not(IsComObjectCreated(hoHashTab))) Begin
        Send CreateComObject of hoHashTab
    End
    Get ComBodyStr Of hoResp To sTemp1
    Get ComAddQueryParams Of hoHashTab sTemp1 To iSuccess

    Get ComLookupStr Of hoHashTab "oauth_token" To sRequestToken
    Get ComLookupStr Of hoHashTab "oauth_token_secret" To sRequestTokenSecret
    Set ComOAuthTokenSecret Of hoHttp To sRequestTokenSecret

    Send Destroy of hoResp

    Showln "oauth_token = " sRequestToken
    Showln "oauth_token_secret = " sRequestTokenSecret

    // ---------------------------------------------------------------------------
    // 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.
    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbUrlForBrowser
    If (Not(IsComObjectCreated(hoSbUrlForBrowser))) Begin
        Send CreateComObject of hoSbUrlForBrowser
    End
    Get ComAppend Of hoSbUrlForBrowser sAuthorizeUrl To iSuccess
    Get ComAppend Of hoSbUrlForBrowser "?oauth_token=" To iSuccess
    Get ComAppend Of hoSbUrlForBrowser sRequestToken To iSuccess
    Get ComAppend Of hoSbUrlForBrowser "&scope=read,write,account" To iSuccess

    Get ComGetAsString Of hoSbUrlForBrowser To sUrl

    Showln "url = " sUrl

    // When the urlForBrowser is loaded into a browser, the response from Trello 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.
    Get Create (RefClass(cComChilkatSocket)) To hoListenSock
    If (Not(IsComObjectCreated(hoListenSock))) Begin
        Send CreateComObject of hoListenSock
    End

    Move 5 To iBackLog
    Get ComBindAndListen Of hoListenSock iCallbackLocalPort iBackLog To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoListenSock To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // 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.
    Move 60000 To iMaxWaitMs
    Get ComAcceptNextConnectionAsync Of hoListenSock iMaxWaitMs To vTask
    If (IsComObject(vTask)) Begin
        Get Create (RefClass(cComChilkatTask)) To hoTask
        Set pvComObject Of hoTask To vTask
    End
    Get ComRun Of hoTask To iSuccess

    //  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(urlForBrowser));
    // in VBScript: Set wsh=WScript.CreateObject("WScript.Shell")
    //               wsh.Run urlForBrowser
    //  The Trello 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.
    Get ComWait Of hoTask iMaxWaitMs To iSuccess
    Get ComStatusInt Of hoTask To iTemp1
    Get ComTaskSuccess Of hoTask To bTemp1
    If (Not iSuccess Or (iTemp1 <> 7) Or (bTemp1 <> True)) Begin
        If (Not iSuccess) Begin
            // The task.LastErrorText applies to the Wait method call.
            Get ComLastErrorText Of hoTask To sTemp1
            Showln sTemp1
        End
        Else Begin
            // The ResultErrorText applies to the underlying task method call (i.e. the AcceptNextConnection)
            Get ComStatus Of hoTask To sTemp1
            Showln sTemp1
            Get ComResultErrorText Of hoTask To sTemp1
            Showln sTemp1
        End

        Send Destroy of hoTask
        Procedure_Return
    End

    // 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.
    Get ComClose Of hoListenSock 10 To iSuccess

    // First get the connected socket.
    Get Create (RefClass(cComChilkatSocket)) To hoSock
    If (Not(IsComObjectCreated(hoSock))) Begin
        Send CreateComObject of hoSock
    End
    Get ComLoadTaskResult Of hoSock vTask To iSuccess
    Send Destroy of hoTask

    // Read the start line of the request..
    Get ComReceiveUntilMatch Of hoSock (character(13)) + (character(10)) To sStartLine
    Get ComLastMethodSuccess Of hoSock To bTemp1
    If (bTemp1 <> True) Begin
        Get ComLastErrorText Of hoSock To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Read the request header.
    Get ComReceiveUntilMatch Of hoSock (character(13)) + (character(10)) + (character(13)) + (character(10)) To sRequestHeader
    Get ComLastMethodSuccess Of hoSock To bTemp1
    If (bTemp1 <> True) Begin
        Get ComLastErrorText Of hoSock To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // 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.
    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbResponseHtml
    If (Not(IsComObjectCreated(hoSbResponseHtml))) Begin
        Send CreateComObject of hoSbResponseHtml
    End
    Get ComAppend Of hoSbResponseHtml "<html><body><p>Chilkat thanks you!</b></body</html>" To iSuccess

    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbResponse
    If (Not(IsComObjectCreated(hoSbResponse))) Begin
        Send CreateComObject of hoSbResponse
    End
    Get ComAppend Of hoSbResponse "HTTP/1.1 200 OK" + (character(13)) + (character(10)) To iSuccess
    Get ComAppend Of hoSbResponse "Content-Length: " To iSuccess
    Get ComLength Of hoSbResponseHtml To iTemp1
    Get ComAppendInt Of hoSbResponse iTemp1 To iSuccess
    Get ComAppend Of hoSbResponse (character(13)) + (character(10)) To iSuccess
    Get ComAppend Of hoSbResponse "Content-Type: text/html" + (character(13)) + (character(10)) To iSuccess
    Get ComAppend Of hoSbResponse (character(13)) + (character(10)) To iSuccess
    Get pvComObject of hoSbResponseHtml to vSbResponseHtml
    Get ComAppendSb Of hoSbResponse vSbResponseHtml To iSuccess

    Get ComGetAsString Of hoSbResponse To sTemp1
    Get ComSendString Of hoSock sTemp1 To iSuccess
    Get ComClose Of hoSock 50 To iSuccess

    // The information we need is in the startLine.
    // For example, the startLine will look something like this:
    //  GET /?oauth_token=c173ff088a09a67389b42b1ee32366a4&oauth_verifier=c65bc8eed882e04bb94023bb12c0dbef HTTP/1.1
    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbStartLine
    If (Not(IsComObjectCreated(hoSbStartLine))) Begin
        Send CreateComObject of hoSbStartLine
    End
    Get ComAppend Of hoSbStartLine sStartLine To iSuccess
    Get ComReplace Of hoSbStartLine "GET /?" "" To iNumReplacements
    Get ComReplace Of hoSbStartLine " HTTP/1.1" "" To iNumReplacements
    Get ComTrim Of hoSbStartLine To iSuccess

    // oauth_token=c173ff088a09a67389b42b1ee32366a4&oauth_verifier=c65bc8eed882e04bb94023bb12c0dbef
    Get ComGetAsString Of hoSbStartLine To sTemp1
    Showln "startline: " sTemp1

    Send ComClear To hoHashTab
    Get ComGetAsString Of hoSbStartLine To sTemp1
    Get ComAddQueryParams Of hoHashTab sTemp1 To iSuccess

    Get ComLookupStr Of hoHashTab "oauth_token" To sRequestToken
    Get ComLookupStr Of hoHashTab "oauth_verifier" To sAuthVerifier

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

    Set ComOAuthToken Of hoHttp To sRequestToken
    Set ComOAuthVerifier Of hoHttp To sAuthVerifier
    Get pvComObject of hoReq to vReq
    Get ComPostUrlEncoded Of hoHttp sAccessTokenUrl vReq To vResp
    If (IsComObject(vResp)) Begin
        Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
        Set pvComObject Of hoResp To vResp
    End
    Get ComLastMethodSuccess Of hoHttp To bTemp1
    If (bTemp1 <> True) Begin
        Get ComLastErrorText Of hoHttp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Make sure a successful response was received.
    Get ComStatusCode Of hoResp To iTemp1
    If (iTemp1 <> 200) Begin
        Get ComStatusLine Of hoResp To sTemp1
        Showln sTemp1
        Get ComHeader Of hoResp To sTemp1
        Showln sTemp1
        Get ComBodyStr Of hoResp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // If successful, the resp.BodyStr contains something like this:
    // oauth_token=4618e19f5101b7199f75aA9e678d1585576ad84fb89fa40c85c4da13589010d5&oauth_token_secret=64a997b26ea1f47105eca36ce1a5d22e
    Get ComBodyStr Of hoResp To sTemp1
    Showln "response BodyStr = " sTemp1

    Send ComClear To hoHashTab
    Get ComBodyStr Of hoResp To sTemp1
    Get ComAddQueryParams Of hoHashTab sTemp1 To iSuccess

    Get ComLookupStr Of hoHashTab "oauth_token" To sAccessToken
    Get ComLookupStr Of hoHashTab "oauth_token_secret" To sAccessTokenSecret

    Send Destroy of hoResp

    // The access token + secret is what should be saved and used for
    // subsequent REST API calls.
    Showln "Access Token = " sAccessToken
    Showln "Access Token Secret = " sAccessTokenSecret

    // Save the access token for subsequent REST API calls.
    Get Create (RefClass(cComChilkatJsonObject)) To hoJson
    If (Not(IsComObjectCreated(hoJson))) Begin
        Send CreateComObject of hoJson
    End
    Get ComAppendString Of hoJson "oauth_token" sAccessToken To iSuccess
    Get ComAppendString Of hoJson "oauth_token_secret" sAccessTokenSecret To iSuccess

    Get Create (RefClass(cComCkFileAccess)) To hoFac
    If (Not(IsComObjectCreated(hoFac))) Begin
        Send CreateComObject of hoFac
    End
    Get ComEmit Of hoJson To sTemp1
    Get ComWriteEntireTextFile Of hoFac "qa_data/tokens/trello.json" sTemp1 "utf-8" False To iSuccess

    Showln "Success."


End_Procedure

 

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