使用谷歌的正确方式是什么;在ruby中使用api客户端来获取Gmail消息?

使用谷歌的正确方式是什么;在ruby中使用api客户端来获取Gmail消息?,ruby,google-api,Ruby,Google Api,我已经在我的google开发者控制台中设置了一个服务帐户,并且打开了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 acco

我已经在我的google开发者控制台中设置了一个服务帐户,并且打开了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>'
~/.rvm/gems/ruby-2.1.4/gems/google-api-client-0.8.2/lib/google/api_-client.rb:654:in`block(2层)in execute!':后端错误(Google::APIClient::ServerError)
from/Users/bbialek/.rvm/gems/ruby-2.1.4/gems/retreable-1.4.1/lib/retreable/retry.rb:27:in'perform'
from/Users/bbialek/.rvm/gems/ruby-2.1.4/gems/retreable-1.4.1/lib/retreable.rb:15:in“retreable”
from/Users/bbialek/.rvm/gems/ruby-2.1.4/gems/google-api-client-0.8.2/lib/google/api_-client.rb:635:“执行中的块!”
from/Users/bbialek/.rvm/gems/ruby-2.1.4/gems/retreable-1.4.1/lib/retreable/retry.rb:27:in'perform'
from/Users/bbialek/.rvm/gems/ruby-2.1.4/gems/retreable-1.4.1/lib/retreable.rb:15:in“retreable”
from/Users/bbialek/.rvm/gems/ruby-2.1.4/gems/google-api-client-0.8.2/lib/google/api_-client.rb:626:in'execute!'
从stack.rb:22:in`'

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

以防万一有人在挣扎中发现了这个

这里是一个工作实现。希望能有帮助。 您需要在您的google开发者控制台中注册应用程序..等等,重要的是,向您的google应用程序域中的服务帐户授予适当的作用域(下面的作用域是完全访问)

一个例子是

 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
这让我花了很长很长的时间才开始工作,我感觉自己就像是在黑暗中拍摄。我不明白谷歌为什么让它变得如此困难

 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