如何在 MVC 中使用 OAuth 从 Twitter 获取姓名、电子邮件、性别等? [英] How to get name, email, gender etc from Twitter using OAuth in MVC?

查看:65
本文介绍了如何在 MVC 中使用 OAuth 从 Twitter 获取姓名、电子邮件、性别等?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 OAuth 将我的 MVC 应用程序与 twitter 连接,但我似乎无法获取外部数据?目前我正在使用这个:

 AuthenticationResult result = OAuthWebSecurity.VerifyAuthentication(Url.Action("ExternalLoginCallback", new { ReturnUrl = returnUrl }));var extraData = result.ExtraData;if (result.Provider == "facebook"){emailadres = extraData["用户名"];}else if (result.Provider == "twitter"){emailadres = extraData["用户名"];}

Facebook 确实给了我一封电子邮件,但 Twitter 似乎没有这样做......

有什么解决办法吗?

解决方案

您可以通过查询以下端点获取名称 -获取帐户/verify_credentials

它返回以下响应.但是您无法获得电子邮件或性别(请参阅 Twitter 论坛中的这些主题 - 无电子邮件, 无性别)来自此回复.

<预><代码>{"name": "马特·哈里斯","profile_sidebar_border_color": "C0DEED",profile_background_tile":假,"profile_sidebar_fill_color": "DDEEF6","location": "旧金山","profile_image_url": "http://a1.twimg.com/profile_images/554181350/matt_normal.jpg","created_at": "2007 年 2 月 17 日星期六 20:49:54 +0000","profile_link_color": "0084B4","favorites_count": 95,"url": "http://themattharris.com",contributors_enabled":假,utc_offset":-28800,身份证":777925,profile_use_background_image":真,"profile_text_color": "333333",受保护":假,followers_count":1025,"lang": "en",已验证":假,"profile_background_color": "C0DEED",geo_enabled":真,通知":假,"description": "Twitter 的开发者倡导者.也是​​一名黑客和英国侨民,与 @cindyli 结婚并住在旧金山.","time_zone": "蒂华纳",friends_count":294,status_count":2924,"profile_background_image_url": "http://s.twimg.com/a/1276711174/images/themes/theme1/bg.png",地位": {坐标":{坐标":[-122.40075845,37.78264991],类型":点"},最喜欢的":假,"created_at": "Tue Jun 22 18:17:48 +0000 2010",截断":假,"text": "浏览和更新@twitterapi 文档",贡献者":空,身份证":16789004997,地理":{坐标":[37.78264991,-122.40075845],类型":点"},in_reply_to_user_id":空,地方":空,"source": "<a href=\"http://itunes.apple.com/app/twitter/id333903271?mt=8\" rel=\"nofollow\">iPhone 版 Twitter</a>",in_reply_to_screen_name":空,in_reply_to_status_id":空},"screen_name": "thematharris",以下":假}

编辑正如 Davis 在评论中提到的,可以通过新的 API 修改获得 email.但我保留这个答案是为了支持问题的完整性.

I'm using OAuth to connect my MVC application with twitter but I can't seem to get external data? Currently I'm using this:

        AuthenticationResult result = OAuthWebSecurity.VerifyAuthentication(Url.Action("ExternalLoginCallback", new { ReturnUrl = returnUrl }));

        var extraData = result.ExtraData;
        if (result.Provider == "facebook")
        {
            emailadres = extraData["username"];
        }
        else if (result.Provider == "twitter")
        {
            emailadres = extraData["username"];
        }

Facebook does give me an email, but Twitter doesn't seem to do that...

Any solutions?

解决方案

You can get name by querying following endpoint - GET account/verify_credentials

It returns following response. But You cannot get Email or Gender (refer these threads from Twitter forums - No Email, No Gender) from this response.

{
  "name": "Matt Harris",
  "profile_sidebar_border_color": "C0DEED",
  "profile_background_tile": false,
  "profile_sidebar_fill_color": "DDEEF6",
  "location": "San Francisco",
  "profile_image_url": "http://a1.twimg.com/profile_images/554181350/matt_normal.jpg",
  "created_at": "Sat Feb 17 20:49:54 +0000 2007",
  "profile_link_color": "0084B4",
  "favourites_count": 95,
  "url": "http://themattharris.com",
  "contributors_enabled": false,
  "utc_offset": -28800,
  "id": 777925,
  "profile_use_background_image": true,
  "profile_text_color": "333333",
  "protected": false,
  "followers_count": 1025,
  "lang": "en",
  "verified": false,
  "profile_background_color": "C0DEED",
  "geo_enabled": true,
  "notifications": false,
  "description": "Developer Advocate at Twitter. Also a hacker and British expat who is married to @cindyli and lives in San Francisco.",
  "time_zone": "Tijuana",
  "friends_count": 294,
  "statuses_count": 2924,
  "profile_background_image_url": "http://s.twimg.com/a/1276711174/images/themes/theme1/bg.png",
  "status": {
    "coordinates": {
      "coordinates": [
        -122.40075845,
        37.78264991
      ],
      "type": "Point"
    },
    "favorited": false,
    "created_at": "Tue Jun 22 18:17:48 +0000 2010",
    "truncated": false,
    "text": "Going through and updating @twitterapi documentation",
    "contributors": null,
    "id": 16789004997,
    "geo": {
      "coordinates": [
        37.78264991,
        -122.40075845
      ],
      "type": "Point"
    },
    "in_reply_to_user_id": null,
    "place": null,
    "source": "<a href=\"http://itunes.apple.com/app/twitter/id333903271?mt=8\" rel=\"nofollow\">Twitter for iPhone</a>",
    "in_reply_to_screen_name": null,
    "in_reply_to_status_id": null
  },
  "screen_name": "themattharris",
  "following": false
}

EDIT As mentioned by Davis in the comment, one can get email with new API modifications. But I am keeping this answer as is to support the integrity of question.

这篇关于如何在 MVC 中使用 OAuth 从 Twitter 获取姓名、电子邮件、性别等?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆