【问题标题】:(Tweetinvi) - AuthFlow.CreateCredentialsFromVerifierCode(verifierCode, token); returns null(Tweetinvi) - AuthFlow.CreateCredentialsFromVerifierCode(verifierCode, token);返回空
【发布时间】:2017-05-21 08:02:29
【问题描述】:

今天我试图将我的令牌身份验证与我网站的其余部分集成,但是当我测试代码时它不起作用。我知道它以前有效,但我没有改变它,所以发生了什么?

除了 Twitter,我的 Facebook 和 LinkedIn 身份验证代码也有同样的问题,这两个女巫之前也都正常工作???

从一开始我就去了 Tweetinvi 的 wiki 页面:https://github.com/linvi/tweetinvi/wiki/Authentication 我复制了代码测试并调试它。

首先我遇到了 ValidateTwitterAuth ActionResult 中的 (token = new AuthenticationToken) 为空的问题,但我按照有效的说明解决了这个问题。但接下来的问题是 ITwitterCredentials userCredsnull 我不知道为什么?

我该如何解决这个问题? 此问题的原因是否与阻止我的其他令牌处理程序工作的原因相同?

我的代码:

(家庭控制器的一部分)

(我确实删除了 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


    【解决方案1】:

    您的问题来自以下方面:

    var token = new AuthenticationToken()
    {
        AuthorizationKey = "APP-ID",
        AuthorizationSecret = "APP-SECRET",
    };
    

    AuthorizationKeyAuthorizationSecret 不是 APP_IDAPP_SECRET

    您应该从在您的ActionResult TwitterAuth() 中创建的_authenticationContext 获取这些信息。

    有多种方法可以存储此信息,以便在回调完成时访问它。

    如果您不使用负载平衡,我建议您使用以下使用 url 参数的代码:

    public ActionResult TwitterAuth()
    {
        var appCreds = new ConsumerCredentials(MyCredentials.CONSUMER_KEY, MyCredentials.CONSUMER_SECRET);
        var redirectURL = "http://" + Request.Url.Authority + "/Home/ValidateTwitterAuth";
        var authenticationContext = AuthFlow.InitAuthentication(appCreds, redirectURL);
    
        return new RedirectResult(authenticationContext.AuthorizationURL);
    }
    
    public ActionResult ValidateTwitterAuth()
    {
        var verifierCode = Request.Params.Get("oauth_verifier");
        var authorizationId = Request.Params.Get("authorization_id");
    
        if (verifierCode != null)
        {
            var userCreds = AuthFlow.CreateCredentialsFromVerifierCode(verifierCode, authorizationId);
            var user = Tweetinvi.User.GetAuthenticatedUser(userCreds);
    
            ViewBag.User = user;
        }
    
        return View();
    }
    

    您可以在此处阅读有关此主题的更多信息:https://github.com/linvi/tweetinvi/wiki/Authentication#web-application-considerations

    【讨论】:

    • 非常感谢,我的令牌处理程序突然不再工作了,我真的感觉很糟糕。但现在它是一个下来,两个去:)
    猜你喜欢
    • 2020-12-13
    • 2019-12-25
    • 2012-11-04
    • 2019-02-18
    • 2018-04-26
    • 2018-06-04
    • 2021-02-10
    • 2017-06-08
    • 2015-03-06
    相关资源
    最近更新 更多