2015-12-15 117 views
0

我已經分配了使用MailChimp API提取某些數據的任務。到目前爲止,該腳本已經毫無問題地提取了報告,廣告系列,列表和成員詳細信息。但是,文檔here中提到的工作流程自動化API不起作用。事實上,在DOC給出的這個非常CURL例子是給404(資源未找到)錯誤:Mailchimp自動化API返回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

我suggestin使用'requests'這是一個生命的救星。 – taesu

+0

@taesu我知道它是一個很好的建議,我使用'requests'。但在某些情況下,我更喜歡使用香草python,而不是依賴外部庫。省去包裝和配置的麻煩。 –

+2

祝你好運。 404只是一個404.你應該聯繫mail-chimp不要SO。 – taesu

回答

0

我希望Mailchimp能迴應我的SO問題,但這次並沒有成功!取而代之的是,我的客戶不得不要求從Mailchimp的支持,這是他們說什麼:

  1. 首先,我們要調用/automations端點檢索「正確」 workflow_id

  2. 然後,我們必須使用上述結果作爲參數到/automations/{workflow_id}/emails/端點並獲取電子郵件發送和其他數據。

這種技術的工作,在這裏參考了修改後的代碼的一部分:

## 
    # Lets fetch the automation data now 
    # 
    autos = [] 
    data ={} 
    print "Now pulling automation data (UNSTABLE AND UNTESTED FEATURE)" 
    turl = baseurl + "automations" #lists/" + lid + "/members" 
    print "turl is " + turl 
    if DEBUG: 
     tfp = open('sample_automation_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['automations']) 
    workflow_id = tdata['automations'][0]["id"] 
    print tcnt, " automation settings pulled. The workflow_id is " + workflow_id 

    print "Now pulling automation emails using workflow_id " + workflow_id + " (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 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.." 
相關問題