Capturing the Authorization URL in order to open the Twitter "Authorize App" in a pop up window using Javascript

Jan 28, 2013 at 6:09 PM

I have used this library with great success, thank you for that. One last implementation I am trying to make is that when the user clicks the button/link to authorize my app that they are routed to a pop up window (javascript) for the Twitter authorization page rather than redirecting to the authorization page then back to my website after the user has authorized.

 

Here is a code snippet from the LinqToTwitterWebFormsDemo:

 

auth = new WebAuthorizer

 {

Credentials = credentials,

PerformRedirect = authUrl => Response.Redirect(authUrl)

};


if (!Page.IsPostBack && Request.QueryString["oauth_token"] != null)

{

auth.CompleteAuthorization(Request.Url);

}

 

I am not sure whether I would have to store either the authUrl from the lambda expression or the Request.QueryString["oauth_token"] value in order to store that newly generated oauth token and append it to the Twitter oauth URL which I can direct to using a pop up window.

Am I on the right track by trying to capture these values in the code? My code is working correctly, but I can never see the value of authURL in the PerformRedirect lambda expression. The value of Request.QueryString["oauth_token"] is always null even though once I sign into my Twitter account I am successfully redirected to the Twitter authorization page for my Twitter app.

 

Any tips would be great, 

 

Thanks,

Tom

Coordinator
Jan 28, 2013 at 6:45 PM

Hi Tom,

The authUrl for redirection is a combination of the URL required to open the Twitter authorization page and the callback parameter of BeginAuthorization. When BeginAuthorization starts, it does some initial OAuth communication with Twitter, builds authUrl (which includes the callback parameter), and calls the delegate/lambda assigned to the PerformRedirect property. Twitter redirects to the callback URL (that L2T sent as a parameter during PerformRedirect) after the user authorizes your application. You then make the call to CompleteAuthorization, from the page specified by the callback URL. By passing Request.Url to CompleteAuthorization, WebAuthorizer can extract and process the results from Twitter after authorization and populate remaining credentials (OAuthToken and AccessToken) as well as ScreenName and UserID.

@JoeMayo

Dec 10, 2013 at 12:37 PM
Hi,

I have been trying to open a new window for twitter login, but it always opens up in the same page. Any help would be much appreciated. Please find my code below.

public ActionResult LogMeIn()
    {
        if (credentials.ConsumerKey == null || credentials.ConsumerSecret == null)
        {
            credentials.ConsumerKey = ConfigurationManager.AppSettings["consumerKey"];
            credentials.ConsumerSecret = ConfigurationManager.AppSettings["consumerSecret"];

        }

        auth = new MvcAuthorizer
        {
            Credentials = credentials,
            PerformRedirect = authUrl => Response.Redirect(authUrl)


        };

        auth.CompleteAuthorization(Request.Url); 

        if (!auth.IsAuthorized)
        {
            return auth.BeginAuthorization(Request.Url);
        }

        twitterCtx = new TwitterContext(auth);
        twitterCtx.UpdateStatus(DateTime.Now.ToString() + "Test");


        return View();
    }
Coordinator
Dec 11, 2013 at 5:26 PM
Hi,

The URL passed to BeginAuthorization is the callback URL that Twitter will re-direct the user to after they authorize your application.

@JoeMayo on Twitter