我正确设置了全身份验证,并且能够解析其他几个提供商响应,包括YouTube用户的一些属性,但我无法获取YouTube用户指向其频道的链接。这是响应的样子,我正在尝试获取最后一行的链接:
--- !ruby/hash:OmniAuth::AuthHash
provider: youtube
uid: http://gdata.youtube.com/feeds/api/users/DLjkduRouoiweRktkkl
info: !ruby/hash:OmniAuth::AuthHash::InfoHash
uid: http://gdata.youtube.com/feeds/api/users/DLjkduRouoiweRktkkl
nickname: Example User
email: exampleuser@gmail.com
first_name: Example
last_name: User
image: http://lh3.googleusercontent.com/-7vCkdsSDAfsU/AAAAAAAAAAA/kjRe3kjk/s88-c-k/photo.jpg
description:
location: Example City CA, US
channel_title: Example User
subscribers_count: '66'
credentials: !ruby/hash:Hashie::Mash
token: kh58.lkjASDF93-W2-ADFJkjfdo498_W8IWnjWTWY
expires_at: 1354004818
expires: true
extra: !ruby/hash:Hashie::Mash
user_hash: !ruby/hash:Hashie::Mash
xmlns: http://www.w3.org/2005/Atom
xmlns$media: http://search.yahoo.com/mrss/
xmlns$gd: http://schemas.google.com/g/2005
xmlns$yt: http://gdata.youtube.com/schemas/2007
id: !ruby/hash:Hashie::Mash
$t: http://gdata.youtube.com/feeds/api/users/DLjkduRouoiweRktkkl
published: !ruby/hash:Hashie::Mash
$t: '2012-02-14T23:39:43.000Z'
updated: !ruby/hash:Hashie::Mash
$t: '2012-11-27T06:07:22.000Z'
category:
- !ruby/hash:Hashie::Mash
scheme: http://schemas.google.com/g/2005#kind
term: http://gdata.youtube.com/schemas/2007#userProfile
title: !ruby/hash:Hashie::Mash
$t: Example User
type: text
content: !ruby/hash:Hashie::Mash
$t: Example user channel description
type: text
link:
- !ruby/hash:Hashie::Mash
rel: alternate
type: text/html
href: http://www.youtube.com/channel/kljasdSDAF98324
.
.
.
我可以在我的用户模型中执行此操作,从而在顶部获得诸如"昵称"之类的内容:
def add_omniauth(auth)
user.name = auth["info"]["nickname"]
end
但是我到底如何到达最底部的那个 URL?我试过了:
user.link = auth["extra"]["user_hash"]["link"]["href"]
但无济于事。作为一个轨道和红宝石新手,我很感激你的帮助。
好的,事实证明,链接:在 youtube 响应中是一个数组。您可以分辨,因为它没有作为哈希列在它旁边。我在这里和我的一个朋友交谈后了解到这一点。因此,为了在响应底部获得该链接:
self.url = auth["extra"]["user_hash"]["link"].first["href"]