ruby - 在 ruby​​ 中使用 google 的 api 客户端获取 Gmail 消息的正确方法是什么?

标签 ruby google-api

我已经在我的谷歌开发者控制台中设置了一个服务帐户,并且我已经打开了 Gmail API。使用 ruby​​ 的各种示例,我想出了以下脚本来尝试获取我的电子邮件:

#!/usr/bin/ruby

require 'google/api_client'

SERVACC = "abcdefg@developer.gserviceaccount.com" # My Service Account's CLIENT ID
GAPMAIL = "myapp@mydomain.com" #gApps account
PKCFILE = "myapp.p12" # Downloaded S.A. cred file
GMSCOPE = "https://www.googleapis.com/auth/gmail.readonly"
TOKEURI = "https://accounts.google.com/o/oauth2/token"

options = {:application_name => 'gmail', :application_version => 'v1'}
Gclient = Google::APIClient.new( options )
Gclient.authorization = Signet::OAuth2::Client.new(
  :token_credential_uri => TOKEURI,
  :audience => TOKEURI,
  :scope => GMSCOPE,
  :issuer => SERVACC,
  :signing_key => Google::APIClient::PKCS12.load_key(PKCFILE, 'notasecret')
)
Gclient.authorization.fetch_access_token!

result = Gclient.execute!(
  :api_method => Gclient.discovered_api('gmail').users.messages.list,
  :parameters => {:userId => GAPMAIL}
)

不幸的是,它会出现后端错误:

~/.rvm/gems/ruby-2.1.4/gems/google-api-client-0.8.2/lib/google/api_client.rb:654:in `block (2 levels) in execute!': Backend Error (Google::APIClient::ServerError)
        from /Users/bbialek/.rvm/gems/ruby-2.1.4/gems/retriable-1.4.1/lib/retriable/retry.rb:27:in `perform'
        from /Users/bbialek/.rvm/gems/ruby-2.1.4/gems/retriable-1.4.1/lib/retriable.rb:15:in `retriable'
        from /Users/bbialek/.rvm/gems/ruby-2.1.4/gems/google-api-client-0.8.2/lib/google/api_client.rb:635:in `block in execute!'
        from /Users/bbialek/.rvm/gems/ruby-2.1.4/gems/retriable-1.4.1/lib/retriable/retry.rb:27:in `perform'
        from /Users/bbialek/.rvm/gems/ruby-2.1.4/gems/retriable-1.4.1/lib/retriable.rb:15:in `retriable'
        from /Users/bbialek/.rvm/gems/ruby-2.1.4/gems/google-api-client-0.8.2/lib/google/api_client.rb:626:in `execute!'
        from stack.rb:22:in `<main>'

我实际上看到服务器错误 (5xx) 计数随着 API 的使用而上升,所以我不能太过分了。我错过了什么?

最佳答案

以防万一有人在挣扎时发现这个....

这是一个有效的实现。希望能帮助到你。 您需要在您的谷歌开发者控制台等中注册该应用程序,重要的是,在您的谷歌应用程序域中为服务帐户授予适当的范围(以下范围是完全访问权限)。

google_api_key = #Your API Key - here it's stored as a string, but you might want to use the file version
google_api_email = #The email id attached to your service account
account = #the email account you want to use
application_name = # a name for your app
application_version = #version for your app
  @google_mail_session = Google::APIClient.new(application_name: application_name, application_version: application_version)

  key = Google::APIClient::KeyUtils.load_from_pem(
      google_api_key,
      'notasecret')
  asserter = Google::APIClient::JWTAsserter.new(
      google_api_email,
      ['https://mail.google.com/'],
      key
  )
  @google_mail_session.authorization = asserter.authorize(account)
  # Leave the account details out for a generic authorization, you'll need to specify the account when you make a call to the API.
  @gmail_api = @google_mail_session.discovered_api('gmail', 'v1')

一个示例调用是

 response = @google_mail_session.execute(
   :api_method => @gmail_api.users.messages.list, 
   :parameters => {:userId => 'me'}) # you can use the standard address here if you don't want to authorize to a specific account above

我花了很长时间才开始工作,我基本上感觉自己是在黑暗中拍摄。为什么谷歌让它变得如此困难我无法理解......

关于ruby - 在 ruby​​ 中使用 google 的 api 客户端获取 Gmail 消息的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28774422/

相关文章:

ruby - 'eval' 是与 Ruby 中的绑定(bind)对象交互的唯一方式吗?

ruby-on-rails - 如何在 Linux 中设置 Ruby 环境?

ruby - 如何在 Watir 中设置 cookie

javascript - Google API获取刷新 token : Uncaught ReferenceError: offline is not defined

google-api - Google Classroom API 修改附件

google-api - 谷歌驱动器直接链接

ruby-on-rails - 有人可以向我解释这段代码在做什么吗?

ruby - 在 Ruby 中,有没有办法轻松地只删除数组中的 1 个匹配项?

google-api - G Suite API 中 Etag 字段的用途是什么?

android - 安全性:应用程序源代码中的谷歌客户端 secret 安全吗?