Rails - 如何通过 API 将联系人添加到 SendGrid 营销活动
Rails - How to add contacts to SendGrid marketing campaigns via API
我需要使用 SendGrid 发出 HTTP get 和 post 请求以将联系人添加到我们的帐户,但是他们的电子邮件营销功能似乎没有 gem。
归结为提出一些请求,但我无法通过他们的身份验证步骤。
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
哪个returns...
RestClient::Unauthorized: 401 Unauthorized: {"errors":[{"field":null,"message":"authorization required"}]}
using their API is to add contacts的终极objective。
我怎么会错误地发出这个 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
要通过 API 将营销联系人添加到您的 SendGrid 帐户,
请参阅 https://sendgrid.api-docs.io/v3.0/contacts-api-recipients/add-recipients
上的文档
您可以在页面的 "code generation" 部分查看示例代码。
require 'uri'
require 'net/http'
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 <<YOUR_API_KEY>>'
request["content-type"] = 'application/json'
request.body = "[{\"email\":\"example@example.com\",\"first_name\":\"\",\"last_name\":\"User\",\"age\":25},{\"email\":\"example2@example.com\",\"first_name\":\"Example\",\"last_name\":\"User\",\"age\":25}]"
response = http.request(request)
puts response.read_body
我需要使用 SendGrid 发出 HTTP get 和 post 请求以将联系人添加到我们的帐户,但是他们的电子邮件营销功能似乎没有 gem。
归结为提出一些请求,但我无法通过他们的身份验证步骤。
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
哪个returns...
RestClient::Unauthorized: 401 Unauthorized: {"errors":[{"field":null,"message":"authorization required"}]}
using their API is to add contacts的终极objective。
我怎么会错误地发出这个 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
要通过 API 将营销联系人添加到您的 SendGrid 帐户, 请参阅 https://sendgrid.api-docs.io/v3.0/contacts-api-recipients/add-recipients
上的文档您可以在页面的 "code generation" 部分查看示例代码。
require 'uri'
require 'net/http'
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 <<YOUR_API_KEY>>'
request["content-type"] = 'application/json'
request.body = "[{\"email\":\"example@example.com\",\"first_name\":\"\",\"last_name\":\"User\",\"age\":25},{\"email\":\"example2@example.com\",\"first_name\":\"Example\",\"last_name\":\"User\",\"age\":25}]"
response = http.request(request)
puts response.read_body