Created On: 06 April 2011
Problem:
Does any one know how to call Twitter as a web service from within a Visual COBOL application?
Resolution:
If you use the TweetSharp library at http://tweetsharp.codeplex.com/ and follow the instructions on the web site to download the binaries and get an OAuth key from Twitter then the following code will work to connect to Twitter and get a list of public Tweets.
Steps:
1. Download binaries .zip file from TweetSharp site and unzip it into folder called C:\tweetsharp.
2. Create a Visual COBOL managed Console project.
3. Under Project right click References->Add a reference, then browse and select C:\tweetsharp\lib\4.0\tweetsharp.dll.
4. Register your application with Twitter to get a Consumer Key and Consumer Secret number so you can authorize.
5. When you run the code below, (after replacing the ConsumerKey and ConsumerSecret values with the values that Twitter gives you) it will open up a Twitter page that will ask you to allow access and will then display a Verifier number.
6. Enter this number into the data item in the ACCEPT statement.
The code given here is a Visual COBOL version of the sample code provided on the TweetSharp site.
Steps:
1. Download binaries .zip file from TweetSharp site and unzip it into folder called C:\tweetsharp.
2. Create a Visual COBOL managed Console project.
3. Under Project right click References->Add a reference, then browse and select C:\tweetsharp\lib\4.0\tweetsharp.dll.
4. Register your application with Twitter to get a Consumer Key and Consumer Secret number so you can authorize.
5. When you run the code below, (after replacing the ConsumerKey and ConsumerSecret values with the values that Twitter gives you) it will open up a Twitter page that will ask you to allow access and will then display a Verifier number.
6. Enter this number into the data item in the ACCEPT statement.
The code given here is a Visual COBOL version of the sample code provided on the TweetSharp site.
$set ilusing"tweetsharp"
$set ilusing"System.Diagnostics"
program-id. Program1 as "testtwitter.Program1".
data division.
working-storage section.
01 service type TweetSharp.TwitterService.
01 tweets type IEnumerable[type TweetSharp.TwitterStatus].
01 tweet type TweetSharp.TwitterStatus.
01 requestToken type TweetSharp.OAuthRequestToken.
01 uri type Uri.
01 verifier string.
01 access1 type TweetSharp.OAuthAccessToken.
procedure division.
*> replace with real values of ConsumerKey and ConsumerSecret from Twitter
set service to new type TweetSharp.TwitterService("ConsumerKey", "ConsumerSecret")
*> Step 1 - Retrieve an OAuth Request Token
set requestToken to service::GetRequestToken
*> Step 2 - Redirect to the OAuth Authorization URL
set uri to service::GetAuthorizationUri(requestToken)
invoke type Process::Start(uri::ToString)
*> Step 3 - Exchange the Request Token for an Access Token
display "Enter Twitter Verifier Number:"
accept verifier *> <-- This is input into your application by your user
set access1 to service::GetAccessToken(requestToken, verifier)
*> Step 4 - User authenticates using the Access Token
invoke service::AuthenticateWith(access1::Token, access1::TokenSecret)
set tweets to service::ListTweetsOnPublicTimeline
perform varying tweet thru tweets
display tweet::User::ScreenName " says " tweet::Text
end-perform
goback.
end program Program1.