(Tweetinvi) - AuthFlow.CreateCredentialsFromVerifierCode(verifierCode, token); returns 空

(Tweetinvi) - AuthFlow.CreateCredentialsFromVerifierCode(verifierCode, token); returns null

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

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

从头开始,我去了 Tweetinvi 的维基页面:https://github.com/linvi/tweetinvi/wiki/Authentication 我复制了代码测试和调试它。

首先,我遇到了 ValidateTwitterAuth ActionResult 中的 (token = new AuthenticationToken) 为 null 的问题,但我按照有效的说明修复了该问题。但是接下来的问题是 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");
}

提前致谢

您的问题来自以下方面:

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