0

I'm having troubles sending a draft in Gmail through their API and the documentation doesn't help very much, especially since I'm working with Ruby.

I can create a draft without any issue, but then when I try to send the newly created draft, I get an error saying:

ArgumentError (wrong number of arguments (0 for 1))

The involved code is as follows:

@gmail = client.discovered_api('gmail', 'v1')
@send_result = client.execute(
  :api_method => @gmail.users.drafts.send,
  :parameters => { 'userId' => 'me' }, 
  :body_object => { 'id' => '<message_id>' } 
)

Taking a look at the debugger, the error seems to appear because of this:

@gmail.users.drafts.send

What am I missing here? I haven't seen anywhere that I should be passing parameters into the api_method? Also where can I find where this is documented and what is the parameter supposed to be?

Thanks!

Eric D
  • 6,901
  • 1
  • 15
  • 26
Kevin
  • 301
  • 2
  • 13

2 Answers2

1

The question is pretty old at this point, but I just ran into the same problem and figured it's better to answer late than never.

@gmail.users.drafts.send is colliding with Ruby's Object#send. You can work around the collision by converting the Google::APIClient::Resource item to a hash and then reading the value by key:

:api_method => @gmail.users.drafts.to_h["gmail.users.drafts.send"]

Your example, including the workaround:

@gmail = client.discovered_api('gmail', 'v1')
@send_result = client.execute(
  :api_method => @gmail.users.drafts.to_h["gmail.users.drafts.send"],
  :parameters => { 'userId' => 'me' }, 
  :body_object => { 'id' => '<message_id>' } 
)

I hope that helps!

David Mills
  • 2,385
  • 1
  • 22
  • 25
0

I'm just going off of: https://developers.google.com/gmail/api/v1/reference/users/drafts/send

But I think you have it right. The userId should be a parameter (e.g. in the URL) and the draft ID should be in the (POST) body. Can you confirm you're actually providing a draft ID and not the message.id?

Are you able to get an HTTP trace of the actual request, that would help immensely (you should likely be able to set this on the client or underlying http library your client uses, etc).

Eric D
  • 6,901
  • 1
  • 15
  • 26
  • The strange thing is that the request doesn't actually happen, it's throwing an exception before it makes the request so I don't think a trace would help here, that's the frustrating part. If I put a debug break between the first and second line and just call @gmail.users.drafts.send I get the error there, so it looks like an exception is being thrown before it even tries to call the #execute – Kevin Aug 26 '14 at 18:46
  • At this point, I'd probably be looking at the ruby source code (for the google API client). Hopefully someone more familiar with the google-api-ruby-client tag can chime in here soon... – Eric D Aug 26 '14 at 19:27
  • is @gmail.users.drafts.send object something you can introspect to see if there's any definition/documentation on it's expectations? e.g. in python i'd be calling print(), dir() and help() on it about now. – Eric D Aug 26 '14 at 20:07
  • Are you sure it's "@gmail.users.drafts.send"? Seems from other examples it should just be "gmail.users.drafts.send" (no "@"). e.g. http://stackoverflow.com/questions/25493213/creating-draft-via-google-gmail-api – Eric D Aug 26 '14 at 21:10
  • In ruby the @ symbol just sets it as an instance variable, I've tried it both ways just in case and gotten the same result. My next step is to try to look into the source on that object to see what's going on, but that's trickier since it's loaded up dynamically. I'll see what I can find out. I can't believe something like this is so difficult... – Kevin Aug 26 '14 at 21:45
  • to clarify you are using :body_object => { 'id' => '' } instead of :body_object => { 'id' => '' } ? – Arthur Thompson Aug 26 '14 at 22:11