我需要使用 SendGrid 发出 HTTP get 和 post 请求以将联系人添加到我们的帐户,但是他们的电子邮件营销功能似乎没有什么好东西。
归结为提出一些请求,但我无法通过他们的身份验证步骤。
curl -X "GET" "https://api.sendgrid.com/v3/templates" -H "Authorization: Bearer Your.API.Key-HERE" -H "Content-Type: application/json"
并且使用 Rest-Client gem 我正在尝试像这样发出身份验证请求...
username = 'username'
api_key = 'SG.MY_API_KEY'
key = Base64.encode64(username + ":" + api_key)
headers = {"Authorization" => "Bearer #{key}", "Content-Type" => "application/json"}
response = RestClient.get 'https://api.sendgrid.com/v3/templates', headers
返回...
RestClient::Unauthorized: 401 Unauthorized: {"errors":[{"field":null,"message":"authorization required"}]}
using their API is to add contacts的终极目标.
我是如何错误地发出这个 get 请求的?
最佳答案
我终于明白了。为了将来引用,这里是有效的代码...
require 'rest_client'
api_key = 'YOUR_API_KEY'
headers = {'Authorization' => "Bearer #{api_key}"}
data = {:email => 'email@website.com'}
response = RestClient.post 'https://api.sendgrid.com/v3/contactdb/recipients', [data].to_json, headers
关于ruby-on-rails - Rails - 如何通过 API 将联系人添加到 SendGrid 营销事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34513857/