Retrieve userId and send private message with linqtotwitter

此生再无相见时 提交于 2020-01-06 08:16:34

问题


I want to send private message to Twitter with my web app in asp.net mvc3. I was doing the authorization with oauth, here is my code:

in AccountController:

     public ActionResult LogOn()
    {
        credentials.ConsumerKey = TwitterClient.ConsumerKey;
        credentials.ConsumerSecret = TwitterClient.ConsumerSecret;

        auth = new MvcAuthorizer {  Credentials = credentials };
        auth.CompleteAuthorization(Request.Url);
        if (!auth.IsAuthorized)
        {
            string callbackUrl = "http://127.0.0.1:31891/Account/CompleteAuth";
            Uri uri = new Uri(callbackUrl);
            return auth.BeginAuthorization(uri);
        }
        return RedirectToAction("Index", "Home");
     }

After the user is authorized , he is redirect to action CompleteAuth, and I get the token and tokenSecret, here is code of this action:

    public ActionResult CompleteAuth(string oauth_token, string oauth_verifier)
   {          
        string AccessToken = oauth_token;
        string AccessTokenSecret = oauth_verifier;
        TempData["AccessToken"] = AccessToken;
        TempData["TokenSecret"] = AccessTokenSecret;
        return RedirectToAction("Tweeting","Home");
    }

after, it's redirect to Home in action Tweeting, when i try to get userId and send to him direct message :

    public ActionResult Tweeting()
    {
        var auth = new MvcAuthorizer
        {
            Credentials = new InMemoryCredentials()
            {
                ConsumerKey  = TwitterClient.ConsumerKey,
                ConsumerSecret = TwitterClient.ConsumerSecret,
                //OAuthToken = (string)Session["AccessToken"],
                //AccessToken = Session["TokenSecret"]
                OAuthToken = TempData["AccessToken"] as string,
                AccessToken = TempData["TokenSecret"] as string
            }
        };
        var twitterContext = new TwitterContext(auth);                                
        var message = twitterContext.NewDirectMessage(auth.UserId, "Hi ucef, cool to discuss with you" + DateTime.Now);
        return View();
    }

But exception occurred because auth.UserId is null, have you any idea?


回答1:


This appears to be the same as your previous question:

Send Private message with LinqToTwitter

Joe



来源:https://stackoverflow.com/questions/13050638/retrieve-userid-and-send-private-message-with-linqtotwitter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!