How to do a quick test of linqToTwitter with VS 2012 .net 45 and forms.
Login with you test account on dev.twitter
Under tools/Manager
This will create the two keys that link your app on twitter to your test application.
Now lets get started
In Visual Studio (2012+), create a new windows form application.
Open the NuGet Package Manager under tools, start the console
Enter install-package linqToTwitter
https://linqtotwitter.codeplex.com/SourceControl/changeset/view/8c63d7991761#BuildScripts/ReadMe.txt
Confirm you projects package.config is up to date with:
https://linqtotwitter.codeplex.com/SourceControl/changeset/view/8c63d7991761#New/Demos/Linq2TwitterDemos_Console/packages.config
You need two more references
System.Configuration
Microsoft.VisualBasic
Follow the code below:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
//add references
using System.Configuration;
//this is for the messagebox in vb, not found in c#
using Microsoft.VisualBasic;
//this was added using NUGetPackageManager console PM>install-package linqtotwitter
using LinqToTwitter; //version 3.1.2
/*
* How to test linqToTwitter with PinAuthentication
* This is the simplest way to test as there are no callbacks from twitter.
* Written by David Dold on April 7, 2015 with VB 2012
*
*/
/* Before you begin, in dev.twitter.com, setup up a test app under Tools, Manage Your Apps
* Then add the values in base64 copy and paste directly from twitter into your consumerKey, consumeSecret
*
* <appSettings>
<add key="consumerKey" value="from dev.twitter" />
<add key="consumerSecret" value="from dev.twitter" />
<add key="ClientSettingsProvider.ServiceUri" value="" /> //
</appSettings>
*/
/* compare your packagaes.config with the requirements on codeplex
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.Bcl" version="1.1.9" targetFramework="net45" />
<package id="Microsoft.Bcl.Build" version="1.0.21" targetFramework="net45" />
<package id="Microsoft.Bcl.Compression" version="3.9.85" targetFramework="net45" />
<package id="Microsoft.Net.Http" version="2.2.28" targetFramework="net45" />
<package id="Rx-Core" version="2.2.5" targetFramework="net45" />
<package id="Rx-Interfaces" version="2.2.5" targetFramework="net45" />
<package id="Rx-Linq" version="2.2.5" targetFramework="net45" />
<package id="Rx-Main" version="2.2.5" targetFramework="net45" />
<package id="Rx-PlatformServices" version="2.2.5" targetFramework="net45" />
</packages>
*/
namespace TwitterTester
{
public partial class Form1 : Form
{
//be explicit, what is this vb?
PinAuthorizer auth = null;
public Form1()
{
InitializeComponent();
try
{
auth = new PinAuthorizer()
{
CredentialStore = new InMemoryCredentialStore
{
ConsumerKey = ConfigurationManager.AppSettings["consumerKey"],
ConsumerSecret = ConfigurationManager.AppSettings["consumerSecret"]
},
GoToTwitterAuthorization = pageLink => Process.Start(pageLink),
GetPin = () =>
{
return (string)Interaction.InputBox("Enter Pin", "Twitter", string.Empty, -1,-1);
}
};
//this returns void, assuming it throws an exception if something is wrong
//"waiting" is on twitter, this will load the default browser, and then prompt the currently logged in twitter users with "Authorize <<your twitter app>> to use your account?"
//from credentials above created in dev.twitter.com "Tools-> Manage Your Apps"
//you cannot "wait" on this, the handoff to the browswe will not occur
auth.AuthorizeAsync();
//this is not blocking, ideally this would block until authorized and then enable the tweet button...
button1.Enabled = true;
}
catch (Exception E)
{
lbInfo.Text = "Exception Constructor: " + E.Message;
}
}
private void button1_Click(object sender, EventArgs e)
{
try
{
using (TwitterContext twitterCtx = new TwitterContext(auth))
{
//in pure event driven programming this should be a BeginTweet, then a callback to OnFailedTweet or OnCompleteTweet.
//MS added this async/await in vs 2012, .net 45, supposedly this is the same, meaning, this is not blocking and IDispatch is still processing windows messages
Task<LinqToTwitter.Status> task = twitterCtx.TweetAsync(textBox1.Text);
task.Wait();
//Task.Result is of type LinqToTwitter.Status
if (task != null)
if (task.IsCompleted)
listBox1.Items.Add("tweeted: ID: " + task.Result.ID);
else
listBox1.Items.Add("Task failed");
else
listBox1.Items.Add("failed");
}
}
catch(Exception E)
{
lbInfo.Text="Exception TweetAsync: "+E.Message;
}
}
}
}
Run the app.
The oAuth requires a code provided by twitter.com to authenticate the currently logged in user to the default browser. Cut and past the code into the (vb) message box.
Tweet away.
Good luck,
~David
No comments:
Post a Comment