0

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

在帐户控制器中:

     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 并向他发送直接消息时,它会重定向到 Home 在推文中:

    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 为空而发生异常,您知道吗?

4

1 回答 1

0

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

使用 LinqToTwitter 发送私人消息

于 2012-10-25T03:51:50.633 回答