如何使用带有Ruby Google API客户端的Gmail API发送消息?

时间:2022-06-20 15:24:47

i'm facing several problem with API,

我面临着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

发送方法询问'id'(消息ID或线程ID)..但为什么?我正在发送新消息,所以它不应该要求。根据Gmail Api documnetation它的可选。 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 .

即使在指定消息ID后,它也会返回此消息。

 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 .

发送消息不起作用。

2 个解决方案

#1


7  

I think

我认为

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

because ".send" is ruby method

因为“.send”是ruby方法

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.

谢谢。

#2


4  

I think you may have a look at this gem I just built that use Gmail API and not using IMAP and SMTP like other gems:

我想你可能会看看我刚刚构建的这个使用Gmail API而不像其他宝石那样使用IMAP和SMTP的gem:

gem install gmail-api-ruby
m = Gmail::Message.new(to: test@test.com, subject: "hello", html: "<b>this is html part<b>, text: "this is the text part")
m.deliver

gmail-api-ruby

Gmail的-API红宝石

It comes with a lot of helpful methods that you use in Gmail interface

它提供了许多在Gmail界面中使用的有用方法

#1


7  

I think

我认为

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

because ".send" is ruby method

因为“.send”是ruby方法

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.

谢谢。

#2


4  

I think you may have a look at this gem I just built that use Gmail API and not using IMAP and SMTP like other gems:

我想你可能会看看我刚刚构建的这个使用Gmail API而不像其他宝石那样使用IMAP和SMTP的gem:

gem install gmail-api-ruby
m = Gmail::Message.new(to: test@test.com, subject: "hello", html: "<b>this is html part<b>, text: "this is the text part")
m.deliver

gmail-api-ruby

Gmail的-API红宝石

It comes with a lot of helpful methods that you use in Gmail interface

它提供了许多在Gmail界面中使用的有用方法