1

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?

Andres
  • 2,013
  • 6
  • 42
  • 67
  • 4
    +1 for the title (even though it could probably be a bit more indicative of your actual problem =) ) – Josh Darnell Dec 16 '11 at 15:57
  • Thanks! yea didn't think about that..should I go ahead and change it? – Andres Dec 16 '11 at 16:00
  • I'm on the fence about that. It might get more attention with the current title =) Someone with the "Edit" privilege may swoop in and change it, though. – Josh Darnell Dec 16 '11 at 16:02
  • true i guess I'll leave it to the moderators to make that decision for me.. :) – Andres Dec 16 '11 at 16:05
  • I achieved this by opening a dialog form with a WebBrowser control in it, then handling the OnNavigate event to detect when the user clicked the Authorize button and passing that back to my application. Let me find the code and I'll post it as an answer – Sebastian Piu Dec 16 '11 at 16:14
  • that sounds awesome @SebastianPiu i don't have much experience with winforms I've always been a asp.net guy that's why this has been pretty difficult for me. Thanks – Andres Dec 16 '11 at 16:17
  • are you open to other components or you just want solution for tweetsharp ? – Vamsi Dec 16 '11 at 16:33
  • how bald? can you upload a photo of your problem.... – Matt Dec 16 '11 at 16:44
  • LOL @Matt I think I'm still ok with the hair but if it weren't for Sebastian by tomorrow I would of quit and gone back to asp.net programming or completely bald..LOL – Andres Dec 16 '11 at 16:53

1 Answers1

2

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;
Sebastian Piu
  • 7,838
  • 1
  • 32
  • 50