我有一个c#mvc 4网站,我在https://dev.twitter.com/apps创建了一个推特应用程序。 从那里我想在主页上有一个按钮,将用户重定向到我在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的c#包装器?
答案 0 :(得分:1)
这是一个多步骤的过程。首先,您将用户引导至Twitter以授权该应用程序,并在此重定向中为您的Twitter提供网站中的回拨URL。然后,Twitter会将用户(或者如果他们拒绝访问)引导回用于该用户代表用户发布到Twitter的代码。
您可以使用类似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");
}