我正在尝试使用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发布于 2016-06-30 23:20:39
无需config.json即可访问google drive api的工作解决方案。此解决方案使用从google auth 2 omniauth策略获得的刷新令牌:
require 'google/apis/drive_v2'
auth = Signet::OAuth2::Client.new(
token_credential_uri: 'https://accounts.google.com/o/oauth2/token',
client_id: "XXX-XXX",
client_secret: "XXX",
refresh_token: self.refresh_token # Get this from the omniauth strategy.
)
auth.fetch_access_token!
x = Google::Apis::DriveV2
drive = x::DriveService.new
drive.authorization = auth
files = drive.list_files这花了我.5一天的时间。我希望它能帮助其他人!:)
https://stackoverflow.com/questions/38111436
复制相似问题