【问题标题】:Add a single contact via sendgrid api in Ruby on Rails在 Ruby on Rails 中通过 sendgrid api 添加单个联系人
【发布时间】:2018-04-05 11:52:03
【问题描述】:

我有一个 Rails 5 应用程序和一个表单,我希望用户在其中输入他们的电子邮件地址并添加为联系人。 THIS QUESTION 离我很近。我得到的响应错误是:{"errors":[{"field":null,"message":"access forbidden"}]}这看起来像是一个身份验证问题。这是我的代码...

def email_signup
    email_address = params[:email_address]

    url = URI("https://api.sendgrid.com/v3/contactdb/recipients")

    http = Net::HTTP.new(url.host, url.port)
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE

    request = Net::HTTP::Post.new(url)
    request["authorization"] = 'Bearer <<my_api_key>>'
    request["content-type"] = 'application/json'
    request.body = "[{\"email\" : email_address}]"

    response = http.request(request)

    redirect_to jobs_url, notice: response.read_body
  end

我错过了什么?

【问题讨论】:

    标签: ruby-on-rails sendgrid-api-v3


    【解决方案1】:

    好的...我有两个问题需要解决。首先,我的 API 密钥的字符串部分中有 &lt;&lt;,另一个是请求正文的格式不正确。这对我有用:

      def email_signup
        email_address = params[:email_address]
    
        url = URI("https://api.sendgrid.com/v3/contactdb/recipients")
    
        http = Net::HTTP.new(url.host, url.port)
        http.use_ssl = true
        http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    
        request = Net::HTTP::Post.new(url)
        request["authorization"] = 'Bearer SG.Q9sdfsd9s098sdf89sf809sdf809sd'
        request["content-type"] = 'application/json'
        request.body = [{"email": email_address}].to_json
    
        response = http.request(request)
    
        redirect_to jobs_url, notice: response.read_body
      end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-03
      • 2016-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-09
      • 2018-11-19
      • 2021-04-29
      相关资源
      最近更新 更多