【问题标题】:Retrieve userId and send private message with linqtotwitter检索 userId 并使用 linqtotwitter 发送私人消息
【发布时间】:2016-12-27 10:58:44
【问题描述】:

我想用我在 asp.net mvc3 中的网络应用程序向 Twitter 发送私人消息。我正在使用oauth 进行授权,这是我的代码:

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

用户被授权后,他被重定向到动作CompleteAuth,我得到了令牌和tokenSecret,这里是这个动作的代码:

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

之后,当我尝试获取 userId 并向他发送直接消息时,它会在 Tweeting 中重定向到主页:

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

但是因为 auth.UserId 为空而发生异常,你知道吗?

【问题讨论】:

    标签: asp.net-mvc-3 linq-to-twitter


    【解决方案1】:

    这似乎与您之前的问题相同:

    Send Private message with LinqToTwitter

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-10
      相关资源
      最近更新 更多