PowerBuilder
PowerBuilder
Quickbooks OAuth1 Authorization (3-legged)
See more QuickBooks Examples
Demonstrates 3-legged OAuth1 authorization for Quickbooks.Chilkat PowerBuilder Downloads
integer li_rc
integer li_Success
string ls_ConsumerKey
string ls_ConsumerSecret
string ls_RequestTokenUrl
string ls_AuthorizeUrl
string ls_AccessTokenUrl
string ls_CallbackUrl
integer li_CallbackLocalPort
oleobject loo_Http
oleobject loo_Req
oleobject loo_Resp
oleobject loo_HashTab1
string ls_RequestToken
string ls_RequestTokenSecret
oleobject loo_SbUrlForBrowser
string ls_Url
oleobject loo_ListenSock
integer li_BackLog
oleobject loo_Sock
integer li_MaxWaitMs
oleobject loo_Task
oleobject loo_Oauth2
string ls_StartLine
string ls_RequestHeader
oleobject loo_SbResponseHtml
oleobject loo_SbResponse
oleobject loo_SbStartLine
integer li_NumReplacements
string ls_AuthVerifier
oleobject loo_HashTab2
string ls_AccessToken
string ls_AccessTokenSecret
oleobject loo_Json
string ls_RealmId
string ls_DataSource
oleobject loo_Fac
li_Success = 0
ls_ConsumerKey = "QUICKBOOKS_CONSUMER_KEY"
ls_ConsumerSecret = "QUICKBOOKS_CONSUMER_SECRET"
ls_RequestTokenUrl = "https://oauth.intuit.com/oauth/v1/get_request_token"
ls_AuthorizeUrl = "https://appcenter.intuit.com/Connect/Begin"
ls_AccessTokenUrl = "https://oauth.intuit.com/oauth/v1/get_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
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_Http.OAuthCallback = ls_CallbackUrl
loo_Req = create oleobject
li_rc = loo_Req.ConnectToNewObject("Chilkat.HttpRequest")
loo_Req.HttpVerb = "POST"
loo_Req.ContentType = "application/x-www-form-urlencoded"
loo_Resp = create oleobject
li_rc = loo_Resp.ConnectToNewObject("Chilkat.HttpResponse")
li_Success = loo_Http.HttpReq(ls_RequestTokenUrl,loo_Req,loo_Resp)
if li_Success = 0 then
Write-Debug loo_Http.LastErrorText
destroy loo_Http
destroy loo_Req
destroy loo_Resp
return
end if
if loo_Resp.StatusCode >= 400 then
Write-Debug "Error response status code = " + string(loo_Resp.StatusCode)
Write-Debug loo_Resp.BodyStr
destroy loo_Http
destroy loo_Req
destroy loo_Resp
return
end if
// If successful, the resp.BodyStr contains this:
// oauth_token=-Wa_KwAAAAAAxfEPAAABV8Qar4Q&oauth_token_secret=OfHY4tZBX2HK4f7yIw76WYdvnl99MVGB&oauth_callback_confirmed=true
Write-Debug loo_Resp.BodyStr
loo_HashTab1 = create oleobject
li_rc = loo_HashTab1.ConnectToNewObject("Chilkat.Hashtable")
loo_HashTab1.AddQueryParams(loo_Resp.BodyStr)
ls_RequestToken = loo_HashTab1.LookupStr("oauth_token")
ls_RequestTokenSecret = loo_HashTab1.LookupStr("oauth_token_secret")
loo_Http.OAuthTokenSecret = ls_RequestTokenSecret
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
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()
// 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.
loo_ListenSock = create oleobject
li_rc = loo_ListenSock.ConnectToNewObject("Chilkat.Socket")
li_BackLog = 5
li_Success = loo_ListenSock.BindAndListen(li_CallbackLocalPort,li_BackLog)
if li_Success = 0 then
Write-Debug loo_ListenSock.LastErrorText
destroy loo_Http
destroy loo_Req
destroy loo_Resp
destroy loo_HashTab1
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.
loo_Sock = create oleobject
li_rc = loo_Sock.ConnectToNewObject("Chilkat.Socket")
li_MaxWaitMs = 60000
loo_Task = loo_ListenSock.AcceptNextAsync(li_MaxWaitMs,loo_Sock)
loo_Task.Run()
// Launch the system's default browser navigated to the URL.
loo_Oauth2 = create oleobject
li_rc = loo_Oauth2.ConnectToNewObject("Chilkat.OAuth2")
li_Success = loo_Oauth2.LaunchBrowser(ls_Url)
if li_Success = 0 then
Write-Debug loo_Oauth2.LastErrorText
destroy loo_Http
destroy loo_Req
destroy loo_Resp
destroy loo_HashTab1
destroy loo_SbUrlForBrowser
destroy loo_ListenSock
destroy loo_Sock
destroy loo_Oauth2
return
end if
// 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_Resp
destroy loo_HashTab1
destroy loo_SbUrlForBrowser
destroy loo_ListenSock
destroy loo_Sock
destroy loo_Oauth2
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...
// Close it so that it's no longer listening on port 3017.
loo_ListenSock.Close(10)
destroy loo_Task
// Read the start line of the request..
ls_StartLine = loo_Sock.ReceiveUntilMatch("~r~n")
if loo_Sock.LastMethodSuccess = 0 then
Write-Debug loo_Sock.LastErrorText
destroy loo_Http
destroy loo_Req
destroy loo_Resp
destroy loo_HashTab1
destroy loo_SbUrlForBrowser
destroy loo_ListenSock
destroy loo_Sock
destroy loo_Oauth2
return
end if
// Read the request header.
ls_RequestHeader = loo_Sock.ReceiveUntilMatch("~r~n~r~n")
if loo_Sock.LastMethodSuccess = 0 then
Write-Debug loo_Sock.LastErrorText
destroy loo_Http
destroy loo_Req
destroy loo_Resp
destroy loo_HashTab1
destroy loo_SbUrlForBrowser
destroy loo_ListenSock
destroy loo_Sock
destroy loo_Oauth2
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
li_rc = loo_SbResponseHtml.ConnectToNewObject("Chilkat.StringBuilder")
loo_SbResponseHtml.Append("<html><body><p>Chilkat thanks you!</b></body</html>")
loo_SbResponse = create oleobject
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
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=qyprdP04IrTDIXtP1HRZz0geQdjXHVlGDxXPexlXZsjZNRcY&oauth_verifier=arx5pj5&realmId=193514465596199&dataSource=QBO
Write-Debug "startline: " + loo_SbStartLine.GetAsString()
loo_HashTab1.Clear()
loo_HashTab1.AddQueryParams(loo_SbStartLine.GetAsString())
ls_RequestToken = loo_HashTab1.LookupStr("oauth_token")
ls_AuthVerifier = loo_HashTab1.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
loo_Req.HttpVerb = "POST"
loo_Req.ContentType = "application/x-www-form-urlencoded"
li_Success = loo_Http.HttpReq(ls_AccessTokenUrl,loo_Req,loo_Resp)
if li_Success = 0 then
Write-Debug loo_Http.LastErrorText
destroy loo_Http
destroy loo_Req
destroy loo_Resp
destroy loo_HashTab1
destroy loo_SbUrlForBrowser
destroy loo_ListenSock
destroy loo_Sock
destroy loo_Oauth2
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_Resp
destroy loo_HashTab1
destroy loo_SbUrlForBrowser
destroy loo_ListenSock
destroy loo_Sock
destroy loo_Oauth2
destroy loo_SbResponseHtml
destroy loo_SbResponse
destroy loo_SbStartLine
return
end if
// If successful, the resp.BodyStr contains something like this:
// oauth_token=12347455-ffffrrlaBdCjbdGfyjZabcdb5APNtuTPNabcdEpp&oauth_token_secret=RxxxxJ8mTzUhwES4xxxxuJyFWDN8ZfHmrabcddh88LmWE
Write-Debug loo_Resp.BodyStr
loo_HashTab2 = create oleobject
li_rc = loo_HashTab2.ConnectToNewObject("Chilkat.Hashtable")
loo_HashTab2.AddQueryParams(loo_Resp.BodyStr)
ls_AccessToken = loo_HashTab2.LookupStr("oauth_token")
ls_AccessTokenSecret = loo_HashTab2.LookupStr("oauth_token_secret")
// 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
// Save this access token for future calls.
loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")
loo_Json.AppendString("oauth_token",ls_AccessToken)
loo_Json.AppendString("oauth_token_secret",ls_AccessTokenSecret)
// Also save the realmId and dataSource from hashTab1.
ls_RealmId = loo_HashTab1.LookupStr("realmId")
Write-Debug "realmId = " + ls_RealmId
ls_DataSource = loo_HashTab1.LookupStr("dataSource")
Write-Debug "dataSource = " + ls_DataSource
loo_Json.AppendString("realmId",ls_RealmId)
loo_Json.AppendString("dataSource",ls_DataSource)
loo_Fac = create oleobject
li_rc = loo_Fac.ConnectToNewObject("Chilkat.FileAccess")
loo_Fac.WriteEntireTextFile("qa_data/tokens/quickbooks.json",loo_Json.Emit(),"utf-8",0)
Write-Debug "Success."
destroy loo_Http
destroy loo_Req
destroy loo_Resp
destroy loo_HashTab1
destroy loo_SbUrlForBrowser
destroy loo_ListenSock
destroy loo_Sock
destroy loo_Oauth2
destroy loo_SbResponseHtml
destroy loo_SbResponse
destroy loo_SbStartLine
destroy loo_HashTab2
destroy loo_Json
destroy loo_Fac