Getting Access Token after Pin Auth has completed

Jul 30, 2011 at 1:41 PM

I'm working on a console application and have built a Desktop application that a user will use to authorize my application.  Once they have authorized the application, it will grab the AccessToken and the OAuth Token to present to the user to supply to me.  I'm able to get the OAuthToken from the OAuthTwitter object but having issues getting either the OAuthSecretToken or the AccessToken.  I figure this is a fairly easy question and the answer is probably right in front of me, but it is Saturday AM!! :-)

Thanks in advance for your help.

Jul 30, 2011 at 2:53 PM

I believe theres a pin authorizer method , i know theres a sample in the samples that shows how it works.

Jul 30, 2011 at 6:27 PM

Right.  I'm using the Pin Authorizer in the Desktop application.  I've based it off the Silverlight demo but, for whatever reason, I don't seem to be able to retrieve the OAuthSecretKey or find the AccessToken to put in the command line's credential object, unless I'm confusing names of what I need once they have entered the pin.

Coordinator
Jul 30, 2011 at 8:33 PM

Hi waldenrt,

Pin authorizer works like this:

  1. Load ConsumerToken and ConsumerSecret into Credentials.
  2. Call Authorize to kick off the process.
  3. LINQ to Twitter will take care of page redirection so the user can authorize the app.
  4. When authorized, Twitter give the user a Pin.
  5. Your application should be waiting for the user to enter the Pin.  At this point, you don't have an AccessToken or OAuthToken.
  6. After you return the pin via GetPin, LINQ to Twitter finishes the authorization process, which you don't see.
  7. The call to Authorize returns.  At this point you can read AccessToken and OAuthToken from the Credentials property of the Authorizer.

The user shouldn't ever be involved in giving you the AccessToken and OAuthToken.  Their only responsibility is in providing the Pin to your application.  You should write your code to recognize the user and store their credentials for them to prevent them from having to go through the authorization process all the time.

Hope this helps,

@JoeMayo

Jul 30, 2011 at 8:58 PM

Thanks Joe,

 

That is what I'm doing but both AccessToken and OAuthToken is coming back to the app as null.  Here is my code:

This is the first step to have them login to Twitter to authorize the app

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            IOAuthCredentials credentials = new InMemoryCredentials();

            credentials.ConsumerKey = "";
            credentials.ConsumerSecret = "";

            auth = new PinAuthorizer
            {
                Credentials = credentials,
                UseCompression = true,
                GoToTwitterAuthorization = pageLink => Dispatcher.BeginInvoke((Action)(() => webBrowser1.Navigate(new Uri(pageLink))))
            };

            auth.BeginAuthorize(resp => Dispatcher.BeginInvoke((Action)(() =>
                {
                    switch (resp.Status)
                    {
                        case TwitterErrorStatus.Success:
                            break;
                        case TwitterErrorStatus.TwitterApiError:
                        case TwitterErrorStatus.RequestProcessingException:
                            MessageBox.Show(resp.Error.ToString(), resp.Message, MessageBoxButton.OK);
                            break;
                    }
                })));

            m_twitterCtx = new TwitterContext(auth, "https://api.twitter.com/1/", "https://search.twitter.com/");
        } 



This is the next action, once they receive the PIN:

            var pin = txtTwitterPin.Text;

            auth.CompleteAuthorize(
                pin,
                completeResp => Dispatcher.BeginInvoke((Action)(() =>
                    {
                        switch (completeResp.Status)
                        {
                            case TwitterErrorStatus.Success:
                                break;
                            case TwitterErrorStatus.TwitterApiError:
                            case TwitterErrorStatus.RequestProcessingException:
                                MessageBox.Show(
                                    completeResp.Error.ToString(),
                                    completeResp.Message,
                                    MessageBoxButton.OK);
                                break;
                        }
                    })));

            txtOAuthToken.Text = auth.Credentials.OAuthToken;
            txtOAuthTokenSecret.Text = auth.Credentials.AccessToken;

I know it works in the since I can do actions against the twitter application but not sure why I can't retrieve the credentials.

 

Thanks,

 

Richard

 

Coordinator
Aug 2, 2011 at 2:21 AM
Edited Aug 2, 2011 at 4:40 AM

Hi Richard,

Thanks for sticking with me while I figured this one out.  While looking at this, I found a bug where async calls are not properly populating AccessToken, which I fixed and you can download here in change set 91526:

That doesn't fully fix your problem yet.  The thing is that async calls return values in the callback. Here's what you should be doing:

            auth.CompleteAuthorize(
                pin,
                completeResp => Dispatcher.BeginInvoke((Action)(() =>
                    {
                        switch (completeResp.Status)
                        {
                            case TwitterErrorStatus.Success:
                               txtOAuthToken.Text = auth.Credentials.OAuthToken;
                               txtOAuthTokenSecret.Text = auth.Credentials.AccessToken;
                               break;
                            case TwitterErrorStatus.TwitterApiError:
                            case TwitterErrorStatus.RequestProcessingException:
                                MessageBox.Show(
                                    completeResp.Error.ToString(),
                                    completeResp.Message,
                                    MessageBoxButton.OK);
                                break;
                        }
                    })));

Notice that I moved your credential reading code into the callback, in the Success case.  The call to CompleteAuthorization starts and returns immediately, which is why you don't have the credentials.  When the operation completes, LINQ to Twitter invokes the callback, which means that all info should be available, unless there was an error.

Joe

Aug 3, 2011 at 2:18 PM

Thanks for the 2 fixes, Joe!!  That is working great now.

 

Thanks,

Richard