Search code examples
c#winformsapitwittertweetsharp

Twitter and Winform C# app is making me go bald


Ok twitter might be great for social media but for a small app that I am trying to create just for fun has turned into a nightmare. I have searched everywhere and can't find a good explanation on how to use the api.

I am using the TweetSharp api and all I want to do is for user to allow my app and be able to post from the app to their twitter.

My problem comes when the app gives out a pin code for the user to type in...where would the user type it in at? Ok so this is what I have so far..if anyone can help i would be most greatful so I can stop pulling my hair..

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TweetSharp;
using System.Configuration;
using Hammock.Authentication.OAuth;
using System.Diagnostics;

namespace testtweet
{
    public partial class Form1 : Form
    {
        //I already have these values not showing here for obvious reasons.
        TwitterService service = new TwitterService("ConsumerKey", "ConsumerSecret");
        public Form1()
        {
            InitializeComponent();
            // Pass your credentials to the service

            // Step 1 - Retrieve an OAuth Request Token
            OAuthRequestToken requestToken = service.GetRequestToken();

            // Step 2 - Redirect to the OAuth Authorization URL
            Uri uri = service.GetAuthorizationUri(requestToken);
            Form2 frm = new Form2(uri.ToString());
            frm.Show();

            OAuthRequestToken requestToken = service.GetRequestToken();
            // Step 3 - Exchange the Request Token for an Access Token
            string verifier = "123456"; // <-- This is input into your application by your user
            OAuthAccessToken access = service.GetAccessToken(requestToken, verifier);

            // Step 4 - User authenticates using the Access Token
            service.AuthenticateWith(access.Token, access.TokenSecret);
            IEnumerable<TwitterStatus> mentions = service.ListTweetsMentioningMe();

        }

    }
}

on my Form2

public Form2(string url)
{
    InitializeComponent();
    webBrowser1.Navigate(url);

}

Here is where I am lost. As you see Step 3 is wating for the pin code..How would I set it in there? I had an idea to write directly in the app.config but how do I know when it has been generated? Should I make a 3rd form to have the user input the pin code there?


Solution

  • so this is what I've got (add a webbrowser named webBrowser1 in the designer)

    public partial class TwitterBrowser : Form
    {
        public TwitterBrowser()
        {
            InitializeComponent();
    
            webBrowser1.Navigated += OnNavigate;
        }
    
        private void OnNavigate(object sender, WebBrowserNavigatedEventArgs e)
        {
            if (e.Url.ToString() != @"https://api.twitter.com/oauth/authorize")
                return;
    
            if (webBrowser1.Document != null)
            {
                var regex = new Regex("<code>([0-9]*)</code>", RegexOptions.Multiline);
                VerificationCode = regex.Match(webBrowser1.DocumentText).Groups[1].Value;
                Close();
            }
        }
    
        public string VerificationCode { get; private set; }
    
        #region Implementation of ITwitterUserInteraction
    
        public void NavigateTo(Uri uri)
        {
            webBrowser1.Navigate(uri);
        }
    
        #endregion
    }
    

    Then use it like this:

      var browser = new TwitterBrowser();
      browser.NavigateTo(uri);
      browser.ShowDialog();
      result = browser.VerificationCode;