Mailchimp自动化API返回404错误(找不到资源)

时间:2015-12-15 19:06:32

标签: python curl mailchimp

我被分配了使用MailChimp API提取一些数据的任务。到目前为止,该脚本已经毫无问题地提取了报告,活动,列表和成员详细信息。但是,文档here中提到的Workflow Automation API无法正常工作。事实上,该文档中给出的非常CURL示例给出了404(未找到资源)错误:

curl --request GET \
--url 'https://usX.api.mailchimp.com/3.0/automations/b0a1c24f1a/emails' \
--user 'anystring:apikey' \
--include

我只用我自己的数据中心号码替换了usX并更新了我的apiKey。在任何情况下,这是我的整个automation.py python脚本的自动化部分,我的用户正在抱怨:

#!/usr/bin/python
import urllib
import urllib2
import base64
import json
import csv
import sys
import os
import codecs
__version__ = "1.4"

##
# Configures the MailChimpExpress
reload(sys)
sys.setdefaultencoding('utf8')
sys.stdout = codecs.getwriter('utf8')(sys.stdout)
workdir = "data/mailchimp/" 
workflow_id = "b0a1c24f1a"


##
# Writes a list to csv
#
# @param tname name of the csv file to output without extension
# @param tlist the python list to write
def write_to_csv(tname, tlist):
    myfile = open(workdir + tname + ".csv", 'wb')
    wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)
    for li in tlist:
        wr.writerow(li)
    myfile.close()
    return tname + ".csv"

##
# Creates a file with specified name and text content.
#
# @param tname Name of the file with extension.
# @param ttext Content to write.
def createfile(tname, ttext):
    myfile = open(workdir + tname , 'w')
    myfile.write(ttext)
    myfile.close()

##
# Pulls data from Mailchimp automation API.
def run():
    key = sys.argv[1] # CAPTURE THE API KEY

    dc = key.split("-")[-1] #this is the data-center where your request goes
    username = "anystring" #could be literally anything as per mailchimp docs!
    output = "" #var to hold raw json
    data = "" #var to hold json dict
    cnt = 0 #counter to keep track of fetched objects

    ##
    # FETCH ALL REPORTS
    #
    campaigns = []
    baseurl = "https://" + dc + ".api.mailchimp.com/3.0/"
    psize, i = 1000, 0

    ##
    # Lets fetch the automation data
    #
    print "Now pulling automation data (UNSTABLE AND UNTESTED FEATURE)"
    autos = []
    data ={}
    #while(True): #No longer needed as there is no limit/offset here
    turl = baseurl + "automations/" +  workflow_id + "/emails" #lists/" + lid + "/members"
    print "turl is " + turl
    if False: #DEBUG:
        tfp = open('sample_workflow_response.json')
        output = tfp.read()
        tfp.close()
    else:
        request = urllib2.Request(turl)
        base64string = base64.encodestring('%s:%s' % (username, key)).replace('\n', '')
        request.add_header("Authorization", "Basic %s" % base64string)
        output = urllib2.urlopen(request).read()
    tdata = json.loads(output)
    tcnt = len(tdata['emails'])

    print tcnt, " emails pulled."
    print ""
    print 'ID', 'position', "create_time","start_time","archive_url"

    for email in tdata["emails"]:
        print email['id'], email['position'], email["create_time"], email["start_time"], email["archive_url"]

    MailChimpExpress.createfile("lastauto.json", output)

    ###
    print "All is well.."

if __name__ == "__main__":
    run()

当我运行上面的代码时,它给出了以下输出:

Now pulling automation data (UNSTABLE AND UNTESTED FEATURE)
turl is https://us8.api.mailchimp.com/3.0/automations/b0a1c24f1a/emails
Traceback (most recent call last):
  File "./automation.py", line 97, in <module>
    run()
  File "./automation.py", line 80, in run
    output = urllib2.urlopen(request).read()
  File "/usr/lib/python2.7/urllib2.py", line 127, in urlopen
    return _opener.open(url, data, timeout)
  File "/usr/lib/python2.7/urllib2.py", line 410, in open
    response = meth(req, response)
  File "/usr/lib/python2.7/urllib2.py", line 523, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/lib/python2.7/urllib2.py", line 448, in error
    return self._call_chain(*args)
  File "/usr/lib/python2.7/urllib2.py", line 382, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.7/urllib2.py", line 531, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 404: Not Found

0 个答案:

没有答案