Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
982 views
in Technique[技术] by (71.8m points)

how to send message using Gmail API with Ruby Google API Client?

i'm facing several problem with API,

first:

send method asking for 'id'(message id or thread id) .. but why ? i'm sending new message so it shouldn't require . according to Gmail Api documnetation its optional . https://developers.google.com/gmail/api/v1/reference/users/messages/send

ArgumentError: Missing required parameters: id.

second:

even after specify message id it return this message .

 Your client has issued a malformed or illegal request.

code

 require 'mime'
 include MIME



msg = Mail.new
msg.date = Time.now
msg.subject = 'This is important'
msg.headers.set('Priority', 'urgent')

msg.body = Text.new('hello, world!', 'plain', 'charset' => 'us-ascii')
msg.from = {'hi@gmail.com' => 'Boss Man'}
msg.to   = {
    'list@example.com' => nil,
    'john@example.com' => 'John Doe',
    'jane@example.com' => 'Jane Doe',
}

@email = @google_api_client.execute(
    api_method: @gmail.users.messages.send(:get),
    body_object: {
        raw: Base64.urlsafe_encode64(msg.to_s)
    },
    parameters: {
        userId: 'me'
    }
)

and of-course authentication working fine.

some other methods also working fine

like:

get list of messages(Users.messages.list)

get single message(Users.messages.get)

but

send message not working .

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think

@gmail.users.messages.send(:get) is equal to @gmail.users.messages.get

because ".send" is ruby method

so now this method is working with

@gmail.users.messages.to_h['gmail.users.messages.send']

example:

msg = Mail.new
msg.date = Time.now
msg.subject = options[:subject]
msg.body = Text.new(options[:message])
msg.from = {@_user.email => @_user.full_name}
msg.to   = {
    options[:to] => options[:to_name]
}
@email = @google_api_client.execute(
    api_method: @gmail.users.messages.to_h['gmail.users.messages.send'],
    body_object: {
        raw: Base64.urlsafe_encode64(msg.to_s)
    },
    parameters: {
        userId: 'me',
    }
) 

Thanks.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...