如何使用Ruby观看新邮件的Gmail邮箱

时间:2016-03-23 22:49:45

标签: ruby gmail gmail-api google-api-client

所以,我正在尝试构建一个小应用程序,该应用程序将在Gmail邮箱上收听新邮件,并对收到的新邮件进行处理。

我有以下连接到Gmail API的代码观看用户邮箱:

require 'google/apis/gmail_v1'
require 'googleauth'
require 'googleauth/stores/file_token_store'
require 'fileutils'

OOB_URI = 'urn:ietf:wg:oauth:2.0:oob'
APPLICATION_NAME = 'Gmail API Ruby Quickstart'
CLIENT_SECRETS_PATH = 'client_secret.json'
CREDENTIALS_PATH = File.join(Dir.home, '.credentials',
                             "gmail-ruby-quickstart.yaml")
SCOPE = Google::Apis::GmailV1::AUTH_GMAIL_MODIFY

##
# Ensure valid credentials, either by restoring from the saved credentials
# files or initiating an OAuth2 authorization. If authorization is required,
# the user's default browser will be launched to approve the request.
#
# @return [Google::Auth::UserRefreshCredentials] OAuth2 credentials
def authorize
  FileUtils.mkdir_p(File.dirname(CREDENTIALS_PATH))

  client_id = Google::Auth::ClientId.from_file(CLIENT_SECRETS_PATH)
  token_store = Google::Auth::Stores::FileTokenStore.new(file: CREDENTIALS_PATH)
  authorizer = Google::Auth::UserAuthorizer.new(
      client_id, SCOPE, token_store)
  user_id = 'default'
  credentials = authorizer.get_credentials(user_id)
  if credentials.nil?
    url = authorizer.get_authorization_url(
        base_url: OOB_URI)
    puts "Open the following URL in the browser and enter the " +
             "resulting code after authorization"
    puts url
    code = gets
    credentials = authorizer.get_and_store_credentials_from_code(
        user_id: user_id, code: code, base_url: OOB_URI)
  end
  credentials
end

# Initialize the API
service = Google::Apis::GmailV1::GmailService.new
service.client_options.application_name = APPLICATION_NAME
service.authorization = authorize

watch_request = Google::Apis::GmailV1::WatchRequest.new
watch_request.topic_name= 'projects/my-project/topics/push'
service.watch_user('me', watch_request)

我能解决的是我如何获得新消息的通知。有人可以帮忙吗?

我读过这篇文章的目的很远:

更新:2016年5月25日

因此,检查google-api-client gem代码,我发现我需要为watch_user方法提供一个块。我试过了,但结果对象似乎没有提供处理新消息事件的任何方法:

#...
service.watch_user('me', watch_request) do |request|
  puts request.inspect
  puts request.methods
end

inspect收益:

#<Google::Apis::GmailV1::WatchResponse:0x007fecd515f9e0 @expiration="1459518159351", @history_id="1038029">

methods收益:

expiration
expiration=
history_id
history_id=
update!
to_json
to_h
psych_to_yaml
to_yaml
to_yaml_properties
pretty_print
pretty_print_cycle
pretty_print_instance_variables
pretty_print_inspect
nil?
===
=~
!~
eql?
hash
<=>
class
singleton_class
clone
dup
itself
taint
tainted?
untaint
untrust
untrusted?
trust
freeze
frozen?
to_s
inspect
methods
singleton_methods
protected_methods
private_methods
public_methods
instance_variables
instance_variable_get
instance_variable_set
instance_variable_defined?
remove_instance_variable
instance_of?
kind_of?
is_a?
tap
send
public_send
respond_to?
extend
display
method
public_method
singleton_method
define_singleton_method
object_id
to_enum
enum_for
gem
pretty_inspect
==
equal?
!
!=
instance_eval
instance_exec
__send__
__id__

这是GitHub上宝石代码的链接:https://github.com/google/google-api-ruby-client

0 个答案:

没有答案