在ruby中使用Google的api客户端获取Gmail邮件的正确方法是什么?

时间:2015-02-27 21:16:03

标签: 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 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的使用下上升,所以我不能太远。我错过了什么?

1 个答案:

答案 0 :(得分:0)

万一有人在挣扎的时候找到了这个......

这是一个有效的实施方案。希望能帮助到你。 您需要在Google开发者控制台中注册该应用..并且,重要的是,将相应的范围授予您的google apps域中的服务帐户(以下范围是完全访问权限)。

var gulp = require('gulp');
var imagemin = require('gulp-imagemin');
var imageminJpegRecompress = require('imagemin-jpeg-recompress');

gulp.task('optimize', function () {
  return gulp.src('src/images/*')
    .pipe(imagemin({
      use:[imageminJpegRecompress({
        loops:4,
        min: 50,
        max: 95,
        quality:'high' 
      })]
    }))
});

示例调用将是

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')

我花了很长时间才开始工作,我觉得自己在黑暗中拍摄。为什么谷歌让它变得如此难以超越我......