【发布时间】:2018-10-05 21:33:36
【问题描述】:
我正在尝试使用批处理 API 向我的列表成员添加一个新字段。 我可以向 API 发布一个批处理请求,然后我会收到一个批处理 ID。 批处理请求已成功接受,但当我检查该作业的状态时,我可以看到它已失败:
[{"status_code":404,"operation_id":null,
"response":"{\"type\":\"http:\/\/developer.mailchimp.com\/documentation\/
mailchimp\/guides\/error-glossary\/\",
\"title\":\"Resource Not Found\",
\"status\":404,
\"detail\":\"The requested resource could not be found.\",\"instance\":\"\"}"}]
Ruby 代码
98 def update_member_count_for_all_users
99 # https://developer.mailchimp.com/documentation/mailchimp/reference/batches/
100 operations = []
101 User.all.each do |user|
102 next unless user_valid?(user) and user.email.present?
103 subscriber_hash = Digest::MD5.hexdigest user.email.downcase
104
105 operations << {
106 method: "PUT",
107 path: "lists/#{list_id}/members/#{subscriber_hash}",
108 body: {
109 merge_fields: {
110 M_COUNT: user.family&.users&.count || 0
111 }
112 }
113 }
114 end
115
116 body = { operations: operations }
117 url = "#{base_url}/batches"
118 response = HTTParty.post(
119 url,
120 body: body.to_json,
121 basic_auth: { username: 'whatever', password: @api_key }
122 )
123
124 puts "POSTING: \n\n"
125 puts "#{base_url}/batches \n" +
126 "body: #{body} \n" +
127 "basic_auth: { username: 'whatever', password: #{@api_key} } \n"
128 puts "=========== \n"
129
130 puts "response is \n\n #{response} \n\n"
131
132 end
RSpec 输出
[{"id":"BATCH_ID","status":"pending","total_operations":0,
"finished_operations":0,"errored_operations":0,
"submitted_at":"2018-04-25T12:34:27+00:00","completed_at":"","response_body_url":"",
"_links":\[{"rel":"parent","href":"https://us17.api.mailchimp.com/3.0/batches",
"method":"GET","targetSchema":"https://us17.api.mailchimp.com/schema/
3.0/Definitions/Batches/CollectionResponse.json",
"schema":"https://us17.api.mailchimp.com/schema/3.0/CollectionLinks/Batches.json"},
{"rel":"self","href":"https://us17.api.mailchimp.com/3.0/batches/c2b6dc406e","method":"GET",
"targetSchema":"https://us17.api.mailchimp.com/schema/3.0/Definitions/Batches/Response.json"},{"rel":"delete", "href":"https://us17.api.mailchimp.com/3.0/batches/c2b6dc406e","method":"DELETE"}\]}][1]
检查批处理请求
➜ ~ curl https://us17.api.mailchimp.com/3.0/batches/BATCH_ID \
--user 'anystring:API-KEY'
"_links": [
{
"href": "https://us17.api.mailchimp.com/3.0/batches",
"method": "GET",
"rel": "parent",
"schema": "https://us17.api.mailchimp.com/schema/3.0/CollectionLinks/Batches.json",
"targetSchema": "https://us17.api.mailchimp.com/schema/3.0/Definitions/Batches/CollectionResponse.json"
},
{
"href": "https://us17.api.mailchimp.com/3.0/batches/6788b3cca5",
"method": "GET",
"rel": "self",
"targetSchema": "https://us17.api.mailchimp.com/schema/3.0/Definitions/Batches/Response.json"
},
{
"href": "https://us17.api.mailchimp.com/3.0/batches/6788b3cca5",
"method": "DELETE",
"rel": "delete"
}
],
"completed_at": "2018-04-25T12:25:20+00:00",
"errored_operations": 1,
"finished_operations": 1,
"id": "6788b3cca5",
"response_body_url": "https://mailchimp-api-batch.s3.amazonaws.com/6788b3cca5-response.tar.gz?AWSAccessKeyId=KEY&Expires=1524727875&Signature=SIG",
"status": "finished",
"submitted_at": "2018-04-25T12:25:13+00:00",
"total_operations": 1
}
这是 response_body_url 的内容:
[{"status_code":404,"operation_id":null,"response":"{\"type\":\"http:\/\/developer.mailchimp.com\/documentation\/mailchimp\/guides\/error-glossary\/\",\"title\":\"Resource Not Found\",\"status\":404,\"detail\":\"The requested resource could not be found.\",\"instance\":\"\"}"}]
【问题讨论】:
-
您可能需要在第 112 行执行
}.to_json。看起来 mailchip 想要将正文作为字符串,您正在发送一堆 json 对象,其中正文也是 json 对象,而不是一个包含 json 的字符串。 -
没错!发表您的评论作为答案。 :-D
-
也许您必须将 auth 标头添加到每个批处理操作,而不是仅添加到
Http.post。 -
根据 mailchimp api 文档,POST 中的基本身份验证就足够了。
-
我知道,这就是为什么我觉得它很奇怪!我也通过电子邮件发送了支持。将在此处为您提供最新信息。
标签: ruby httparty mailchimp-api-v3.0