问题描述
我想使用asp.net mvc3中的Web应用程序向Twitter发送私人消息.我正在使用oauth
进行授权,这是我的代码:
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:
在AccountController中:
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");
}
用户被授权后,他将重定向到操作CompleteAuth
,我得到令牌和tokenSecret
,这是此操作的代码:
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");
}
之后,当我尝试获取userId并将直接消息发送给他时,它将重定向到正在运行"鸣叫中的首页:
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();
}
但是发生异常是因为auth.UserId为null,你有什么主意吗?
But exception occurred because auth.UserId is null, have you any idea?
推荐答案
这似乎与您之前的问题相同:
This appears to be the same as your previous question:
乔
这篇关于检索userId并使用linqtotwitter发送私人消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!