0

我有 ac# mvc 4 网站,我在https://dev.twitter.com/apps创建了一个 twitter 应用程序。从那里我想在主页上有一个按钮,将用户重定向到我在 twitter 上的应用程序以确认访问信息。之后,该网站将在用户推特上发帖说..“我已经加入了新网站..”

我设法完成了重定向用户以允许访问信息的部分:

public ActionResult Login()
        {
            try
            {
                string url = "";
                string xml = "";
                oAuthTwitter oAuth = new oAuthTwitter();

                if (Request["oauth_token"] == null)
                {
                    //Redirect the user to Twitter for authorization.
                    //Using oauth_callback for local testing.
                    Response.Redirect(oAuth.AuthorizationLinkGet());
                }

现在我需要发布关于用户状态的帖子我该怎么做?Twitter API 1.1 有 ac# 包装器吗?

4

1 回答 1

1

这是一个多步骤的过程。首先,您将用户引导至 Twitter 以授权应用程序,并在此重定向中为 Twitter 提供您网站中的回调 URL。然后,Twitter 将使用(或如果他们拒绝访问时不使用)您将用来代表用户发布到 Twitter 的代码将用户引导回该 URL。

您可以通过使用TweetSharp之类的东西来简化很多事情,代码可能看起来像这样:

// This is when the user clicks on a link on your site to use your Twitter app
public ActionResult Twitter()
{
    // Here you provide TweetSharp with your AppID and AppSecret:
    var service = new TwitterService(AppID, AppSecret);

    // Provide TweetSharp with your site's callback URL:
    var token = service.GetRequestToken("http://www.yoursite.com/Home/TwitterCallback");

    // Get the fully-formatted URL to direct the user to, which includes your callback
    var uri = service.GetAuthorizationUri(token);
    return Redirect(uri.ToString());
}

// When twitter redirects the user here, it will contains oauth tokens if the app was authorized
public ActionResult TwitterCallback(string oauth_token, string oauth_verifier)
{
    var service = new TwitterService(AppID, AppSecret);

    // Using the values Twitter sent back, get an access token from Twitter
    var accessToken = service.GetAccessToken(new OAuthRequestToken { Token = oauth_token }, oauth_verifier);

    // Use that access token and send a tweet on the user's behalf
    service.AuthenticateWith(accessToken.Token, accessToken.TokenSecret);
    var result = service.SendTweet(new SendTweetOptions { Status = "I've joined the new web site .. " });

    // Maybe check the "result" for success or failure?

    // The interaction is done, send the user back to your app or show them a page?
    return RedirectToAction("Index", "Home");
}
于 2013-09-10T15:14:42.890 回答