DataFlex
DataFlex
Trello OAuth1 Authorization
See more OAuth1 Examples
Demonstrates OAuth1 authentication for Trello.Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
String sConsumerKey
String sConsumerSecret
String sRequestTokenUrl
String sAuthorizeUrl
String sAccessTokenUrl
String sCallbackUrl
Integer iCallbackLocalPort
Handle hoHttp
Variant vReq
Handle hoReq
Variant vResp
Handle hoResp
Handle hoHashTab
String sRequestToken
String sRequestTokenSecret
Handle hoSbUrlForBrowser
String sUrl
Handle hoListenSock
Integer iBackLog
Variant vSock
Handle hoSock
Integer iMaxWaitMs
Variant vTask
Handle hoTask
Handle hoOauth2
String sStartLine
String sRequestHeader
Variant vSbResponseHtml
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 False To iSuccess
// 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
Set ComHttpVerb Of hoReq To "POST"
Set ComContentType Of hoReq To "application/x-www-form-urlencoded"
Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
If (Not(IsComObjectCreated(hoResp))) Begin
Send CreateComObject of hoResp
End
Get pvComObject of hoReq to vReq
Get pvComObject of hoResp to vResp
Get ComHttpReq Of hoHttp sRequestTokenUrl vReq vResp To iSuccess
If (iSuccess = False) 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
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 = False) 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.
Get Create (RefClass(cComChilkatSocket)) To hoSock
If (Not(IsComObjectCreated(hoSock))) Begin
Send CreateComObject of hoSock
End
Move 60000 To iMaxWaitMs
Get pvComObject of hoSock to vSock
Get ComAcceptNextAsync Of hoListenSock iMaxWaitMs vSock 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
// Launch the system's default browser navigated to the URL.
Get Create (RefClass(cComChilkatOAuth2)) To hoOauth2
If (Not(IsComObjectCreated(hoOauth2))) Begin
Send CreateComObject of hoOauth2
End
Get ComLaunchBrowser Of hoOauth2 sUrl To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoOauth2 To sTemp1
Showln sTemp1
Procedure_Return
End
// 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
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 = False) 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 = False) 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 Set ComHttpVerb Of hoReq To "POST"
Set ComHttpVerb Of hoReq To "POST"
Set ComContentType Of hoReq To "application/x-www-form-urlencoded"
Get pvComObject of hoReq to vReq
Get pvComObject of hoResp to vResp
Get ComHttpReq Of hoHttp sAccessTokenUrl vReq vResp To iSuccess
If (iSuccess = False) 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
// 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