【问题标题】:how do i get message list with Gmail Api?如何使用 Gmail Api 获取邮件列表?
【发布时间】:2014-10-08 05:42:18
【问题描述】:

我想访问消息列表

对象:

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

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

谢谢

【问题讨论】:

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


    【解决方案1】:
    # 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

    【讨论】:

      【解决方案2】:

      不是 ruby​​ 专家,但它看起来像一个方法,你可以调用 () 吗?我想您已经完成了设置身份验证等步骤? messages.list() 函数应该能够在没有任何参数的情况下被调用(好吧,您可能需要指定一个 userId,您可以使用“me”来获取经过身份验证的用户)。

      【讨论】:

      • 我已经通过 OmniAuth 完成了身份验证。我有令牌和刷新令牌。我不知道如何使用它。任何 JavaScript 启动示例都可以提供帮助。
      猜你喜欢
      • 2016-12-12
      • 1970-01-01
      • 2016-12-12
      • 2015-10-19
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      • 2020-09-04
      • 2016-07-30
      相关资源
      最近更新 更多