Posting emails into facebook custom audience

I am trying to insert emails list into facebook custom audience using the following curl from docs:

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

After receiving valid access token with ads_management my code is the following:

  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);
                  });
        }
    }

The error is :

OAuth "Facebook Platform" "invalid_request" "Unsupported post request. Object with ID ... does not exist, cannot be loaded due to missing permissions, or does not support this operation. Please read the Graph API documentation at https://developers.facebook.com/docs/graph-api

What can be the reason? Both access token and audience ID seem to be correct.


It turned out that there was nothing wrong with code itself, I will leave it in case anybody will need it.

The problem was with access token. For Facebook Marketing API you can't get access token programmatically even if you set scope = ads_management .

You need to receive short lived token through web interface first.

Then having this short lived token you may get long lived token valid for 60 days if necessary.

Make sure to have ads_management permission for Marketing API.

链接地址: http://www.djcxy.com/p/32562.html

上一篇: 如何提交Facebook应用程序进行审查

下一篇: 将电子邮件发布到Facebook自定义观众