【发布时间】:2020-05-20 01:03:08
【问题描述】:
我正在尝试使用 Ruby gem 'google_drive'。在使用该 gem 之前,我通过 gem omniauth-google-oauth2 获取用户的令牌。
当我尝试按如下方式使用 google_drive 时:
def google_oauth2(current_user)
session = GoogleDrive.login_with_oauth(self.token)
# Gets list of remote files.
session.files.each do |file|
p file.title
end
end
我收到以下错误:
Sending HTTP get https://www.googleapis.com/drive/v3/files?fields=%2A
Caught error Authorization failed. Server message:
{
"error": "invalid_request",
"error_description": "Required parameter is missing: grant_type"
}
Error - #<Signet::AuthorizationError: Authorization failed. Server message:
{
"error": "invalid_request",
"error_description": "Required parameter is missing: grant_type"
}>
Completed 500 Internal Server Error in 128ms (ActiveRecord: 0.4ms)
我该如何解决这个问题?
更新
用于存储 google oauth 2 令牌的 Omniauth 代码:
def google_oauth2
auth_hash = request.env['omniauth.auth']
@authentication = Authentication.find_or_create_by(
user_id: current_user.id,
provider: auth_hash["provider"],
uid: auth_hash["uid"]
)
@authentication.update_attributes(
:token => auth_hash['credentials']['token'],
:refresh_token => auth_hash['credentials']['refresh_token'],
:provider_description => auth_hash["info"].email
)
flash[:notice] = "google_oauth2 authed."
redirect_to '/'
end
【问题讨论】:
-
你访问过这个页面吗? developers.google.com/identity/protocols/…
-
您必须创建此 client_secrets.json。我相信在谷歌文档的某个地方有一个模型。让我在这里做一些研究。
-
您是否在 Google 那里为您的应用创建了授权?
-
奇怪。我一直很顺利地使用它。你能发布你用来获取这个令牌的代码吗?
标签: ruby-on-rails ruby google-drive-api omniauth