Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails-如何通过API向SendGrid营销活动添加联系人_Ruby On Rails_Ruby_Rest_Sendgrid_Rest Client - Fatal编程技术网

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

Ruby on rails Rails-如何通过API向SendGrid营销活动添加联系人,ruby-on-rails,ruby,rest,sendgrid,rest-client,Ruby On Rails,Ruby,Rest,Sendgrid,Rest Client,我需要使用SendGrid发出HTTP get和post请求,以将联系人添加到我们的帐户中,但是他们的电子邮件营销功能似乎没有什么优势 它归结为提出一些请求,但我无法通过他们的身份验证步骤 使用Rest客户机gem,我试图发出这样的身份验证请求 username = 'username' api_key = 'SG.MY_API_KEY' key = Base64.encode64(username + ":" + api_key) headers = {"Authorization" =&g

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

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

使用Rest客户机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"}]}
这是我们的最终目标


我怎么会错误地发出这个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

我终于弄明白了。作为将来的参考,下面是有效的代码

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帐户, 请参阅位于的文档

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

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
require'uri'
需要“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\u mode=OpenSSL::SSL::verify\u NONE
request=Net::HTTP::Post.new(url)
请求[“授权”]=“持有人”
请求[“内容类型”]=“应用程序/json”
request.body=“[{\”email\”:\”example@example.com\“,”名字“:\”,”姓氏“:\”用户“,”年龄“:25},{\”电子邮件“:\”example2@example.com\“,”名字“:”示例“,”姓氏“:”用户“,”年龄“:25}”
response=http.request(请求)
将response.read\u放入正文

要通过API向SendGrid帐户添加营销联系人, 请参阅位于的文档

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

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
require'uri'
需要“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\u mode=OpenSSL::SSL::verify\u NONE
request=Net::HTTP::Post.new(url)
请求[“授权”]=“持有人”
请求[“内容类型”]=“应用程序/json”
request.body=“[{\”email\”:\”example@example.com\“,”名字“:\”,”姓氏“:\”用户“,”年龄“:25},{\”电子邮件“:\”example2@example.com\“,”名字“:”示例“,”姓氏“:”用户“,”年龄“:25}”
response=http.request(请求)
将response.read\u放入正文

RestClient.get'https://api.sendgrid.com/v3/templates“,{:content_type=>:json},{:Authorization=>“Bearer{api_key}”}
参数错误:参数数量错误(3代表1..2)
您的代码看起来正常。您是否明确配置了API密钥以授权在中使用必要的API?是的。我还创建了一个可以访问所有内容的新密钥,以确保它不是那样的。
RestClient.get'https://api.sendgrid.com/v3/templates“,{:content_type=>:json},{:Authorization=>“Bearer{api_key}”}
参数错误:参数数量错误(3代表1..2)
您的代码看起来正常。您是否明确配置了API密钥以授权在中使用必要的API?是的。我还创建了一个可以访问所有内容的新密钥,以确保它不是那样的。谢谢你,这很有用,我想如果你想添加多个联系人,你可以像这样拥有它data=[{:email=>'email@website.com“},{:email=>”email2@website.com'}]并将[]从[data].中删除到_json。那么,如何才能将它们添加到特定的列表谢谢你这很有用,我想如果你想添加多个联系人,你可以像这样拥有它data=[{:email=>'email@website.com“},{:email=>”email2@website.com'}]并将[]从[data].中删除到_json。那么,如何将它们添加到特定列表中呢