Google YouTube 的身份验证在 Rails 中不起作用
Google auth for YouTube doesn't work in Rails
我正在使用 Rails 5.1.1 和 yt gem 0.28
我用的是官方gem文档(link在上面)。
还有这些文档 Github
并尝试通过 SitePoint
上的本教程检查自己
如果您查看 SP 教程下方的评论 - 存在相同的问题,但您的所有者删除了问题,因此我无法使用它。
Gem 似乎工作正常。至少当我放置:
video = Yt::Video.new id: 'BPNYv0vd78A'
在video_controllers.rb中,然后询问video.description,它回答了描述。所以让我们在这里打勾。
然后我把 YT 配置成据说:
Yt.configure do |config|
config.log_level = :debug
config.client_id = "[my_client_id].apps.googleusercontent.com"
config.client_secret = "[my_secret_key]"
config.api_key = '[my_api_key]'
end
我从google_oauth2复制了值,所以如果没问题,这里也一定没问题。
但是当我尝试使用任何涉及身份验证的东西时,会发生一些非常奇怪的事情。
第一部分 - 访问令牌
根据手册,如果我有访问令牌,我可以:
account = Yt::Account.new access_token: 'ya29.[authenticating_user_token]'
account.email
消息如下:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "authError",
"message": "Invalid Credentials",
"locationType": "header",
"location": "Authorization"
}
],
"code": 401,
"message": "Invalid Credentials"
}
}
它还提供了 curl 命令来模拟这个动作:
curl -X GET -H "content-length: 0" -H "user-agent: Yt::Request (gzip)" -H "authorization: Bearer ya29.[authenticating_user_token]" -H "host: www.googleapis.com" "https://www.googleapis.com/oauth2/v2/userinfo?key=[my_api_key]"
authenticating_user_token 和我从 google auth json 得到的一样,比如 user.auth_hash["credentials"]["token"]
my_api_key 与 Google Developers Console 中的密钥相同。
当我通过 byebug 请求帐户的值时,它给了我:
#<Yt::Models::Account:0x000000051473f0 @access_token="ya29.[authenticating_user_token]", @refresh_token=nil, @device_code=nil, @expires_at=nil, @authorization_code=nil, @redirect_uri=nil, @force=nil, @scopes=nil, @authentication=nil>
第一个没有答案的问题:“如果文档说我只需要 access_token 为什么它使用 api 键?
第二部分refresh_token
实际上,在访问令牌惨败之后,我认为可能是
"credentials"=>{"token"=>"ya29.[authenticating_user_token]",
"expires"=>true,
"expires_at"=>1518443279}
是神秘的刷新令牌!
它给了我同样的 'Invalid Credentials'
所以我修改了videos_controller.rb:
account = Yt::Account.new(refresh_token: user.auth_hash["credentials"]["token"],
expires_at: user.auth_hash["credentials"]["expires_at"],
expires: user.auth_hash["credentials"]["expires"])
这次用 byebug 我得到了帐户的值:
#<Yt::Models::Account:0x00000007a6e5a8 @access_token=nil, @refresh_token="ya29.[you know_what]", @device_code=nil, @expires_at=1518443279, @authorization_code=nil, @redirect_uri=nil, @force=nil, @scopes=nil, @authentication=nil, @user_infos=#<Yt::Collections::UserInfos:0x00000007a305a0 @parent=#<Yt::Models::Account:0x00000007a6e5a8 ...>, @auth=#<Yt::Models::Account:0x00000007a6e5a8 ...>, @page_token=nil, @last_index=0, @items=[]>, @refreshed_authentications=#<Yt::Collections::Authentications:0x007ff0080054b0 @parent=#<Yt::Models::Account:0x00000007a6e5a8 ...>, @auth=#<Yt::Models::Account:0x00000007a6e5a8 ...>, @auth_params={:client_id=>"[my_client_id].apps.googleusercontent.com", :client_secret=>"[my_client_secret]", :refresh_token=>"[you_know_what]", :grant_type=>:refresh_token}, @page_token=nil, @last_index=1, @items=[], @where_params={}>>
并请求 account.email:
*** Yt::Errors::Unauthorized Exception: A request to YouTube API was sent without a valid authentication:
{}
You can retry the same request manually by running:
nil
第二个问题:“是否刷新令牌?”
在 YouTube 帐户中我有一个工作频道,所以这不是原因。
如有任何帮助,我将不胜感激!如果您有任何与 YouTube 兼容的应用程序,请将 link 分享给 Github,这样我就可以找出我的应用程序出了什么问题!
btn-group
use this class for the button div like <div class="btn-group"></div>
and apply
btn-group button:hover {
background-color: #3e8e41;
}
.btn-group:after {
content: "";
clear: both;
display: table;
}
.btn-group button:not(:last-child) {
border-right: none;
}
嗯,这很尴尬 - 甚至没有人试图提供帮助...
还是找到了错误的根源。
所以来自 auth["credentials"]["token"] 的令牌是访问令牌。
我的问题来了,因为我没有在 config/initializers/device.rb 中指定 :scope
应该是:
config.omniauth :google_oauth2, "[client_id_from_google_developers_console].apps.googleusercontent.com", "[key_from_google_developers_console]", scope: 'userinfo.profile, userinfo.email, youtube'
我还注意到当令牌过期时会出现错误,因此您应该执行诸如 begin-rescue 之类的操作来不时更新用户的令牌。
我正在使用 Rails 5.1.1 和 yt gem 0.28
我用的是官方gem文档(link在上面)。
还有这些文档 Github
并尝试通过 SitePoint
上的本教程检查自己如果您查看 SP 教程下方的评论 - 存在相同的问题,但您的所有者删除了问题,因此我无法使用它。
Gem 似乎工作正常。至少当我放置:
video = Yt::Video.new id: 'BPNYv0vd78A'
在video_controllers.rb中,然后询问video.description,它回答了描述。所以让我们在这里打勾。
然后我把 YT 配置成据说:
Yt.configure do |config|
config.log_level = :debug
config.client_id = "[my_client_id].apps.googleusercontent.com"
config.client_secret = "[my_secret_key]"
config.api_key = '[my_api_key]'
end
我从google_oauth2复制了值,所以如果没问题,这里也一定没问题。
但是当我尝试使用任何涉及身份验证的东西时,会发生一些非常奇怪的事情。
第一部分 - 访问令牌
根据手册,如果我有访问令牌,我可以:
account = Yt::Account.new access_token: 'ya29.[authenticating_user_token]'
account.email
消息如下:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "authError",
"message": "Invalid Credentials",
"locationType": "header",
"location": "Authorization"
}
],
"code": 401,
"message": "Invalid Credentials"
}
}
它还提供了 curl 命令来模拟这个动作:
curl -X GET -H "content-length: 0" -H "user-agent: Yt::Request (gzip)" -H "authorization: Bearer ya29.[authenticating_user_token]" -H "host: www.googleapis.com" "https://www.googleapis.com/oauth2/v2/userinfo?key=[my_api_key]"
authenticating_user_token 和我从 google auth json 得到的一样,比如 user.auth_hash["credentials"]["token"]
my_api_key 与 Google Developers Console 中的密钥相同。
当我通过 byebug 请求帐户的值时,它给了我:
#<Yt::Models::Account:0x000000051473f0 @access_token="ya29.[authenticating_user_token]", @refresh_token=nil, @device_code=nil, @expires_at=nil, @authorization_code=nil, @redirect_uri=nil, @force=nil, @scopes=nil, @authentication=nil>
第一个没有答案的问题:“如果文档说我只需要 access_token 为什么它使用 api 键?
第二部分refresh_token
实际上,在访问令牌惨败之后,我认为可能是
"credentials"=>{"token"=>"ya29.[authenticating_user_token]",
"expires"=>true,
"expires_at"=>1518443279}
是神秘的刷新令牌!
它给了我同样的 'Invalid Credentials'
所以我修改了videos_controller.rb:
account = Yt::Account.new(refresh_token: user.auth_hash["credentials"]["token"],
expires_at: user.auth_hash["credentials"]["expires_at"],
expires: user.auth_hash["credentials"]["expires"])
这次用 byebug 我得到了帐户的值:
#<Yt::Models::Account:0x00000007a6e5a8 @access_token=nil, @refresh_token="ya29.[you know_what]", @device_code=nil, @expires_at=1518443279, @authorization_code=nil, @redirect_uri=nil, @force=nil, @scopes=nil, @authentication=nil, @user_infos=#<Yt::Collections::UserInfos:0x00000007a305a0 @parent=#<Yt::Models::Account:0x00000007a6e5a8 ...>, @auth=#<Yt::Models::Account:0x00000007a6e5a8 ...>, @page_token=nil, @last_index=0, @items=[]>, @refreshed_authentications=#<Yt::Collections::Authentications:0x007ff0080054b0 @parent=#<Yt::Models::Account:0x00000007a6e5a8 ...>, @auth=#<Yt::Models::Account:0x00000007a6e5a8 ...>, @auth_params={:client_id=>"[my_client_id].apps.googleusercontent.com", :client_secret=>"[my_client_secret]", :refresh_token=>"[you_know_what]", :grant_type=>:refresh_token}, @page_token=nil, @last_index=1, @items=[], @where_params={}>>
并请求 account.email:
*** Yt::Errors::Unauthorized Exception: A request to YouTube API was sent without a valid authentication:
{}
You can retry the same request manually by running:
nil
第二个问题:“是否刷新令牌?”
在 YouTube 帐户中我有一个工作频道,所以这不是原因。
如有任何帮助,我将不胜感激!如果您有任何与 YouTube 兼容的应用程序,请将 link 分享给 Github,这样我就可以找出我的应用程序出了什么问题!
btn-group
use this class for the button div like <div class="btn-group"></div>
and apply
btn-group button:hover {
background-color: #3e8e41;
}
.btn-group:after {
content: "";
clear: both;
display: table;
}
.btn-group button:not(:last-child) {
border-right: none;
}
嗯,这很尴尬 - 甚至没有人试图提供帮助...
还是找到了错误的根源。 所以来自 auth["credentials"]["token"] 的令牌是访问令牌。
我的问题来了,因为我没有在 config/initializers/device.rb 中指定 :scope 应该是:
config.omniauth :google_oauth2, "[client_id_from_google_developers_console].apps.googleusercontent.com", "[key_from_google_developers_console]", scope: 'userinfo.profile, userinfo.email, youtube'
我还注意到当令牌过期时会出现错误,因此您应该执行诸如 begin-rescue 之类的操作来不时更新用户的令牌。