2015-11-07 102 views
0

我想在我的應用程序中將新註冊添加到Mailchimp列表中。它完美地通過捲曲聲明是這樣的:通過節點和Mailchimp API 3.0將成員添加到Mailchimp列表中

curl --request POST --url 'https://us4.api.mailchimp.com/3.0/lists/[listid]/members' --user 'anystring:[api key]-us4' --header 'content-type: application/json' --data '{"email_address":"[email protected]", "status":"subscribed","merge_fields":{"FNAME":"Freddie","LNAME":"Jones"}}' --include 

我使用的請求模塊的Node.js這樣的:

var request = require('request'); 

request({ 
    url: 'https://us4.api.mailchimp.com/3.0/lists/[list-id]/members', 
    user: 'anystring:[api-key]', 
    json: { 
     "email_address":"[email protected]", 
     "user":"anystring:[api-key]", 
     "status":"subscribed", 
     "merge_fields":{ 
      "FNAME":"Freddie", 
      "LNAME":"Jones" 
     } 
    }, 
    method: 'POST', 
    headers: { 
     'Content-Type': 'application/json' 
    } 
}, function(error, response, body){ 
     if(error) { 
      console.log(error); 
     } else { 
      console.log(response.statusCode, body); 
     } 
    } 
); 

但我得到這個錯誤:

401 { type: 'http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/', 
title: 'API Key Missing', 
status: 401, 
detail: 'Your request did not include an API key.', 
instance: '' } 

我如何正確地制定這個要求?

+0

順便說一句,我編寫了代碼示例中的API密鑰和列表ID。在我的實際要求中,那些是真正的要求。 – tonejac

回答

0

來回穿梭與客戶支持了幾次,終於想通,我需要我的API密鑰才能添加到請求頭正確使用MailChimp v3.0 API進行身份驗證。這裏是我使用的代碼:

request({ 
    url: 'https://us4.api.mailchimp.com/3.0/lists/[list id]/members', 
    json: { 
     'email_address': user.email, 
     'user': 'anystring:[api key]', 
     'status': 'subscribed', 
     'merge_fields': { 
      'FNAME': user.firstName, 
      'LNAME': '' 
     } 
    }, 
    method: 'POST', 
    headers: { 
     'Content-Type': 'application/json', 
     'Authorization': 'apikey [api key]' 
    } 
}, function(error, response, body){ 
    if(error) { 
     console.log(error); 
    } else { 
     console.log(response.statusCode, '>>>>> USER ADDED TO MAILCHIMP'); 
     // THEN log the user into the app 
     req.login(user, function(err) { 
      if (err) { 
       res.status(400).send(err); 
      } else { 
       res.json(user); 
      } 
     }); 
    } 
}); 
2

我剛剛與我最近被用來訪問Mailchimp一些代碼進行比較,發現我提供的API密鑰是這樣的:我用SuperAgent,而不是請求庫

var request = require('superagent'); // I am using SuperAgent 

request.post(url) 
.set('Authorization', 'apikey ' + apiKey) // this sets a header field 
.send(data) 
.end(function(err, response) { 
    // ... 
}); 

注意。您應該可以輕鬆移植該代碼段。

本質上,頭字段Authorization是字符串apikey(尾隨空格)和實際API密鑰的串聯。縱觀請求documentation,這應該工作:

request({ 
    url: 'https://us4.api.mailchimp.com/3.0/lists/[list-id]/members', 
    json: json, 
    method: 'POST', 
    headers: { 
     'Content-Type': 'application/json', 
     'Authorization': 'apikey ' + apiKey 
    } 
}, function(error, response, body) { 
    // ... 
}); 
+0

這是否意味着我需要爲我的請求添加授權密鑰值? – tonejac

+0

看到編輯,希望有所幫助。 – qqilihq

+0

連接時是否如此:'授權':'apikey 12345apikeystringvalueue' – tonejac

0

我在我的mailchimp v3 wrapper中使用請求。我使用以下授權沒有問題:

request({ 
    method : ..., 
    url : ..., 
    auth : { 
    user : 'any', 
    password : api_key 
    }, 
    json : ..., 
    qs : ... 
}