ruby-on-rails - Rails - 如何通过 API 将联系人添加到 SendGrid 营销事件

标签 ruby-on-rails ruby rest sendgrid rest-client

我需要使用 SendGrid 发出 HTTP get 和 post 请求以将联系人添加到我们的帐户,但是他们的电子邮件营销功能似乎没有什么好东西。

归结为提出一些请求,但我无法通过他们的身份验证步骤。

They say to do this

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/

相关文章:

rest - 在 gRPC 之上对 REST web 服务进行版本控制

rest - CakePHP 3.x REST API-调试关闭时的自定义错误消息

ruby-on-rails - 如何让记录器在 IRB 中工作?

ruby-on-rails - 抓取包含文本 nokogiri xpath 的元素

ruby-on-rails - 如何使用 ruby​​ 使用 wcf 服务?

ruby - ruby 中的模板方法

ruby - CoffeeScript 风格的初始化没有进入 Ruby 2.0?

rest - Marklogic REST API - 检索文件内容

ruby-on-rails - 如何将 "where not"子句与 searchkick 一起使用

python - 切换参与者进出 Google Hangouts