将电子邮件发布到Facebook自定义受众群体中

时间:2016-05-18 08:28:23

标签: c# facebook facebook-graph-api curl

我正尝试使用docs中的以下卷曲将电子邮件列表插入Facebook自定义受众群体:

 curl \
-F 'payload={ 
 "schema": "EMAIL_SHA256", 
   "data": [
    "9b431636bd164765d63c573c346708846af4f68fe3701a77a3bdd7e7e5166254",
    "8cc62c145cd0c6dc444168eaeb1b61b351f9b1809a579cc9b4c9e9d7213a39ee",
    "4eaf70b1f7a797962b9d2a533f122c8039012b31e0a52b34a426729319cb792a" 
    ]
  }' \
-F 'access_token=<ACCESS_TOKEN>' \

收到ads_management的有效访问令牌后,我的代码如下:

  class Payload
    {
        public Payload(List<string> strings)
        {
            schema = "EMAIL_SHA256";
            data = new List<string>();                
            foreach (string str in strings)
            {
                data.Add(sha256(str));
            }
        }
        public string schema { get; set; }
        public List<string> data { get; set; }
    }

    class PostData
    {
        public PostData(string token, List<string> strings)
        {
            access_token = token;
            payload = new Payload(strings);
        }
        public Payload payload;
        public string access_token { get; set; }
    }  

    public async Task InsertEmails(List<string> emails, string id)
    {     
        using (var httpClient = new HttpClient())
        {
            httpClient.BaseAddress = new Uri("https://graph.facebook.com/");                
            httpClient.DefaultRequestHeaders
              .Accept
              .Add(new MediaTypeWithQualityHeaderValue("application/json"));                

            HttpRequestMessage request = new HttpRequestMessage(System.Net.Http.HttpMethod.Post, "v2.6/" + id + "/users");                
            PostData postData = new PostData(_accessToken, emails);                
            request.Content = new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(postData),
                                Encoding.UTF8,
                                "application/json");                         
            await httpClient.SendAsync(request)
                  .ContinueWith(responseTask =>
                  {
                      Console.WriteLine("Inserting emails into Facebook Response: {0}", responseTask.Result);
                  });
        }
    }

错误是:

  

OAuth \&#34; Facebook平台\&#34; \&#34; INVALID_REQUEST \&#34; \&#34;不支持的帖子请求。具有ID ...的对象不存在,由于缺少权限而无法加载,或者不支持此操作。请阅读https://developers.facebook.com/docs/graph-api

上的Graph API文档

可能是什么原因?访问令牌和受众ID似乎都是正确的。

1 个答案:

答案 0 :(得分:0)

事实证明,代码本身没有任何问题,如果有人需要,我会留下它。

问题在于访问令牌。对于Facebook Marketing API,即使您设置scope = ads_management,也无法以编程方式获取访问令牌。

您需要先通过网络界面接收短期令牌。

然后拥有这个短暂的令牌,如果有必要,您可以获得有效期为60天的长期令牌。

确保拥有Marketing API的ads_management权限。