如何使用Gmail API获取消息列表? [英] how do i get message list with Gmail Api?

查看:123
本文介绍了如何使用Gmail API获取消息列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想访问消息列表

对象:

Object:

2.0.0-p481 :008 > g.gmail_api.users.messages.list
=> # < Google::APIClient::Method:0x41c948c ID:gmail.users.messages.list >

我是这个API的新成员,无法获取如何使用Gmail API。

i'm new in this API and unable to get how do i use Gmail API.

谢谢

Thanks

推荐答案

# Google
gem "omniauth-google-oauth2"
gem "google-api-client"

在我的模型中

def query_google( email )
    self.refresh_token_from_google if self.expires_at.to_i < Time.now.to_i

    @google_api_client = Google::APIClient.new(
      application_name: 'Joggle',
      application_version: '1.0.0'
    )

    @google_api_client.authorization.access_token = self.access_key
    @gmail = @google_api_client.discovered_api('gmail', "v1")


    # https://developers.google.com/gmail/api/v1/reference/users/messages/list
    # Now that we instantiated gmail, we can take the category (messages) and the method
    # You can also add parameters if you wish to do so:
    # @calendar_events = google_api_client.execute(
    #     :api_method => @calendar.events.list,
    #     :parameters => {
    #         "calendarId" => current_user.email,
    #         'timeMin' => date.to_s,
    #         'timeMax' => max_date.to_s
    #         # 'items' => [{'id' => current_user.email}]
    #     },
    #     :headers => {'Content-Type' => 'application/json'}
    # )
    @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'}
    )


    count = @emails.data.messages.count
    Rails.logger.error count 
    {count: count, last_emails: get_three_emails} if count > 0



end

仅供参考: https://github.com/google/google-api-ruby-client / issues / 135

这篇关于如何使用Gmail API获取消息列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆