【发布时间】:2017-05-21 08:02:29
【问题描述】:
今天我试图将我的令牌身份验证与我网站的其余部分集成,但是当我测试代码时它不起作用。我知道它以前有效,但我没有改变它,所以发生了什么?
除了 Twitter,我的 Facebook 和 LinkedIn 身份验证代码也有同样的问题,这两个女巫之前也都正常工作???
从一开始我就去了 Tweetinvi 的 wiki 页面:https://github.com/linvi/tweetinvi/wiki/Authentication 我复制了代码测试并调试它。
首先我遇到了 ValidateTwitterAuth ActionResult 中的 (token = new AuthenticationToken) 为空的问题,但我按照有效的说明解决了这个问题。但接下来的问题是 ITwitterCredentials userCreds 是 null 我不知道为什么?
我该如何解决这个问题? 此问题的原因是否与阻止我的其他令牌处理程序工作的原因相同?
我的代码:
(家庭控制器的一部分)
(我确实删除了 app-id 和 app-secret,但我检查了它们,所以它们不是问题)
private IAuthenticationContext _authenticationContext;
// Step 1 : Redirect user to go on Twitter.com to authenticate
public ActionResult TwitterAuth()
{
var appCreds = new ConsumerCredentials("APP-ID", "APP-SECRET");
// Specify the url you want the user to be redirected to
var redirectURL = "http://" + Request.Url.Authority +"/Home/ValidateTwitterAuth";
_authenticationContext = AuthFlow.InitAuthentication(appCreds, redirectURL);
return new RedirectResult(_authenticationContext.AuthorizationURL);
}
public ActionResult ValidateTwitterAuth()
{
var token = new AuthenticationToken()
{
AuthorizationKey = "APP-ID",
AuthorizationSecret = "APP-SECRET",
ConsumerCredentials = new ConsumerCredentials("APP-ID", "APP-SECRET")
};
// Get some information back from the URL
var verifierCode = Request.Params.Get("oauth_verifier");
// Create the user credentials
ITwitterCredentials userCreds = AuthFlow.CreateCredentialsFromVerifierCode(verifierCode, token);
// Do whatever you want with the user now!
ViewBag.User = Tweetinvi.User.GetAuthenticatedUser(userCreds);
string access_token = userCreds.AccessToken;
string access_token_secret = userCreds.AccessTokenSecret;
return RedirectToAction("Index", "Home");
}
提前致谢
【问题讨论】:
标签: oauth asp.net-mvc-5 twitter-oauth nullreferenceexception tweetinvi