Skip to content

Instantly share code, notes, and snippets.

@ShogunPanda
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ShogunPanda/9007021 to your computer and use it in GitHub Desktop.
Save ShogunPanda/9007021 to your computer and use it in GitHub Desktop.
Google Drive
client = Google::APIClient.new(authorization: :oauth_2, application_name: NAME, application_version: VERSION)
client.client_id = CLIENT_ID
client.client_secret = CLIENT_SECRECT
client.authorization.scope = SCOPES
authorizer = Clavem::Authorizer.new do |auth, req, res|
raise RuntimeError.new if request.query["error"].present? || request.query["code"].blank?
client.authorization.code = request.query["code"].to_s
client.authorization.fetch_access_token!
client.authorization.refresh_token
end
client.authorization.redirect_uri = authorizer.callback_url
authorizer.authorize(client.authorization.authorization_uri)
if authorizer.succeeded? then
refresh_token = authorizer.token
end
# Now you can store the refresh token somewehere
client = Google::APIClient.new(authorization: :oauth_2, application_name: NAME, application_version: VERSION)
client.client_id = CLIENT_ID
client.client_secret = CLIENT_SECRECT
client.authorization.scope = SCOPES
client.authorization.code = REFRESH_TOKEN
client.authorization.fetch_access_token!
api = client.discovered_api('drive', 'v2')
data = client.execute(api_method: api.files.get, parameters: {"fileId" => ID})
raise ArgumentError if result.status == 404
url = result.data.alternate_link
# Now you can download from URL using Faraday, open-uri or whatever.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment