Facebook Ad Leads检索asp.net

时间:2016-10-03 16:23:18

标签: asp.net-mvc facebook lead

我有没有办法从asp.net的Lead_ID读取广告引导数据?我看到有PHP的例子,但没有适用于C#的SDK。不幸的是我必须使用Asp.net而不能使用任何其他技术

非常感谢任何建议

1 个答案:

答案 0 :(得分:0)

您可以使用facebook sdk c#和

var fb = new FacebookClient
            {
                AccessToken ="token"
            };
            var json = fb.Get("page_id or ad_id/leadgen_forms");
            var forms = JsonConvert.DeserializeObject<FormsLeads>(json.ToString());
            foreach (var form in forms.data)
            {
                var jsonre = fb.Get(form.id + "/leads");
                var leads = JsonConvert.DeserializeObject<Leads>(jsonre.ToString());
                while (leads.paging != null)
                {
                    foreach (var lead in leads.data)
                    {
                        var leadnovo = new Models.Lead();
                        leadnovo.CamposExtras.Add(new CampoExtra { Nome = "idfacebook", Valor = lead.id });
                        leadnovo.DataCadastro = lead.created_time;

                        foreach (var t in lead.field_data)
                        {
                            if (t.name == "full_name")
                            {
                                leadnovo.Nome = t.values.FirstOrDefault();
                            }
                            else
                                if (t.name == "email")
                                {
                                    leadnovo.Email = t.values.FirstOrDefault();
                                }
                                else
                                    if (t.name == "phone_number")
                                    {
                                        leadnovo.Celular = t.values.FirstOrDefault();
                                    }
                                    else
                                    {
                                        leadnovo.CamposExtras.Add(new CampoExtra()
                                        {
                                            Nome = t.name,
                                            Valor = t.values.FirstOrDefault()

                                        });
                                    }
                        }
                        if (db.Leads.FirstOrDefault(c => c.Email == leadnovo.Email) == null)
                        {
                            db.Leads.Add(leadnovo);
                            db.SaveChanges();
                        }
                    }
                    if (leads.paging != null)
                    {
                        jsonre = fb.Get(form.id + "/leads?after=" + leads.paging.cursors.after);
                        leads = JsonConvert.DeserializeObject<Leads>(jsonre.ToString());
                    }
                }
            }
        }

反序列化的类

 public class Cursors
{
    public string before { get; set; }
    public string after { get; set; }
} 
public class Field_Data
    {
        public string name { get; set; }
        public string[] values { get; set; }
    }


 public class FormsLeads
    {
        public Formulario[] data { get; set; }
        public Paging paging { get; set; }
    }
 public class Formulario
    {
        public string id { get; set; }
        public string leadgen_export_csv_url { get; set; }
        public string locale { get; set; }
        public string name { get; set; }
        public string status { get; set; }
    }
 public class Lead
    {
        public DateTime created_time { get; set; }
        public string id { get; set; }
        public Field_Data[] field_data { get; set; }
    }
public class Leads
    {
        public Lead[] data { get; set; }
        public Paging paging { get; set; }
    }
public class Paging
    {
        public Cursors cursors { get; set; }
    }