Getting 'auth' URL

Nov 8, 2013 at 4:59 PM
Hi,

Basically, I'm working with VB.net and trying to retrive my authorisation URL with "Linq-To-Twitter". IF the user isin't already logged into their twitter account they can authorise it through the page.

Is there another way to to this? Or am I just missing a function?

Regards
Coordinator
Nov 8, 2013 at 5:26 PM
Hi,

I don't understand what you're asking.

Follow @JoeMayo
Nov 8, 2013 at 5:44 PM
Nevermind, I fixed it.

I've got another problem now, with this:
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim connect As PinAuthorizer = New PinAuthorizer()
        Dim auth As IOAuthCredentials = New InMemoryCredentials
        auth.ConsumerKey = tw_consumerkey
        auth.ConsumerSecret = tw_consumersecret
        connect.Credentials = auth
        connect.GetPin = AddressOf VerifierCallback
        connect.GoToTwitterAuthorization = Function(pageLink) Process.Start(pageLink)
        connect.Authorize()
    End Sub
So that when the user clicks the button it'll bring them to the page.

But, I'm getting an error saying that the VerifierCallBack isn't defined?
Coordinator
Nov 8, 2013 at 5:55 PM
Is this ASP.NET, Windows Forms, WPF, or something else? The Verifier is the pin, which you give to LINQ to Twitter via the callback you assign to GetPin.
Nov 9, 2013 at 10:27 AM
Hi, it's Windows Forms.
Coordinator
Nov 9, 2013 at 8:07 PM
In that case, you need to split up the authorization process. There's a WindowsPhone demo in the downloadable source code that describes what you need to do. Here are the basic steps.
  1. Create a form just for doing OAuth.
  2. On that OAuth form, add a Web browser control, a text box, and a complete button.
  3. Whenever you need to user to authorize, send them to the OAuth form.
  4. In the page load, start the authorization process. Here's how the Windows Phone demo does it:
        void Page_Loaded(object sender, RoutedEventArgs e)
        {
            pinAuth = new PinAuthorizer
            {
                Credentials = new InMemoryCredentials 
                { 
                    ConsumerKey = "", 
                    ConsumerSecret = "" 
                },
                UseCompression = true,
                GoToTwitterAuthorization = pageLink => Dispatcher.BeginInvoke(() => OAuthWebBrowser.Navigate(new Uri(pageLink, UriKind.Absolute)))
            };

            this.pinAuth.BeginAuthorize(resp =>
                Dispatcher.BeginInvoke(() =>
                {
                    switch (resp.Status)
                    {
                        case TwitterErrorStatus.Success:
                            break;
                        case TwitterErrorStatus.TwitterApiError:
                        case TwitterErrorStatus.RequestProcessingException:
                            MessageBox.Show(
                                resp.Exception.ToString(),
                                resp.Message,
                                MessageBoxButton.OK);
                            break;
                    }
                }));
        }
The key here is assigning a callback to the GoToTwitterAuthorization so it causes the Web browser control to navigate to the URL it's given. After the user authorizes, the Web browser control will show them a PIN. The user enters the pin into the text box and clicks the button, here's the handler from the Windows Phone demo that shows what needs to be done:
        private void AuthenticateButton_Click(object sender, RoutedEventArgs e)
        {
            pinAuth.CompleteAuthorize(
                PinTextBox.Text,
                completeResp => Dispatcher.BeginInvoke(() =>
                {
                    switch (completeResp.Status)
                    {
                        case TwitterErrorStatus.Success:
                            SharedState.Authorizer = pinAuth;
                            NavigationService.GoBack();
                            break;
                        case TwitterErrorStatus.TwitterApiError:
                        case TwitterErrorStatus.RequestProcessingException:
                            MessageBox.Show(
                                completeResp.Exception.ToString(),
                                completeResp.Message,
                                MessageBoxButton.OK);
                            break;
                    }
                }));
        }
The important part here is the PIN number from the text box that you pass as the first parameter to CompleteAuthorize. That's how LINQ to Twitter gets the PIN, which Twitter also calls a verifier.

This is using the old APM style, which I added to support SL & WP. I've recently released the new LINQ to Twitter 3.0 Beta that uses the new async style in a PCL. PinAuthorizer has a BeginAuthorizeAsync and a CompleteAuthorizeAsync. It's very early and I don't have any documentation and few samples, but you might prefer the cleaner syntax.

Follow @JoeMayo