Reputation: 111040
I'm trying to use the Ruby gem 'google_drive'. Before using that gem, I'm obtaining the user's token via the gem omniauth-google-oauth2.
When I try to use google_drive as follows:
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
I get the following error:
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)
How can I resolve this?
Omniauth code being used to store the google oauth 2 tokens:
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
Upvotes: 2
Views: 773
Reputation: 111040
Working solution to access google drive api without requiring a config.json. This solution uses the refresh token obtained from the google auth 2 omniauth strategy:
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
This took me .5 day. I hope it helps someone else out there! :)
Upvotes: 3