Reputation: 11
junior developer here with a gmail api question. My rails app needs to search for and retrieve around 300 gmail messages whose bodies it will then scrape using nokogiri. I've installed the google-api-client gem, and successfully configured authorization following this person's guide. Using their example I can successfully retrieve a user's list of labels with the following code:
client = Signet::OAuth2::Client.new(access_token: session[:access_token])
client.expires_in = Time.now + 1_000_000
service = Google::Apis::GmailV1::GmailService.new
service.authorization = client
@labels_list = service.list_user_labels('me')
I tried searching google's ruby api docs for how to do the search I want, but their docs are pretty sparse. It seems that I'll need to run at least two separate queries - the first to retrieve a list of messages that meet the sender and date parameters of my search, and then an additional query to retrieve the message body. The syntax for this is eluding me though. I've played around with the gmail ruby api instance methods for #list_user_messages
and #get_user_message
and have had no luck getting them to work. Anyone here with experience using them?
Upvotes: 1
Views: 869
Reputation: 6791
You may want to check the documentation on Google API Client for Ruby. This will explain basic functions and implementations with Google APIs using Ruby.
for querying sender try this one :
@emails = @google_api_client.execute(
api_method: @gmail.users.messages.list,
parameters: {
userId: "me",
# searching messages based on number of queries:
# https://developers.google.com/gmail/api/guides/filtering
q: "from:" + email.to_s
},
headers: {'Content-Type' => 'application/json'}
)
For the the date, you can only filter using the operation : after:
, before:
, older:
,newer:
. You might want to check the list of supported operators you can use with Gmail.
Hope this helps.
Upvotes: 1