1

我正在尝试创建一些简单的 Ruby 代码来使用 Campaign Monitor API 添加电子邮件。下面是我的代码。

require 'httparty'
require 'json'

def request
    url = 'https://api.createsend.com/api/v3.1/subscribers/MYLISTID.json'
    auth = {:username => 'MYAPIKEY', :password => 'x'}
    response = HTTParty.post(url,
                  :basic_auth => auth, :body => {
                      'EmailAddress' => 'mike@hotmail.com',
                      'Name' => 'Test',
                      'Resubscribe' => true,
                      'RestartSubscriptionBasedAutoresponders' => true
                  })
    puts response
    puts response.code
end
request

我可以连接 API。但是,当我尝试添加电子邮件时,我收到以下回复。

{"Code"=>400, "Message"=>"无法反序列化您的请求。
请检查文档并重试。
错误字段:订阅者"}
400

当我将请求更改为get而不是put 我的响应时:

{"代码"=>1, "消息"=>"无效的电子邮件地址"}

我不明白我做错了什么,因为我遵循了Campaign Monitor API上的文档

4

1 回答 1

0

看起来您已经正确设置了所有内容,您只需要将帖子的正文转换为 json 字符串。

  response = HTTParty.post(url,
    :basic_auth => auth, :body => {
       'EmailAddress' => 'mike@hotmail.com',
       'Name' => 'Test',
       'Resubscribe' => true,
       'RestartSubscriptionBasedAutoresponders' => true
     }.to_json)

我想指出,还有一个 Campaign Monitor API gem 可以为您完成所有这些工作。

活动监控 API Gem

于 2015-10-15T14:38:25.980 回答