登录与twitter获取用户配置文件.net

本文关键字:配置文件 net 用户 获取 twitter 登录 | 更新日期: 2023-09-27 18:11:19

我有一个web应用程序在asp.net。我想使用登录与推特获取获取用户信息。我已遵照以下文章

中所提到的一切指示行事。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返回后。在应用程序的页面加载上我输入check url its

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
        }
    }
}

登录与twitter获取用户配置文件.net

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();
}