-
Notifications
You must be signed in to change notification settings - Fork 30
Start Coding
To get started with the monkehTweet library you first need to instantiate the monkehTweet.cfc component.
There are two required parameters for the init() method which are:
- the consumer key
- the consumer secret
Note: Details on how to obtain these can be found in the Twitter Application Setup section of the Getting Started guide
In this example we are instantiating the component within the ColdFusion APPLICATION scope for persistence throughout the application.
<cffunction name="OnApplicationStart" access="public" returntype="boolean" output="false">
<cfscript>
application.objMonkehTweet =
createObject('component', 'com.coldfumonkeh.monkehTweet')
.init(
consumerKey = '< enter your consumer key >',
consumerSecret = '< enter your consumer secret >',
parseResults = true
);
return true;
</cfscript>
</cffunction>
The parseResult parameter allows you to contorl whether or not the responses from the API calls are returned as a literal JSON string or as a ColdFusion struct for easier debugging and manipulation. Every little helps, right?
Simply insert your consumer tokens into the method as seen above.
If you are intending to use the component to manage and interact with a sole user then you can also bypass the requirement to authenticate against the Twitter API as you should already have obtained the oauth token and oauth token secret values.
Note: Details on how to obtain these can be found in the Single User Access section of the Getting Started guide
If you have your oauth token values you can add them into the init() method as shown below, also sending in your Twitter account username:
application.objMonkehTweet =
createObject('component', 'com.coldfumonkeh.monkehTweet')
.init(
consumerKey = '< enter your consumer key >',
consumerSecret = '< enter your consumer secret >',
oauthToken = '< enter the access token >',
oauthTokenSecret = '< enter the access secret >',
userAccountName = '< enter your twitter screen name >',
parseResults = true
);
This bypasses the need for OAuth authorisation and authentication requests as you already have the access details required. You are ready to use monkehTweet instantly.
Lucky you!
The demo code available within this project is a simple but concise workflow to allow for user authentication against your Twitter application.
Open the index.cfm file included with the library.
Here you can set your callback URL value required by Twitter to redirect your user to the specified location after they have authorised your application.
authStruct = application.objMonkehTweet.getAuthorisation(
callbackURL='http://127.0.0.1:8500/monkehTweet/authorize.cfm');
Note: This value can be different from the value entered when setting up your account with Twitter. However, it must be a valid URL
Once you have entered your details into the init() method as described above and set your callback URL, re-initialize the application by passing through the reinit=1 URL parameter to reload the application.
Hit the index.cfm file in your browser.
<cfscript>
/*
Firstly we need to have the user grant access to our application.
We do this (using OAuth) through the getAuthorisation() method.
The callbackURL is optional. If not sent through, Twitter will use the callback URL it has stored for your application.
*/
authStruct = application.objMonkehTweet.getAuthorisation(
callbackURL='http://[yourdomain]/authorize.cfm');
if (authStruct.success){
// Here, the returned information is being set into the session scope.
// You could also store these into a DB (if running an application for multiple users)
session.oAuthToken = authStruct.token;
session.oAuthTokenSecret = authStruct.token_secret;
}
</cfscript>
<!--- Now, we need to relocate the user to Twitter to perform the authorisation for us --->
<cflocation url="#authStruct.authURL#" addtoken="false" />