Send gmail messages with google-api-ruby-client '0.9.pre3'

Work through sending gmail with the new google-api-ruby client in rails 4 app.

require 'google/apis/gmail_v1'

Gmail = Google::Apis::GmailV1

class MailService

  def initialize(params)
    @params = params
  end

  def call
    message = Gmail::Message.new
    service = Gmail::GmailService.new
    message.raw = (redacted)
    service.request_options.authorization = current_user.token.fresh_token
    result = service.send_user_message(current_user.email, message)
  end

end

And this is the result of calling the API:

Sending HTTP post https://www.googleapis.com/gmail/v1/users/me/messages/send?
200
#<Hurley::Response POST https://www.googleapis.com/gmail/v1/users/me/messages/send == 200 (63 bytes) 858ms>
Success - #<Google::Apis::GmailV1::Message:0x007fc9cf9b52dd
 @id="15096369c05cdb1d",
 @thread_id="15096369c05cdb1d">

An unhandled message is sent without problems from the API Explorer, but when it is executed from my application, I get an error message in my inbox. In the example above, the edited sample is a valid RFC 2822 string with source code 64, and fresh_token represents the oauth2 access token for the current user.

Look at the mail bounce

Bounce <nobody@gmail.com>
2:43 PM (19 minutes ago)
to me

An error occurred. Your message was not sent.

Anyone have any thoughts? It looks like maybe my (sending) email was picked up in a raw message, but not in the recipient ... Although I suppose the API can forward failures based on my oauth access token.

. !

EDIT: RFC 2822 base64.

+4
1

, , . send_user_message , (0.9.13). raw : " RFC 2822 base64url . messages.get drafts.get format = RAW. JSON raw." , .

google-api-client 0.8 0.9, base64 . 0,8:

response = @service.execute(
  api_method: api.users.messages.to_h['gmail.users.messages.send'],
  body_object: {
    raw: Base64.urlsafe_encode64(mail.to_s)
  },
  parameters: {
    userId: 'me',
  }
)

message = { raw: mail.to_s }
res = @service.send_user_message('me', message, {})

0.9.

https://github.com/google/google-api-ruby-client/issues/474.

+2

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


All Articles