代码之家  ›  专栏  ›  技术社区  ›  brianrhea

使用Ruby Gmail API向Gmail添加标签

  •  0
  • brianrhea  · 技术社区  · 7 年前

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

    API的代码 create_user_label 要求 label_object 我以为这是我创造的。

    任何帮助都将不胜感激。

    2 回复  |  直到 4 年前
        1
  •  3
  •   shahjapan    4 年前

    这个 inline docs 假设您需要使用类型为的对象 Google::Apis::GmailV1::Label 作为您的 label_object :

    @param[谷歌::API::Gmail v1::标签]Label\u对象

    查看是否更改 label 从哈希值到该值的变量:

    label = Google::Apis::GmailV1::Label.new({
      name: 'TestLabel',
      label_list_visibility: 'labelShow',
      message_list_visibility: 'show',
    })
    
        2
  •  0
  •   Manmeet Singh    4 年前
    label = Google::Apis::GmailV1::Label.new({
       name: 'Label Name',
       label_list_visibility: 'labelShow',
       message_list_visibility: 'show',
       color: { background_color: "#fb4c2f", text_color: "#000000" 
      }
     })
    

    五颜六色