获取所有列表成员 - MailChimp 3.0
Posted
技术标签:
【中文标题】获取所有列表成员 - MailChimp 3.0【英文标题】:Get All List Members - MailChimp 3.0 【发布时间】:2017-08-15 09:09:17 【问题描述】:我正在尝试获取列表中的所有成员(大约 19,000 名成员),并在 C# 中使用 Mailchimp.NET.V3 包。
以下代码只检索前 1000 个成员
IMailChimpManager MC = new MailChimpManager(@"xxxxxxxxxxxxxxxxxxxxxxxxx-xxx");
var listMembers = await MC.Members.GetAllAsync(ListId);
我也尝试过使用MemberRequest
构造函数,但这永远不会返回任何值。
var listMembers = await MC.Members.GetAllAsync(ListId, new MemberRequest Limit = 20000 );
有人可以帮忙吗?谢谢!
【问题讨论】:
【参考方案1】:您需要使用 'count' 参数,而不是限制。
【讨论】:
【参考方案2】:使用偏移值。
List<Member> listMembers = new List<Member>();
IMailChimpManager manager = new MailChimpManager(MailChimpApiKey);
bool moreAvailable = true;
int offset = 0;
while (moreAvailable)
var listMembers = manager.Members.GetAllAsync(yourListId, new MemberRequest
Status = Status.Subscribed,
Limit = 250,
Offset = offset
).ConfigureAwait(false);
var Allmembers = listMembers.GetAwaiter().GetResult();
foreach(Member member in Allmembers)
listMembers.Add(member);
if (Allmembers.Count() == 250)
//if the count is < of 250 then it means that there aren't more results
offset += 250;
else
moreAvailable = false;
【讨论】:
【参考方案3】:我在 unity 3D 上使用 C# 脚本完成了类似的操作,包括基本身份验证和 WWW 请求。此脚本使用 API 3.0 返回 Mailchimp 上列表的成员。也许这段代码有帮助。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class mailchimpRESTful : MonoBehaviour
/*
* USE using CURL:
*
curl --request GET \
--url 'https://<dataCenter>.api.mailchimp.com/3.0/lists/<your-list-name>/members/' \
--user 'anystring:<mailchimp-api-key>'
EXAMPLE using CURL:
curl --request GET \
--url 'https://us12.api.mailchimp.com/3.0/lists/era987af43/members/' \
--user 'anystring:e419ac3fefefgkjne0559901222a3dbf-us12'
*/
// Mailchimp API RESTful C# script GET Member list
private const string DC = "<your-data-center>"; // us1,us2..us18
private const string LIST = "<your-list-name>"; // Something similar to: era987af43
private const string URL = "<your-url>"; // "https://<dataCenter>.api.mailchimp.com/3.0/lists/<your-list-name>/members/";
private const string USER = "anystring"; // You can use any user
private const string API_KEY = "your-api-key"; // Mailchimp API-key: e419ac3fefefgkjne0559901222a3dbf-us12
public void Start()
Request ();
public void Request()
Dictionary<string, string> headers = new Dictionary<string,string>();
headers ["Authorization"] = "Basic " + System.Convert.ToBase64String (System.Text.Encoding.ASCII.GetBytes (USER + ":" + API_KEY));
WWW request = new WWW(URL, null, headers);
StartCoroutine (OnResponse (request));
private IEnumerator OnResponse(WWW req)
yield return req;
Debug.Log ("Query Result:"+req.text);
【讨论】:
【参考方案4】: private async Task<List<Member>> GetMemberListAsync(string listId)
var offset = 0;
var moreAvailable = true;
var listMembers = new List<Member>();
while (moreAvailable)
var result = await manager.Members.GetAllAsync(listId, new MemberRequest
Status = Status.Subscribed,
Limit = 250,
Offset = offset
);
var resultList = result.ToList();
foreach (var member in resultList)
listMembers.Add(member);
if (resultList.Count() == 250)
offset += 250;
else
moreAvailable = false;
return listMembers;
【讨论】:
【参考方案5】:var listMembers = manager.Members.GetAllAsync(ListId).ConfigureAwait(false);
var Allmembers = listMembers.GetAwaiter().GetResult();
【讨论】:
虽然这段代码 sn-p 可以解决问题,但including an explanation 确实有助于提高帖子的质量。请记住,您正在为将来的读者回答问题,而这些人可能不知道您的代码建议的原因。也请尽量不要用解释性的 cmets 挤满你的代码,因为这会降低代码和解释的可读性! 代码sn-p只返回1000,作者想得到19000。以上是关于获取所有列表成员 - MailChimp 3.0的主要内容,如果未能解决你的问题,请参考以下文章
是否可以在 mailchimp API 3.0 中按成员获取细分?
通过合并字段使用 MailChimp API 3.0 搜索成员