Error constructing Gmail API request in Ruby - google-api

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!

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!

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).

Related

Creating a Gmail Draft with Recipients through Gmail API

I have been trying to figure out how to automatically add recipients to a Draft email that is created using the Gmail API through their Ruby library. I can create the draft without any issues but setting the recipients is causing me troubles and I haven't been able to find any good examples showing the best way to add email specific things.
Using the Google API playground and pulling in drafts that have already been created, it looks like the structure should be something similar to what is shown below, but whenever the draft is created, there are no recipients.
#result = client.execute(
:api_method => gmail.users.drafts.create,
:parameters => {
'userId' => "me"
},
:body_object => {
'message' => {
'raw' => Base64.urlsafe_encode64('Test Email Message'),
'payload' => {
'headers' =>
[
{
'name' => "To",
'value' => "John Smith <john_smith.fake#gmail.com>"
}
]
}
}
}
)
'raw' should contain the entire (RFC822) email, complete with body and headers. Do not use the 'payload.headers' structure, that parsed format is only used for returning during message.get() presently.
so for 'raw' you'd want to Base64.urlsafe_encode64() a string like:
"To: someguy#example.com\r\nFrom: myself#example.com\r\nSubject: my subject\r\n\r\nBody goes here"

How to add a Person using the Highrise Ruby gem?

I am trying to use https://github.com/tapajos/highrise/ to update user accounts when people sign up to an app. However I am not getting very far.
In console I am doing:
person = Highrise::Person.create(:name => "charlie")
Which saves fine, but if I do something like
person = Highrise::Person.create(:name => "charlie", :email => "charlie#222.com")
then I get:
Unprocessable Entity
I can not get my head around this, I essentially want to add a full record:
person = Highrise::Person.create(:name => "charlie", :params => {:contact_data => {:email_addresses => "charlie#222.com"}})
but still i get the same error and can not find any examples online
You were on the right track with that last attempt. Give this a try:
person = Highrise::Person.create(
:first_name => "Charlie", :last_name => "Bravo",
:contact_data => {
:email_addresses => [{
:email_address => {:address => "charlie#222.com"}
}]
}
)
This matches the structure of the create a person request, as defined in the Highrise API. https://github.com/37signals/highrise-api/blob/master/sections/people.md#create-person
Also you can refer to ruby api's test spec for more examples https://github.com/tapajos/highrise/blob/f44cb3212c6d49549330c46454fe440ac117fa1b/spec/highrise/person_spec.rb#L40

Google big query "required parameter missing" with ruby gem

I'm using Google's ruby api client to talk to big query and I have it all set up and working, except for queries where I'm getting this error:
{"error"=>
{"errors"=>
[{"reason"=>"required",
"domain"=>"global",
"message"=>"Required parameter is missing"}],
"code"=>400,
"message"=>"Required parameter is missing"}}
Here is what I'm calling:
bq = client.discovered_api("bigquery", "v2")
resp = client.execute(
bq.jobs.query,
{ "projectId" => "1234",
"query" => "SELECT count(*) FROM [api_logs.api_logs_week_28__Jul_2012] where timestamp >= 1341817200 and timestamp <= 1341903599"
}
)
The frustrating part is on the query api docs, these same exact parameters work just fine. Any ideas?
First- - I don't know ruby, but I do know bigquery, so I've taken a look at the ruby google drive example and tried to adapt it:
result = client.execute(
:api_method => bq.jobs.query,
:body_object => { "query" => "SELECT 17" },
:parameters => { "projectId => "1234" })
Essentially the projectId needs to be a parameter, and the query needs to be in the post data.

Not Equal (ne) and OR not returning correct results in Mongomapper scopes

I can't chain these two scopes together in Mongomapper using an OR:
scope :comment_is_nil, where(:comment => nil)
scope :post_not_blank, where(:post.ne => "")
It should return model objects where the comment is not nil, OR the post is not blank.
This doesn't work:
Model.where("$or" => [{:comment_is_nil, :post_not_blank])
Any ideas?
Chaining scopes is an and operation so M.comment_is_nil.post_not_blank won't work as you know. MongoDB's or syntax looks like this:
Model.where(
:$or => [
{ :comment => nil },
{ :post.ne => '' }
]
)
So you need to give it an array of individual conditions by manually expanding the scopes.

Why can't i get sendgrid to replace my variables in my template when using their smtpapi?

I believe that i have followed the documentation on SendGrid's site but so far when I receive an email that I've sent through their API it never substitutes the replacements i've specified in the x-smtpapi headers. I am using HTTParty to send the request like this:
HTTParty.post(Sendgrid::Postman.api_url, {
:query => params.merge({ "api_user" => #config[:api_user], "api_key" => #config[:api_key] }),
:headers => headers, :format => :json
})
the "params" look like this:
{"from"=>"noreply#foo.com", "text"=>"Happy Holidays -first_name- -last_name-,\nI hope this message finds you in good health and high spirits.", "to"=>["foo#gmail.com"], "subject"=>"foo"}
The Headers look like this:
{"X-SMTPAPI"=>"{\"sub\": {\"-first_name-\": [\"Foo\"], \"-email-\": [\"foo#gmail.com\"], \"-login-\": [\"heavysixer\"], \"-last_name-\": [\"Bar\"]}, \"to\": [\"foo#gmail.com\"]}"}
The mail is always successfully delivered but when it arrives in the inbox the values that were supposed to be substituted still appear like -first_name- & -last_name-
What am I missing? I've been messing around with this for a solid day now?
-----------------------------------------------------------
UPDATE:
Per the advice below I have tried to push the x-smtpapi params into the form post yet I am getting the same result. The query string for my post now looks like this:
params = {"api_user" => 'foo', "api_key" => 'bar', "from"=>"noreply#foo.com", "text"=>"Happy Holidays -first_name- -last_name-,\nI hope this message finds you in good health and high spirits.", "to"=>["foo#gmail.com"], "subject"=>"foo", "x-smtpapi"=>{"sub"=>{"-first_name-"=>["foo"], "-email-"=>["foo#gmail.com"], "-login-"=>["foo"], "-last_name-"=>["bar"]}}}
HTTParty.post(Sendgrid::Postman.api_url, :query => params, :format => :json)
Their documentation implies that x-smtpapi should be one of the posted parameters, not an http header.

Resources