PinAuthorizer Return URL

Nov 12, 2013 at 7:44 PM
Thanks to LinqToTwitter peeps for the great work - making life much easier for many!

I am trying to auth my app (WebForms) to send tweets on behalf of a user - I saw this thread: https://linqtotwitter.codeplex.com/discussions/462138

This works as advertised except that of course it is for console and (as a result?) once the user authorizes the app, we get a Twitter page showing the PIN and a link back to my app's Domain home page where ostensibly they can enter the pin.

Of course that is not what we want, we want to come back to our initiating WebForm preferably with Credentials(?) now containing the Users auth info.

I tried changing the pageLink parameter to reflect the return URL, expecting no effect and was not disappointed.

Pretty sure I am missing something fundamental so any guidance or a short snippet would be very much appreciated!

Thanks!
Coordinator
Nov 12, 2013 at 8:29 PM
Hi,

Pass your callback url to the call to BeginAuthorize. Then, on the callback page, add the logic to call CompleteAuthorize.

Follow @JoeMayo
Nov 12, 2013 at 9:44 PM
ah, ok - the PinAuthorizer example I initially found doesn't show those calls but I then found your example here:

https://linqtotwitter.codeplex.com/wikipage?title=Implementing%20OAuth%20for%20ASP.NET%20Web%20Forms

with WebAuthorizer which seems the more obvious choice (my bad for not finding it first I guess) - if I'm on the wrong track a heads up w be great otherwise thanks very much!
Coordinator
Nov 12, 2013 at 10:09 PM
Looks like you're on the right track.
Nov 12, 2013 at 11:27 PM
Edited Nov 12, 2013 at 11:45 PM
Hi,

I implemented the sample (url above) on a separate test page (see below) but the line
  auth.BeginAuthorization(Request.Url);
takes us to Twitter which then sends :
Whoa there! There is no request token for this page
My understanding was that the auth object would handle the back & forth for that so I'm not sure where to go from there.

I have checked the ConsumerKey and Consumer Secret and they match what shows in Twitter App Settings.

The only local kink is that I am running this in debug VS 2012 using local IIS with domain of "test.mydomain.com" and a corresponding entry in my local "hosts" file to handle the domain resolution to 127.0.0.1. The Twitter settings also show "test.mydomain.com" as the app's website and a callback URL of http://test.mydomain.com/TestTwitter.aspx

As shown below I am using
https://api.twitter.com/oauth/authorize
as the twitter auth URL

I'm stumped?



TestTwitter.aspx
using LinqToTwitter;
using LinqToTwitter.Json;
using LinqToTwitter.Common;
using LinqToTwitter.Security;
using LinqToTwitter.Serialization;

 public partial class TestTwitter : System.Web.UI.Page
    {
        private IOAuthCredentials credentials;
        private WebAuthorizer auth;
        protected void Page_Load(object sender, EventArgs e)
        {
            credentials = new SessionStateCredentials();
            if (credentials.ConsumerKey == null || credentials.ConsumerSecret == null)
            {
                credentials.ConsumerKey = ConfigurationManager.AppSettings["twitterConsumerKey"];
                credentials.ConsumerSecret = ConfigurationManager.AppSettings["twitterConsumerSecret"];
            }
            auth = new WebAuthorizer
            {
                Credentials = credentials,
                PerformRedirect = authUrl => Response.Redirect("https://api.twitter.com/oauth/authorize")
            };

            if (!IsPostBack) {
                auth.CompleteAuthorization(Request.Url);
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            auth.BeginAuthorization(Request.Url);
        }
    }
Coordinator
Nov 12, 2013 at 11:47 PM
Don't change the PerformRedirect URL - use Response.Redirect(authUrl). The URL I provide to that method is properly formed and goes to the correct URL. I think your next problem will be the test.mydomain.com because Twitter doesn't recognize that. You'll have to use localhost in development.

There's a Web Site in the downloadable source code named LinqToTwitterWebFormsDemo. If you want to see the details of what's happening watch the traffic through Fiddler and you'll see the URL it's using for the callback (oauth_callback) in the Authorization header.

You can specify your callback url like this:
        protected void Button1_Click(object sender, EventArgs e)
        {
            auth.BeginAuthorization(MyCallbackUrl);
        }
Where MyCallbackUrl contains your callback location. You'll have to build that URL so that it contains the proper domain.

Follow @JoeMayo
Marked as answer by Serexx on 11/12/2013 at 5:20 PM
Nov 12, 2013 at 11:50 PM
oh heck - just found it sorry!!

I thought authUrl had to be specified like Response.Redirect("https://api.twitter.com/oauth/authorize") - my bad!
Nov 12, 2013 at 11:52 PM
Edited Nov 13, 2013 at 12:19 AM
Thanks v much Joe!

Worked perfectly! I do greatly appreciate the help.

BTW - the "test.mydomain.com" (actually a different domain name) does work with Twitter since the redirect is done in the browser and I believe the browser actually does look at the "hosts" file (Windows\System32\Drivers\hosts) to resolve unknown DNS (I think after it looks elsewhere) and local IIS thinks the same thing, ergo....

At least that's how I think it works and I get the proper user credentials back.

Thanks again !