LinqtoTwitter, Invalid / expired Token

Aug 5, 2014 at 9:30 AM
i have a twitter a App, which will access tweets on behalf of the user who visit my website. i have used linqtotwitter library to access the twitter API. In the process of OAuth i am facing an error and the error is:
<?xml version="1.0" encoding="UTF-8"?><hash><error>Invalid / expired Token</error<request>/oauth/access_token</request> Please visit the LINQ to Twitter FAQ (at the HelpLink) for help on resolving this error.
code used for oauth is:
public partial class Twitter : System.Web.UI.Page
{
    AspNetAuthorizer auth;

    protected async void Page_Load(object sender, EventArgs e)
    {
        auth = new AspNetAuthorizer
        {
            CredentialStore = new InMemoryCredentialStore
            {
                ConsumerKey = ConfigurationManager.AppSettings["ConsumerKey"],
                ConsumerSecret = ConfigurationManager.AppSettings["ConsumerSecret"]
            },
            GoToTwitterAuthorization =
                twitterUrl => Response.Redirect(twitterUrl, false)
        };

        if (!Page.IsPostBack)
        {
            using (DBEntity model = new DBEntity())
            {
                Guid uid = Guid.Parse(get the current logged in userid in application);

                var data = ( linq query to check the user credentials i.e OAuthToken and OAuthTokenSecret exits or not).ToList();

                if (data.Count > 0)
                {
                    AspNetAuthorizer _auth;

                    _auth = new AspNetAuthorizer
                    {
                        CredentialStore = new InMemoryCredentialStore
                        {
                            ConsumerKey = ConfigurationManager.AppSettings["ConsumerKey"],
                            ConsumerSecret = ConfigurationManager.AppSettings["ConsumerSecret"],
                            OAuthToken = data[0].OAuthToken ,
                            OAuthTokenSecret = data[0].OAuthTokenSecret
                        }
                    };


                    // Remember, do not call authorize - you don't need it.
                    // auth.Authorize();

                    using (var twitterCtx = new TwitterContext(_auth))
                    {

                        var tweets = await
                                    (from tweet in twitterCtx.Status
                                     where tweet.Type == StatusType.Home
                                     select tweet)
                                    .ToListAsync();
                    }
                }
                else
                {
                    if (Request.QueryString["oauth_token"] != null)
                    {
                        await auth.CompleteAuthorizeAsync(Request.Url);

                        // This is how you access credentials after authorization.
                        // The oauthToken and oauthTokenSecret do not expire.
                        // You can use the userID to associate the credentials with the user.
                        // You can save credentials any way you want - database, isolated 
                        //   storage, etc. - it's up to you.
                        // You can retrieve and load all 4 credentials on subsequent queries 
                        //   to avoid the need to re-authorize.
                        // When you've loaded all 4 credentials, LINQ to Twitter will let you 
                        //   make queries without re-authorizing.
                        //

                        ICredentialStore credentials = auth.CredentialStore;
                        string oauthToken = credentials.OAuthToken;
                        string oauthTokenSecret = credentials.OAuthTokenSecret;
                        string screenName = credentials.ScreenName;
                        ulong userID = credentials.UserID;

                        lbl_screenname.Text = "Welcome " + credentials.ScreenName;

                        //save credentials of the user if it visits for first time
                        model.table.Add(new table
                        {
                            my db user token fields
                        });

                        model.SaveChanges();

                        Response.Redirect("~/Default.aspx", false);
                    }
                    else
                    {
                        string twitterCallbackUrl =   "http://127.0.0.1:1234/Twitter"; 
                        await auth.BeginAuthorizeAsync(new Uri(twitterCallbackUrl));
                    }
                }
            }
        }
    }
}
i am geting error on this line: await auth.CompleteAuthorizeAsync(Request.Url); what could be the problem? how i can fix this?
Coordinator
Aug 5, 2014 at 8:00 PM
Hi,

As the error message says, there's a URL at the HelpLink that you should visit to see possible reasons for the error. That link is for the FAQ:

https://linqtotwitter.codeplex.com/wikipage?title=LINQ%20to%20Twitter%20FAQ&referringTitle=Documentation

You're seeing a 401 error and there are many reasons this can happen. If you work through the list of items in the FAQ, you're likely to find the reason. Also, here's some documentation on OAuth that might help you understand it better:

https://linqtotwitter.codeplex.com/wikipage?title=Learning%20to%20use%20OAuth&referringTitle=Securing%20Your%20Applications

@JoeMayo
Aug 14, 2014 at 11:00 AM
Hi Joe,
thanx for your reply
i have tried all the steps in FAQ's everthing is fine, i have regenerated my api key and its secrect too, then what could be the problem. please suggest me something. i have configured my oauth according to this: [https://linqtotwitter.codeplex.com/wikipage?title=Implementing%20OAuth%20for%2https://linqtotwitter.codeplex.com/wikipage?title=Implementing%20OAuth%20for%20ASP.NET%20Web%20Forms&referringTitle=Learning%20to%20use%20OAuth)