Creating a project through the Google Gmail API

I am trying to create a draft message for a registered user, but keep getting an error Missing draft messagewhen I run below

require 'google/api_client'
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.last.refresh_token
token = client.authorization.fetch_access_token!
gmail = client.discovered_api('gmail', 'v1')
params = { 'userId' => 'me', 'draft' => { 'message' => {'raw' => 'test email' } } }
# { 'userId' => 'me', 'message' => {'raw' => 'test email' } }
result = client.execute(api_method: gmail.users.drafts.create, parameters: params)

In addition, I tried a commented out combination for the parameters and still have no luck. Any ideas?

+1
source share
2 answers

I had the same problem when I tried to do this for the first time. The solution I found was to not include the message information as part of the parameters, but rather pass it to the body: object, as shown below.

@result = client.execute(
  :api_method => gmail.users.drafts.create,
  :parameters => {
    'userId' => "me"      
  },
  :body_object => {
    'message' => {
      'raw' =>  Base64.urlsafe_encode64('Test Email Message')
    }
  }
)
+4
source

raw message - SMTP-.

  • body_object
  • SMTP-, message

:

params = { 'userId' => 'me', 'draft' => { 'message' => {'raw' => 'From: foo@example.com\nSubject:Ignore\n\ntest email' } } }
+1

Source: https://habr.com/ru/post/1547779/


All Articles