ruby-on-rails - 如何使用 Gmail Api 获取邮件列表?

标签 ruby-on-rails google-api google-docs-api gmail-api

我想访问消息列表

对象:

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

我是此 API 的新手,无法了解如何使用 Gmail API。

谢谢

最佳答案

# 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

关于ruby-on-rails - 如何使用 Gmail Api 获取邮件列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25310300/

相关文章:

google-drive-api - 在 Javascript 客户端中从 Google Drive 下载文件

ruby-on-rails - ruby on rails 3 使用缓存数据重新加载

ruby-on-rails - 设置 Redis 以使用 Heroku 和 Rails 4.0.0

google-apps-script - 如何获取 Google 文档的最后编辑日期

android - 无法使用 Pubnub 教程解析方法 requestLocationUpdates

java - 来自授权码交换的 invalid_grant 响应

python - 使用 python-gdata 批量插入电子表格

ruby-on-rails - “scaffold” 在 Ruby on Rails 中是什么意思?

ruby-on-rails - cucumber rails -

java - 使用Java中的批处理请求和nextPageToken从Google Drive Api获取超过1000个文件