HTTParty to batch update users on MailChimp 3.0 API

半城伤御伤魂 提交于 2020-01-06 06:05:37

问题


I'm trying to use the batch API to add a new field to my list members. I'm able to post a batch request to the API and I receive a batch ID back. The batch request is accepted successfully but when I check the status of that job, I can see that is has failed:

[{"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 code

 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 output

    [{"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]

Checking the batch request

➜  ~ 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
}

This is the content of the 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\":\"\"}"}]

回答1:


I was using the wrong list ID. It's not the ID you see in the URL. From the page where you can see the list, click Settings, List names and default . That's where the list ID is located.



来源:https://stackoverflow.com/questions/50021727/httparty-to-batch-update-users-on-mailchimp-3-0-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!