本文介绍了Rails - 如何通过 API 将联系人添加到 SendGrid 营销活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要使用 SendGrid 发出 HTTP get 和 post 请求以将联系人添加到我们的帐户,但是他们的电子邮件营销功能似乎没有什么亮点.
I need to make HTTP get and post requests with SendGrid to add contacts to our account, however there doesn't seem to be a gem for their email marketing functionality.
这归结为提出一些请求,但我无法通过他们的身份验证步骤.
It boils down to making a few requests however I can't get past their authentication step.
curl -X "GET" "https://api.sendgrid.com/v3/templates" -H "Authorization: Bearer Your.API.Key-HERE" -H "Content-Type: application/json"
并且使用 Rest-Client gem 我正在尝试像这样发出身份验证请求......
And using the Rest-Client gem I'm trying to make the authentication request like so...
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"}]}
我如何错误地发出此获取请求?
How am I incorrectly making this get request?
推荐答案
我最终想通了.为了将来参考,这是有效的代码...
I ended up figuring it out. For future reference, here's the code that worked...
require 'rest_client'
api_key = 'YOUR_API_KEY'
headers = {'Authorization' => "Bearer #{api_key}"}
data = {:email => '[email protected]'}
response = RestClient.post 'https://api.sendgrid.com/v3/contactdb/recipients', [data].to_json, headers
这篇关于Rails - 如何通过 API 将联系人添加到 SendGrid 营销活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!