0

I have a web application in asp.net . I want to use login with twiiter to get get user info. I have followed all instructions mentioned in below article

http://www.aspsnippets.com/Articles/Login-with-Twitter-in-ASPNet-using-Twitter-Button.aspx

I am getting redirected to twitter app then after authentication I am being redirected to my localhost app. Then I have check user is authorized but when I try to get user details by method FetchProfile() I am getting error.

My code is as below :

First on button click

protected void LoginTwitter(object sender, EventArgs e)
{

    if (!TwitterConnect.IsAuthorized)
    {
        TwitterConnect twitter = new TwitterConnect();
        twitter.Authorize(Request.Url.AbsoluteUri.Split('?')[0]);
    }

}

then after authenticating back from twitter . on page load of application I have check url its

http://localhost:63977/Account/Login?oauth_token=K0mECAAAAAAAxRXEAAABV44xPgc&oauth_verifier=qYLFiOlFPx4gxEu6V4AmTJG2JNjJ3nV2

then code to check

protected void Page_Load(object sender, EventArgs e)
{
    TwitterConnect.API_Key = HelperClasses.TwitterApiKey;
    TwitterConnect.API_Secret = HelperClasses.TwitterApiSecret;

    if (Request.QueryString["oauth_token"] != null)
    {
        //twiiter
        if (TwitterConnect.IsAuthorized)
        {
            TwitterConnect twitter = new TwitterConnect();
            //LoggedIn User Twitter Profile Details
            DataTable twitterUserDataTable = twitter.FetchProfile(); // error here
        }
    }
}
2

Tweetinvi provides a sample project doing exactly what you want to do : https://github.com/linvi/tweetinvi/tree/master/Examplinvi.Web.

I have highlighted the lines you will be interested in :

https://github.com/linvi/tweetinvi/blob/master/Examplinvi.Web/Controllers/HomeController.cs#L14-L36

You can also find more about the Authentication in tweetinvi here : https://github.com/linvi/tweetinvi/wiki/Authentication.

Here is the snippet that you want to use of ASP.NET authentication :

private IAuthenticationContext _authenticationContext;

// Step 1 : Redirect user to go on Twitter.com to authenticate
public ActionResult TwitterAuth()
{
    var appCreds = new ConsumerCredentials("CONSUMER_KEY", "CONSUMER_SECRET");

    // Specify the url you want the user to be redirected to
    var redirectURL = "http://" + Request.Url.Authority + "/Home/ValidateTwitterAuth";
    _authenticationContext = AuthFlow.InitAuthentication(appCreds, redirectURL);

    return new RedirectResult(authenticationContext.AuthorizationURL);
}

public ActionResult ValidateTwitterAuth()
{
    // Get some information back from the URL
    var verifierCode = Request.Params.Get("oauth_verifier");

    // Create the user credentials
    var userCreds = AuthFlow.CreateCredentialsFromVerifierCode(verifierCode, _authenticationContext);

    // Do whatever you want with the user now!
    ViewBag.User = Tweetinvi.User.GetAuthenticatedUser(userCreds);
    return View();
}

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.