This part focuses on creating a Twitter application to facilitate working with the API. Other posts will address tweeting text-only status updates; tweeting with media; and using LinqToTwitter’s asynchronous methods inside synchronous program methods, such as a console application’s Main() method.
So you want to send a tweet from your .NET-based application. But like most normal people, you struggle to get OAuth to work, and handling responses that are a mix of HTTP status codes and in-body data is making you rip out what little hair you have left.
Welcome to the club. You’re not alone; not by a longshot.
That said, it’s difficult to find a decent walkthrough / tutorial on the basics of using LinqToTwitter to send tweets, so here’s my humble contribution to a great library.
Before We Start
In order to use the Twitter REST API, the first thing we need to do, before writing a single line of .NET, is create an application in Twitter itself.
Creating an application basically means getting permission from Twitter to use the API; it is free (as in “free beer”), and it is instantaneous. That is, we don’t have to pay to use the API (provided we remain within its rate limits) and we can begin using it immediately after creating our application (there’s no pre-approval).
Some important notes about creating Twitter applications:
- Seems obvious to say, but if you want to create a Twitter application, you must have a Twitter account.
- The REST API and other application programming options can allow you to impersonate others, and it is possible to do so with LinqToTwitter. This, however, is complicated and at best will prove Pyrrhic. Just create the application using the account you want to have making your tweets, and life will be considerably easier.
- Twitter has a lot of APIs and a lot of ways to make applications, depending on what you want to build. Throughout this series of articles, when I talk about the Twitter API and Twitter applications, I am specifically talking about the REST API version 1.1 and applications that use REST API 1.1.
- Twitter has rate limits and specific rules about what you can do with an application.
- They’ve been known to pull the rug out from under developers, especially if a program is popular, competes with one of their projects or attempts to game their service in any way. We should all be so lucky as to make something too good, but if you manage it, prepare to pay the price.
- In light of the above: Never build a for-profit application that relies on a free, third-party API. You’re only asking for trouble, because as the old saw goes: You get what you pay for.
Well that's asinine pic.twitter.com/SjRT0PyYsf
— Dave Glick (@daveaglick) September 18, 2015
The good news here is that a single mobile number can be associated with up to 10 Twitter accounts. However, to validate a mobile phone with Twitter, you must use text messaging. While most true cellular service providers’ SMS systems will work with Twitter’s SMS, most virtual text messaging services won’t.
Walkthrough: Creating A Twitter Application
To create a Twitter application, which will allow us to use the Twitter REST API and send tweets from a .NET application:
⚬ Fire up your favorite Web browser and go to https://apps.twitter.com. You need to be logged in to Twitter to create your new app, so do so if you aren’t.
⚬ Once you’re logged in, you should see a button that says “Create new app.” Click it.
You should now see the application registration page, which looks like this:
I’ve filled in the three required fields with appropriate values; you can enter any values you like that match Twitter’s rules.
Important notes about this form:
- Application names must be unique systemwide. In other words, your application’s name cannot be a name used by any other application Twitter has authorized. So, no generic application names.
- Your application’s name, description and URL are publicly viewable. Therefore, do not provide values that would embarrass your mother or compromise your security.
- Your application’s URL must somehow relate to your application. In other words, wherever this URL goes to, that place must make mention of your application.
- Do not provide a callback URL. For this tutorial, we’re not going to use it.
⚬ Scroll down, read the developer agreement, check the box indicating that you agree to the terms, and then click Create your Twitter application.
If all went according to plan, you’ll be at your application’s new home page:
Generate Access Keys And Copy Keys For Part 2
Your application is created and can be used immediately, but we have an important step to do on our application page, before we start coding: We need to create an access token and access secret, plus write down our consumer key and consumer secret.
On your application’s page, scroll down to the Application settings section, and click the “manage keys and access tokens” link.
⚬ Up near the top of the page, you’re going to see a consumer key and a consumer secret. We need to copy these keys, as LinqToTwitter is going to need them in order to log us in to the Twitter REST API.
Now we want to generate an access token and an access secret. These are the final two keys LinqToTwitter will need to allow us to use this application.
⚬ Scroll down until you see “Your Access Token.” You’ll see that you don’t have one. So click the button, under Token Actions, labeled “Create my access token.”
In a moment, you should see these new keys:
⚬ Copy down the access token and the access secret. Once you have them, you’re ready to start coding with LinqToTwitter.
Ensure Your Keys Have Write Permission
An important note about these keys: Make sure your access tokens say you have “read and write” permission. You need write permission in order to tweet.
This is the default permission grant when creating a Twitter application and generating its access keys. However, it’s possible to set your application’s permissions to read only, and if you did that, any access tokens you created will also be read only.
In other words, you won’t be able to post status updates (tweet) if your application is set to read-only, or if you use an access token that was created when your application was set to read-only, even if you later change the application to have read and write permission. (That’s because Twitter doesn’t want you to get people using your application, expecting it to only read their information; then later sending out tweets, direct messages, etc., after changing your application’s permissions.)
To correct this problem, make sure your application has read and write permission by clicking the Permissions tab, then regenerate your access tokens:
That’s it for now. In Part 2, we’ll write code to send plain-old-text tweets. In Part 3, we’ll tweet with media. And in Part 4, we’ll see how to call the asynchronous methods used by LinqToTwitter in a program method that doesn’t usually support async operations, such as the Main() argument of a console application.
All links in this post on delicious: https://delicious.com/dougvdotcom/posting-status-updates-to-twitter-via-linqtotwitter-walkthrough-tutorial-part-1