【问题标题】:Add a Label to Gmail using the Ruby Gmail API使用 Ruby Gmail API 向 Gmail 添加标签
【发布时间】:2018-06-17 22:26:35
【问题描述】:

我正在尝试使用 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。

任何帮助将不胜感激。

【问题讨论】:

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


    【解决方案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',
    })
    

    【讨论】:

    • 成功了。谢谢!
    【解决方案2】:
    label = Google::Apis::GmailV1::Label.new({
       name: 'Label Name',
       label_list_visibility: 'labelShow',
       message_list_visibility: 'show',
       color: { background_color: "#fb4c2f", text_color: "#000000" 
      }
     })
    

    有颜色

    【讨论】:

      猜你喜欢
      • 2018-07-30
      • 1970-01-01
      • 2017-12-21
      • 2017-09-13
      • 2021-09-16
      • 1970-01-01
      • 2016-04-24
      • 2014-08-22
      • 2019-04-24
      相关资源
      最近更新 更多