skip to Main Content

I’m trying to implement whatsapp business API but I’m getting forbidden error I think its because i dont have enough permission.
I have also implemented this code on Postman its works fine there but its not working in app i dont know why?

var client = new HttpClient();
        var request = new HttpRequestMessage(HttpMethod.Post, "https://graph.facebook.com/v15.0/110474688636083/messages");
        request.Headers.Add("Authorization", "Bearer EAAM2wERIcIsBAFSGQD3yCSYRd5II5u7hU1859z8VcpNFlZBjJrqJUR2QrgZADHlHYSCG0zWvpYqVkFlzea9TsN1wnu8ZBZBSiEaXQu5OZAQC63ufVKZAQDHZB25CIq3TBQ9rxr2DdZB1oZBgJtia4eAEBbzqfjwJpXm9M5SZCGhDh7JbK0s1ldz2Od099jHfKrFvnQDZD");
        var content = new StringContent("{n    "messaging_product": "whatsapp",n    "to": ""+WHATSAPPNO+"",n    "type": "template",n    "template": {n        "name": "hello_world",n        "language": {n            "code": "en_US"n        }n    }n}", null, "application/json");
        request.Content = content;
        var response = await client.SendAsync(request);
        response.EnsureSuccessStatusCode();
        Console.WriteLine(await response.Content.ReadAsStringAsync());

thank you for your time

2

Answers


  1. You did not specify the access token.

    request.Headers.Add("Authorization", "Bearer TOKEN");
    

    TOKEN here is an placeholder and not a real token

    Login or Signup to reply.
  2. This will help you to resolve your issue,

    • JSON is not in correct format in your code.

    Model files,

    public class Language
    {
        public string code { get; set; }
    }
    
    public class WhatsAppDataModel
    {
        public string messaging_product { get; set; }
        public string to { get; set; }
        public string type { get; set; }
        public Template template { get; set; }
    }
    
    public class Template
    {
        public string name { get; set; }
        public Language language { get; set; }
    }
    

    Then you need two packages RestClient and Newtonsoft.Json,
    Main c# class function,

    string url = "https://graph.facebook.com/v15.0/110474688636083/messages";
    Uri baseUri = new Uri(url);
    
    IRestClient client = new RestClient(baseUri)
    IRestRequest request = new RestRequest("", Method.Post);
    
    request.AddHeader("Authorization", "Bearer " + Token);
    request.AddHeader("Content-Type", "application/json");
    
    Language lang = new Language();
    lang.code = "en_US";
    
    Template template = new Template();
    template.name = "hello_world";
    template.language = lang;
    
    WhatsAppDataModel whatsapp = new WhatsAppDataModel();
    whatsapp.messaging_product = "whatsapp";
    whatsapp.to = "<whatsappNumber>";
    whatsapp.type = "template";
    whatsapp.template = template;
    
    string body = JsonConvert.SerializeObject(whatsapp);
    
    request.AddParameter("application/json", body , ParameterType.RequestBody);
    
    var response = client.Execute(request);
    var content = response.Content; // raw content as string  
    

    facebook graph API latest version is v16.0 and not v15.0. So try updating that might also help you.

    Changelog

    Format is change for sending message in v16.0 check this

    curl -X POST 
      'https://graph.facebook.com/v16.0/FROM_PHONE_NUMBER_ID/messages' 
      -H "Authorization: ACCESS_TOKEN" 
      -d '{
        "messaging_product": "whatsapp",
        "to": "1650XXXXXXX",
        "text": {"body" : "hi"}
       }'
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search