Chilkat Examples

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

DataFlex Web API Examples

Primary Categories

ABN AMRO
AWS Translate
Activix CRM
Adyen
Amazon DynamoDB
Amazon MWS
Amazon Pay
Amazon Rekognition
Aruba Fatturazione
Azure Maps
Azure Monitor
Azure OAuth2
Azure Storage Accounts
Bitfinex v2 REST
Bluzone
CallRail
CardConnect
Cerved
ClickBank
Clickatell
Cloudfare
Constant Contact
DocuSign
Duo Auth MFA
ETrade
Ecwid
Egypt ITIDA
Etsy
Facebook
Frame.io
GeoOp
GetHarvest
Global Payments
Google People
Google Search Console
Hungary NAV Invoicing
IBM Text to Speech
Ibanity
IntakeQ
Jira
Lightspeed
MYOB
Magento
Mailgun
MedTunnel
MercadoLibre
Microsoft Calendar

Microsoft Group
Microsoft Tasks and Plans
Microsoft Teams
Okta OAuth/OIDC
OneLogin OIDC
OneNote
PRODA
PayPal
Paynow.pl
Peoplevox
Populi
QuickBooks
Rabobank
Refinitiv
Royal Mail OBA
SCiS Schools Catalogue
SII Chile
SMSAPI
SOAP finkok.com
SendGrid
Shippo
Shopify
Shopware
Shopware 6
SimpleTexting
Square
Stripe
SugarCRM
TicketBAI
Trello
Twilio
Twitter
UniPin
VoiceBase
Vonage
Walmart
Walmart v3
WhatsApp
WiX
WooCommerce
WordPress
Xero
Yahoo Mail
Yousign
Zoom
_Miscellaneous_
eBay
effectconnect
hacienda.go.cr

 

 

 

(DataFlex) Etsy OAuth1 Authorization

See more Etsy Examples

Demonstrates 3-legged OAuth1 authorization for Etsy.

For more information, see https://www.etsy.com/developers/documentation/getting_started/oauth

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

    Move "keystring" To sConsumerKey
    Move "shared_secret" To sConsumerSecret

    // Specify one or more SPACE separated scopes as query params in the requestTokenUrl
    // See https://www.etsy.com/developers/documentation/getting_started/oauth#section_permission_scopes
    Move "https://openapi.etsy.com/v2/oauth/request_token?scope=email_r%20listings_r%20listings_w%20listings_d" To sRequestTokenUrl
    Move "https://www.etsy.com/oauth/signin" To sAuthorizeUrl
    Move "https://openapi.etsy.com/v2/oauth/access_token" 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:  
    // login_url=https%3A%2F%2Fwww.etsy.com%2Foauth%2Fsignin%3Foauth_consumer_key%3D9ad9l1omxzbwfr2niq0ce1ly%26oauth_token%3D7116b4d0c72c2736561853d9e50113%26service%3Dv2_prod&oauth_token=7116b4d0c72c2736561853d9e50113&oauth_token_secret=3b7612b5d3&oauth_callback_confirmed=true&oauth_consumer_key=9ad9l1omxzbwfr2niq0ce1ly&oauth_callback=http%3A%2F%2Flocalhost%3A3017%2F
    Get ComBodyStr Of hoResp To sTemp1
    Showln sTemp1

    // We'll need this for later..
    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

    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 "?" To iSuccess
    Get ComBodyStr Of hoResp To sTemp1
    Get ComAppend Of hoSbUrlForBrowser sTemp1 To iSuccess
    Get ComGetAsString Of hoSbUrlForBrowser To sUrl

    Send Destroy of hoResp

    // When the url is loaded into a browser, the response from Etsy 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(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 Google 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.
    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 like this:
    //  GET /?oauth_token=a3bc8bec84acc31418b68a532e9511&oauth_verifier=b5558d37 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=a3bc8bec84acc31418b68a532e9511&oauth_verifier=b5558d37
    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=7898d7ba280dc791586dcfd26b37a9&oauth_token_secret=f2a7c267aa
    Get ComBodyStr Of hoResp To sTemp1
    Showln 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 this access token for future calls.
    // Just in case we need user_id and screen_name, save those also..
    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/etsy.json" sTemp1 "utf-8" False To iSuccess

    Showln "Success."


End_Procedure

 

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