使用Ruby Gmail API

时间:2018-01-08 22:15:31

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

我尝试使用Ruby Gmail API在我的Gmail帐户中创建新标签。

我有一个有效的连接,可以访问我的消息,列表标签等。

但是,当我尝试使用create_user_label时,我收到以下错误。

Google::Apis::ClientError: invalidArgument: Invalid request
/Users/brianrhea/.rvm/gems/ruby-2.4.2/gems/google-api-client-0.19.1/lib/google/apis/core/http_command.rb:218:in 'check_status'
/Users/brianrhea/.rvm/gems/ruby-2.4.2/gems/google-api-client-0.19.1/lib/google/apis/core/api_command.rb:116:in 'check_status'
/Users/brianrhea/.rvm/gems/ruby-2.4.2/gems/google-api-client-0.19.1/lib/google/apis/core/http_command.rb:183:in 'process_response'
/Users/brianrhea/.rvm/gems/ruby-2.4.2/gems/google-api-client-0.19.1/lib/google/apis/core/http_command.rb:299:in 'execute_once'
/Users/brianrhea/.rvm/gems/ruby-2.4.2/gems/google-api-client-0.19.1/lib/google/apis/core/http_command.rb:104:in 'block (2 levels) in execute'
/Users/brianrhea/.rvm/gems/ruby-2.4.2/gems/retriable-3.1.1/lib/retriable.rb:61:in 'block in retriable'
/Users/brianrhea/.rvm/gems/ruby-2.4.2/gems/retriable-3.1.1/lib/retriable.rb:57:in 'times'
/Users/brianrhea/.rvm/gems/ruby-2.4.2/gems/retriable-3.1.1/lib/retriable.rb:57:in 'retriable'
/Users/brianrhea/.rvm/gems/ruby-2.4.2/gems/google-api-client-0.19.1/lib/google/apis/core/http_command.rb:101:in 'block in execute'
/Users/brianrhea/.rvm/gems/ruby-2.4.2/gems/retriable-3.1.1/lib/retriable.rb:61:in 'block in retriable'
/Users/brianrhea/.rvm/gems/ruby-2.4.2/gems/retriable-3.1.1/lib/retriable.rb:57:in 'times'
/Users/brianrhea/.rvm/gems/ruby-2.4.2/gems/retriable-3.1.1/lib/retriable.rb:57:in 'retriable'
/Users/brianrhea/.rvm/gems/ruby-2.4.2/gems/google-api-client-0.19.1/lib/google/apis/core/http_command.rb:93:in 'execute'
/Users/brianrhea/.rvm/gems/ruby-2.4.2/gems/google-api-client-0.19.1/lib/google/apis/core/base_service.rb:360:in 'execute_or_queue_command'
/Users/brianrhea/.rvm/gems/ruby-2.4.2/gems/google-api-client-0.19.1/generated/google/apis/gmail_v1/service.rb:543:in 'create_user_label'
/Users/brianrhea/Sites/myproject/lib/tasks/gmail_api.rake:47:in 'block in <top (required)>'
/Users/brianrhea/.rvm/gems/ruby-2.4.2/gems/rake-12.3.0/exe/rake:27:in '<top (required)>'
/Users/brianrhea/.rvm/gems/ruby-2.4.2/bin/ruby_executable_hooks:15:in 'eval'
/Users/brianrhea/.rvm/gems/ruby-2.4.2/bin/ruby_executable_hooks:15:in '<main>'

我在堆栈跟踪中的失败代码是:

client = Google::Apis::GmailV1::GmailService.new
client.authorization = current_user.Token.fresh_token
user_id = 'me'

label = {
  "label_list_visibility" => "labelShow",
  "message_list_visibility" => "show",
  "name" => "TestLabel"
}

client.create_user_label(user_id, label)

create_user_label中的API代码需要label_object我认为我正在创建。

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

inline docs表示您需要使用Google::Apis::GmailV1::Label类型的对象作为label_object

  

@param [Google :: Apis :: GmailV1 :: Label] label_object

查看是否将哈希值中的label变量更改为:

label = Google::Apis::GmailV1::Label.new({
  name: 'TestLabel',
  label_list_visibility: 'labelShow',
  message_list_visibility: 'show',
})

答案 1 :(得分:0)

label = Google::Apis::GmailV1::Label.new({
   name: 'Label Name',
   label_list_visibility: 'labelShow',
   message_list_visibility: 'show',
   color: { background_color: "#fb4c2f", text_color: "#000000" 
  }
 })

有颜色