0

我有一个 Rails 5 应用程序和一个表单,我希望用户在其中输入他们的电子邮件地址并添加为联系人。这个问题离我很近。我得到的响应错误是:{"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

我错过了什么?

4

1 回答 1

0

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

  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
于 2018-04-05T15:05:51.707 回答