@khanh 的回答有点错误:我没有足够的声誉来发表评论,所以我会在这里发布,抱歉
uid 可以通过这种方式获得(@khanh),但昵称应该是这种方式
puts env['omniauth.auth']['info']['name']
['info'] 哈希包括:
description
,
email
(nil
在推特上),
image
(个人资料图片链接),
location
,
name
(推特用户名),
nickname
(@用户名),
urls
(网址的哈希)。
整体['omniauth.auth']
看起来是这样的:
#<OmniAuth::AuthHash
credentials=#<OmniAuth::AuthHash
secret="some-secret-xxxxxxxxxxxxxxxxxxxxxx"
token="some-token-xxxxxxxxxxxxxxxxxxxxxx"
>
extra=#<OmniAuth::AuthHash
access_token=#<OAuth::AccessToken:0x0055cab742d2b8
@token="some-token-xxxxxxxxxxxxxxxxxxxxxx",
@secret="some-secret-xxxxxxxxxxxxxxxxxxxxxx",
@consumer=#<OAuth::Consumer:0x0055cab760ba58
@key="some-consumer-key-xxxxxxxxxxxxxx",
@secret="some-consumer-secret-xxxxxxxxxxxxxx",
@options={
:signature_method=>"HMAC-SHA1",
:request_token_path=>"/oauth/request_token",
:authorize_path=>"/oauth/authenticate",
:access_token_path=>"/oauth/access_token",
:proxy=>nil,
:scheme=>:header,
:http_method=>:post,
:oauth_version=>"1.0",
:site=>"https://api.twitter.com"
},
@http=#<Net::HTTP api.twitter.com:443 open=false>,
@http_method=:post,
@uri=#<URI::HTTPS https://api.twitter.com>
>,
@params={
:oauth_token=>"some-token-xxxxxxxxxxxxxxxxxxxxxx",
"oauth_token"=>"some-token-xxxxxxxxxxxxxxxxxxxxxx",
:oauth_token_secret=>"some-secret-xxxxxxxxxxxxxxxxxxxxxx",
"oauth_token_secret"=>"some-secret-xxxxxxxxxxxxxxxxxxxxxx",
:user_id=>"000000000",
"user_id"=>"000000000",
:screen_name=>"sample_user",
"screen_name"=>"sample_user",
:x_auth_expires=>"0",
"x_auth_expires"=>"0"
},
@response=#<Net::HTTPOK 200 OK readbody=true>
>
raw_info=#<OmniAuth::AuthHash
contributors_enabled=false
created_at="Fri Sep 02 23:56:11 +0000 2011"
default_profile=false
default_profile_image=false
description="Hi my name is Sample and I'm a Twitter user."
entities=#<OmniAuth::AuthHash
description=#<OmniAuth::AuthHash
urls=[]
>
url=#<OmniAuth::AuthHash
urls=[
#<OmniAuth::AuthHash
display_url="link.to/my/blog_or_someth…"
expanded_url="http://link.to/my/blog_or_something.html"
indices=[0, 23]
url="https://t.co/SAMPLE007"
>
]
>
>
favourites_count=690
follow_request_sent=false
followers_count=42
following=false
friends_count=34
geo_enabled=false
has_extended_profile=false
id=000000000
id_str="000000000"
is_translation_enabled=false
is_translator=false
lang="ja"
listed_count=24
location="Tokyo, Japan"
name="Sample user"
notifications=false
profile_background_color="DBE9ED"
profile_background_image_url="http://abs.twimg.com/images/themes/theme17/bg.gif"
profile_background_image_url_https="https://abs.twimg.com/images/themes/theme17/bg.gif"
profile_background_tile=false
profile_banner_url="https://pbs.twimg.com/profile_banners/0000000000/00000000000"
profile_image_url="http://pbs.twimg.com/profile_images/010101010101010101/LeoGT5Xu_normal.png"
profile_image_url_https="https://pbs.twimg.com/profile_images/010101010101010101/LeoGT5Xu_normal.png"
profile_link_color="CC3366"
profile_sidebar_border_color="DBE9ED"
profile_sidebar_fill_color="E6F6F9"
profile_text_color="333333"
profile_use_background_image=true
protected=false
screen_name="sample_user"
statuses_count=61468
time_zone="Tokyo"
url="https://t.co/SAMPLE007"
utc_offset=32400
verified=false
>
>
info=#<OmniAuth::AuthHash::InfoHash
description="Hi my name is Sample and I'm a Twitter user."
email=nil
image="http://pbs.twimg.com/profile_images/010101010101010101/LeoGT5Xu_normal.png"
location="Tokyo, Japan"
name="Sample User"
nickname="sample_user"
urls=#<OmniAuth::AuthHash
Twitter="https://twitter.com/sample_user"
Website="https://t.co/SAMPLE007">
>
provider="twitter"
uid="000000000"
>
所以你可以从
env['omniauth.auth']['extra']['raw_info']['name']
env['omniauth.auth']['extra']['raw_info']['screen_name']
也。我不确定哪个(从['info']
和['raw_info']
)是更好的方法。但['raw_info']
似乎比前者给了我们更多的信息。
编辑:是的,是的,是的,这都是在维基上写的!
Auth Hash Schema - intridea/omniauth Wiki | Github
但是 Twitter 没有具体案例,所以也许我的回答毕竟是有用的!:)