0

I'm trying to write an app that can tweet using an 'application' I registered with Twitter. I am using TweetSharp and have tried to get my TwitterService set up as follows:

public Twitter(string consumerKey, string consumerSecret)
    {
        this.twitterService = new TwitterService(consumerKey, consumerSecret);
        OAuthRequestToken oAuthRequestToken = this.twitterService.GetRequestToken();
        Uri uri = this.twitterService.GetAuthorizationUri(oAuthRequestToken);
        Process.Start(uri.ToString());
        OAuthAccessToken oAuthAccessToken = 
            this.twitterService.GetAccessToken(oAuthRequestToken);
        this.twitterService
            .AuthenticateWith(oAuthAccessToken.Token, oAuthAccessToken.TokenSecret);
    }

It gets to the OAuthAccessToken line and then takes me to the Authorize [my app] to use your account? page on the Twitter website. Before I specified a phony callback url, it displayed a page with the PIN that my user is supposed to enter when I clicked the 'Authorize app' button. Then when I added a phony callback url, it would attempt to go to that page and my code would blow to smithereens with the following error:

The remote server returned an error: (401) Unauthorized.

What I want to know is: can I tweet programatically without the need to enter a PIN or have a legitimate callback url?

lintmouse
  • 5,079
  • 8
  • 38
  • 54

1 Answers1

1

Tweets must be sent in the context of a user. (Ref: POST statuses/update.) Therefore, your app must get the user's authorization (an OAuth access token) in order to send a Tweet. Since you can't get an access token without using either PIN-based authentication or a callback URL, I'm afraid that what you are asking simply cannot be done.

If, however, you just want to avoid prompting your users to enter the PIN each time they start your app, then the answer is simple: Once you have a valid access token, save it somewhere (e.g. to a file) and then reload it next time your app runs. For my WinForms app, I use .NET's built-in per-user Settings mechanism to store the Access Token and Access Token Secret. A web app would probably be better off using a database or similar to persist access tokens.

Note: If you do this, you'll also need to check the validity of the stored access token, and repeat the authorization process if it's no longer valid. The Twitter API documentation suggests using the GET account/verify_credentials method for this purpose.

David Wilson
  • 177
  • 1
  • 15
  • I was hoping I could get my app to use Twitter to send log messages. I've heard of Internet of Things devices sending tweets (http://www.psfk.com/2012/01/tweeting-plants-water.html). How do they get away with it without an actual user? – lintmouse May 11 '13 at 04:34
  • @dustmouse They don't. http://www.botanicalls.com/archived_kits/twitter/ This page says that a Twitter account is required for the device to send Tweets. – David Wilson May 11 '13 at 05:45
  • Having to use a Twitter account wouldn't be a show stopper for me. Just if a user must actually interact with it to send a tweet. – lintmouse May 11 '13 at 12:54