首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rails -如何通过API向SendGrid营销活动添加联系人

Rails -如何通过API向SendGrid营销活动添加联系人
EN

Stack Overflow用户
提问于 2015-12-29 15:14:30
回答 2查看 2K关注 0票数 1

我需要使用SendGrid制作HTTP和post请求,以便将联系人添加到我们的帐户中,但是对于他们的电子邮件营销功能,似乎没有创业板。

这归根结底是提出了一些请求,但我无法通过他们的认证步骤。

他们说要这么做

代码语言:javascript
复制
curl -X "GET" "https://api.sendgrid.com/v3/templates" -H "Authorization: Bearer Your.API.Key-HERE" -H "Content-Type: application/json"

使用Rest-Client gem,我试图提出这样的认证请求.

代码语言:javascript
复制
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

还会回来..。

代码语言:javascript
复制
RestClient::Unauthorized: 401 Unauthorized: {"errors":[{"field":null,"message":"authorization required"}]}

使用他们的API来添加联系人的最终目标。

我怎么会不正确地提出这个请求?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-12-29 20:50:48

最后我弄明白了。为了将来的参考,这是有效的代码..。

代码语言:javascript
复制
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
票数 5
EN

Stack Overflow用户

发布于 2017-08-23 01:07:12

若要通过API向SendGrid帐户添加营销联系人,请参阅https://sendgrid.api-docs.io/v3.0/contacts-api-recipients/add-recipients的文档。

您可以在页面的“代码生成”部分看到示例代码。

代码语言:javascript
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34513857

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档