imagewhatsappwhatsapi

Whatsapp Api Cloud Image Upload Issue


I am trying to upload an image for whataspp cloud api , i've transformed curl code to c# using RestSharp but i got this error . I was triying to change the file parameter format but it doesn't work. I don't know if i am missing something in the json maybe.

here is the code i use :

public void whatsapp_image_upload()
    {
        var client = new RestClient("https://graph.facebook.com/" + num_whatsapp_business + "/media");
        client.Timeout = -1;
        var request = new RestRequest(Method.POST);
        request.AddHeader("Authorization", "Bearer " + token_authorization);
        request.AddHeader("Content-Type", "application/json");
        request.AddParameter("messaging_product", "whatsapp");
        request.AddParameter("file", "C:\\Users\\cnarea\\Pictures\\empaque.jpg");
        request.AddParameter("type", "image/jpeg");

        IRestResponse response = client.Execute(request);
        Console.WriteLine(response.Content);
    }

and this is the error i recieve :

{"error":{"message":"An unknown error has occurred.","type":"OAuthException","code":1,"fbtrace_id":"AFWXnEVRuvp82ewjaUEtoLa"}}

Solution

  • Well i was able to solve it, i have to use postman app to obtain c# code ,my code is this :

     public void whatsapp_image_upload()
        {
            try
            {             
                string filePath = @"C:\Users\cnarea\Pictures\procesos.jpeg";
                var client = new RestClient("https://graph.facebook.com/"+num_whatsapp_business+"/media");
                client.Timeout = -1;
                var request = new RestRequest(Method.POST);
                request.AddHeader("Authorization", "Bearer "+token_authorization);
                request.AddFile("file", filePath, "image/jpeg");
                request.AddParameter("messaging_product", "whatsapp");
                IRestResponse response = client.Execute(request);
                Console.WriteLine(response.Content);
            }
            catch (Exception ex)
            {
    
                Console.WriteLine(ex.Message);
            }
    
          
        }