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
(DataFlex) Quickbooks OAuth1 Authorization (3-legged)Demonstrates 3-legged OAuth1 authorization for Quickbooks.
Use ChilkatAx-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 hoHashTab1 String sRequestToken String sRequestTokenSecret Handle hoSbUrlForBrowser String sUrlForBrowser 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 Handle hoHashTab2 String sAccessToken String sAccessTokenSecret Handle hoJson String sRealmId String sDataSource Handle hoFac String sTemp1 Integer iTemp1 Boolean bTemp1 Move "QUICKBOOKS_CONSUMER_KEY" To sConsumerKey Move "QUICKBOOKS_CONSUMER_SECRET" To sConsumerSecret Move "https://oauth.intuit.com/oauth/v1/get_request_token" To sRequestTokenUrl Move "https://appcenter.intuit.com/Connect/Begin" To sAuthorizeUrl Move "https://oauth.intuit.com/oauth/v1/get_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 Get ComStatusCode Of hoResp To iTemp1 If (iTemp1 >= 400) Begin Get ComStatusCode Of hoResp To iTemp1 Showln "Error response status code = " iTemp1 Get ComBodyStr Of hoResp To sTemp1 Showln sTemp1 Procedure_Return End // If successful, the resp.BodyStr contains this: // oauth_token=-Wa_KwAAAAAAxfEPAAABV8Qar4Q&oauth_token_secret=OfHY4tZBX2HK4f7yIw76WYdvnl99MVGB&oauth_callback_confirmed=true Get ComBodyStr Of hoResp To sTemp1 Showln sTemp1 Get Create (RefClass(cComChilkatHashtable)) To hoHashTab1 If (Not(IsComObjectCreated(hoHashTab1))) Begin Send CreateComObject of hoHashTab1 End Get ComBodyStr Of hoResp To sTemp1 Get ComAddQueryParams Of hoHashTab1 sTemp1 To iSuccess Get ComLookupStr Of hoHashTab1 "oauth_token" To sRequestToken Get ComLookupStr Of hoHashTab1 "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 ComGetAsString Of hoSbUrlForBrowser To sUrlForBrowser // When the urlForBrowser is loaded into a browser, the response from Quickbooks 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(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 Quickbooks 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... // Close it so that it's no longer listening on port 3017. Get ComClose Of hoListenSock 10 To iSuccess // The first thing to do is to 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=abcdRQAAZZAAxfBBAAABVabcd_k&oauth_verifier=9rdOq5abcdCe6cn8M3jabcdj3Eabcd 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=qyprdP04IrTDIXtP1HRZz0geQdjXHVlGDxXPexlXZsjZNRcY&oauth_verifier=arx5pj5&realmId=193514465596199&dataSource=QBO Get ComGetAsString Of hoSbStartLine To sTemp1 Showln "startline: " sTemp1 Send ComClear To hoHashTab1 Get ComGetAsString Of hoSbStartLine To sTemp1 Get ComAddQueryParams Of hoHashTab1 sTemp1 To iSuccess Get ComLookupStr Of hoHashTab1 "oauth_token" To sRequestToken Get ComLookupStr Of hoHashTab1 "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=12347455-ffffrrlaBdCjbdGfyjZabcdb5APNtuTPNabcdEpp&oauth_token_secret=RxxxxJ8mTzUhwES4xxxxuJyFWDN8ZfHmrabcddh88LmWE Get ComBodyStr Of hoResp To sTemp1 Showln sTemp1 Get Create (RefClass(cComChilkatHashtable)) To hoHashTab2 If (Not(IsComObjectCreated(hoHashTab2))) Begin Send CreateComObject of hoHashTab2 End Get ComBodyStr Of hoResp To sTemp1 Get ComAddQueryParams Of hoHashTab2 sTemp1 To iSuccess Get ComLookupStr Of hoHashTab2 "oauth_token" To sAccessToken Get ComLookupStr Of hoHashTab2 "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. 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 // Also save the realmId and dataSource from hashTab1. Get ComLookupStr Of hoHashTab1 "realmId" To sRealmId Showln "realmId = " sRealmId Get ComLookupStr Of hoHashTab1 "dataSource" To sDataSource Showln "dataSource = " sDataSource Get ComAppendString Of hoJson "realmId" sRealmId To iSuccess Get ComAppendString Of hoJson "dataSource" sDataSource 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/quickbooks.json" sTemp1 "utf-8" False To iSuccess Showln "Success." End_Procedure |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.