Skip to content

Instantly share code, notes, and snippets.

@falonofthetower
Created March 6, 2016 00:27
Show Gist options
  • Save falonofthetower/bfa1cb1e7a596ff62661 to your computer and use it in GitHub Desktop.
Save falonofthetower/bfa1cb1e7a596ff62661 to your computer and use it in GitHub Desktop.
module GoogleDriveAccess
def new_client
Signet::OAuth2::Client.new(
client_id: ENV["CLIENT_ID"],
:temporary_credential_uri =>
'https://www.google.com/accounts/OAuthGetRequestToken',
:authorization_uri =>
'https://www.google.com/accounts/OAuthAuthorizeToken',
:token_credential_uri =>
'https://www.google.com/accounts/OAuthGetAccessToken',
:client_credential_key => 'anonymous',
:client_credential_secret => 'anonymous',
authorization_uri: "https://accounts.google.com/o/oauth2/auth",
token_credential_uri: "https://accounts.google.com/o/oauth2/token",
auth_provider_x509_cert_url: "https://www.googleapis.com/oauth2/v1/certs",
client_secret: ENV["GOOGLE_CLIENT_SECRET"],
redirect_uri: ENV["REDIRECT_URI"],
javascript_origins: ENV["JAVASCRIPT_ORIGINS"]
)
end
def refresh(token)
@client.refresh_token = token
@client.grant_type = 'refresh_token'
@client.fetch_access_token!
@drive.authorization = @client
end
def new_drive
Google::Apis::DriveV2::DriveService.new
end
def download_file(file_id, destination_path)
@drive.get_file(file_id, download_dest: destination_path)
end
def upload_file(title, options = {})
data = Google::Apis::DriveV2::File.new(title: title)
@drive.insert_file(data, upload_source: options[:source_path], content_type: options[:type])
end
def create_folder(title, parents = [])
data = Google::Apis::DriveV2::File.new(title: title)
data.mime_type = "application/vnd.google-apps.folder"
data.parents = parents
drive.insert_file(data)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment