繁体   English   中英

用Twitter登录以获得用户个人资料asp.net

[英]Login with twitter to get user profile asp.net

我在asp.net中有一个Web应用程序。 我想使用Twitter登录以获取用户信息。 我已按照以下文章中提到的所有说明进行操作

http://www.aspsnippets.com/Articles/Login-with-Twitter-in-ASPNet-using-Twitter-Button.aspx

我将被重定向到Twitter应用程序,然后在身份验证后被重定向到我的本地主机应用程序。 然后我检查了用户是否被授权,但是当我尝试通过方法FetchProfile()获取用户详细信息时,出现错误。

我的代码如下:

首先单击按钮

protected void LoginTwitter(object sender, EventArgs e)
{

    if (!TwitterConnect.IsAuthorized)
    {
        TwitterConnect twitter = new TwitterConnect();
        twitter.Authorize(Request.Url.AbsoluteUri.Split('?')[0]);
    }

}

然后在从twitter认证回来之后。 在应用程序的页面加载上,我检查了其网址

http://localhost:63977/Account/Login?oauth_token=K0mECAAAAAAAxRXEAAABV44xPgc&oauth_verifier=qYLFiOlFPx4gxEu6V4AmTJG2JNjJ3nV2

然后代码检查

protected void Page_Load(object sender, EventArgs e)
{
    TwitterConnect.API_Key = HelperClasses.TwitterApiKey;
    TwitterConnect.API_Secret = HelperClasses.TwitterApiSecret;

    if (Request.QueryString["oauth_token"] != null)
    {
        //twiiter
        if (TwitterConnect.IsAuthorized)
        {
            TwitterConnect twitter = new TwitterConnect();
            //LoggedIn User Twitter Profile Details
            DataTable twitterUserDataTable = twitter.FetchProfile(); // error here
        }
    }
}

Tweetinvi提供了一个示例项目,该项目正是您想要做的: https//github.com/linvi/tweetinvi/tree/master/Examplinvi.Web

我已突出显示您感兴趣的行:

https://github.com/linvi/tweetinvi/blob/master/Examplinvi.Web/Controllers/HomeController.cs#L14-L36

您还可以在tweetinvi中找到有关身份验证的更多信息: https : //github.com/linvi/tweetinvi/wiki/Authentication

这是您要使用ASP.NET身份验证的代码段:

private IAuthenticationContext _authenticationContext;

// Step 1 : Redirect user to go on Twitter.com to authenticate
public ActionResult TwitterAuth()
{
    var appCreds = new ConsumerCredentials("CONSUMER_KEY", "CONSUMER_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()
{
    // Get some information back from the URL
    var verifierCode = Request.Params.Get("oauth_verifier");

    // Create the user credentials
    var userCreds = AuthFlow.CreateCredentialsFromVerifierCode(verifierCode, _authenticationContext);

    // Do whatever you want with the user now!
    ViewBag.User = Tweetinvi.User.GetAuthenticatedUser(userCreds);
    return View();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM