locked
LinqtoTwitter, Invalid / expired Token RRS feed

  • Question

  • User1070938216 posted

    Hi folks,

    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));
                        }
                    }
                }
            }
        }
    }

    according to the linqtotwittre documentation everything seems right.
    i am geting error on this line: await auth.CompleteAuthorizeAsync(Request.Url); what could be the problem? how i can fix this?

    Tuesday, August 5, 2014 6:22 AM

All replies

  • User-1818759697 posted

    Hi,

    This error occurs when the token you're using is either expired or invalid. Verify that the strings you're using for access token and access token secret are valid. You may have inadvertently expired the tokens and need to regenerate them.

    i am geting error on this line: await auth.CompleteAuthorizeAsync(Request.Url);

    check the URL and call back.

    Regards

    Tuesday, August 5, 2014 10:54 PM
  • User1070938216 posted

    thanx Shawn for your reply,
    actually i am authenticating the user for the first time, i don't have the token already so the possibility of token expiration doesn't exists here, token credentials are fine too i.e. APi key and Api secrect key, with the request the oauth_token and oauth_verifier are sent to,

    after user consent, the error pops up, like:

     

    Wednesday, August 6, 2014 3:53 AM