Search code examples
rubyapigmailimapgoogle-oauth

Accessing Thread ID via Gmail IMAP Extensions (xoauth2)


I'm trying to grab the message id or thread id from Gmail in my Rails app. I'm using the gmail_xoauth gem (https://github.com/nfo/gmail_xoauth) and the standard net/imap in ruby. Here's a snippet of what I have:

require 'gmail_xoauth'
imap = Net::IMAP.new('imap.gmail.com', 993, usessl = true, certs = nil, verify = false)
imap.authenticate('XOAUTH2', user.email, user.token)
imap.examine('[Gmail]/All Mail')

imap.search(["SINCE", "15-May-2014"]).each do |message_id|
    puts imap.fetch(message_id, "(X-GM-THRID)")
end

Ruby’s Net::IMAP throws and exception (“unknown attribute”), so via a couple of Google searches, I landed here: http://blog.wojt.eu/post/13496746332/retrieving-gmail-thread-ids-with-ruby

I added the little patch that he mentioned, but now with the imap.fetch(message_id, "(X-GM-THRID)"), I get another error:

ArgumentError: wrong number of arguments (0 for 1)

I feel that I have gotten closer, but can't wrap my head around what is wrong. Any help is appreciated.

Thanks


Solution

  • I did manage to solve it via IMAP using this monkey patch - https://gist.github.com/kellyredding/2712611

    Although it would be worth exploring the Gmail API, as its far easier now - https://developers.google.com/gmail/