google / google-api-ruby-client-samples

Samples for the Google API Ruby Client
327 stars 164 forks source link

Gmail sample #17

Closed cthulhu666 closed 4 years ago

cthulhu666 commented 10 years ago

Sinatra app talking with the new Gmail API. Based on Calendar API sample.

barnett commented 10 years ago

Can we also show an example using the body_object, was stuck on that for a while when trying to use the create a new gmail draft. It doesn't 'fit' in the example but can include as a separate file.

require 'google/api_client'

# user = User.find(x)

client = Google::APIClient.new
client.authorization.client_id = ENV['GOOGLE_CLIENT_ID']
client.authorization.client_secret = ENV['GOOGLE_CLIENT_SECRET']
client.authorization.grant_type = 'refresh_token'
client.authorization.refresh_token = user.refresh_token
client.authorization.fetch_access_token!
gmail = client.discovered_api('gmail', 'v1')

result = client.execute(
  api_method: gmail.users.drafts.create,
  parameters:  { 'userId' => 'me' },
  body_object: { 'message' =>
    { 'raw' =>  Base64.urlsafe_encode64(user.message) }
  }
)
austinkelleher commented 8 years ago

+1 @sgomes @icco