skip to Main Content

Here is the curl From Facebook Docs to Upload a file to their servers for sending to the specified user:

curl  
  -F recipient='{"id":"USER_ID"}' 
  -F message='{"attachment":{"type":"file", "payload":{}}}' 
  -F filedata=@/tmp/receipt.pdf 
  "https://graph.facebook.com/v2.6/me/messages?access_token=PAGE_ACCESS_TOKEN"


curl  
  -F 'recipient={"id":"USER_ID"}' 
  -F 'message={"attachment":{"type":"audio", "payload":{}}}' 
  -F 'filedata=@/tmp/clip.mp3;type=audio/mp3' 
  "https://graph.facebook.com/v2.6/me/messages?access_token=PAGE_ACCESS_TOKEN"

I am able to do this via Curl but I would like to do that using Java and Spring-Boot.
How to go about it?

The file I am trying to send is in my src-main-resource-docs folder.

Edit 1
I wanted to know what’s this filedata=@/tmp/clip.mp3;type=audio/MP3
Should this just be the link to my resource file?

Or do I have to change it to MultipartFile to send it?

Edit 2
Currently this is what I am doing:

  CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost uploadFile = new HttpPost("https://graph.facebook.com/v2.6/me/messages?access_token=PAGE_ACCESS_TOKEN");
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.addTextBody("recipient", "{"id":"USER_ID"}", ContentType.APPLICATION_JSON);


        File f = new File("./src/main/resources/docs/test.pdf");
        builder.addBinaryBody(
            "file",
            new FileInputStream(f),
            ContentType.APPLICATION_OCTET_STREAM,
            f.getName()
        );

        HttpEntity multipart = builder.build();
        uploadFile.setEntity(multipart);
        CloseableHttpResponse response = httpClient.execute(uploadFile);
        HttpEntity responseEntity = response.getEntity();

This works fine for pdfs

But in audio/mp3 there is a type=audio/mp3 . Where do I put that?

2

Answers


  1. Chosen as BEST ANSWER

    Finally was able to upload Docs/Media to Facebook via the Send Api Reference,here's what I did:

    I used OkHttp3 library to build the request,

    /*
            *For Pdf ->MediaType.parse("file/pdf")
            *For Video-> MediaType.parse("video/mp4")
            *For Image-> MediaType.parse("image/png")
            */
            final MediaType MEDIA_TYPE_PNG=MediaType.parse("audio/mp3");
            final OkHttpClient client =new OkHttpClient();
    
    
            RequestBody requestBody = new MultipartBody.Builder()
              .setType(MultipartBody.FORM)
              .addFormDataPart("filedata", "file_name.extension", RequestBody.create(MEDIA_TYPE_PNG, new File("path_of_file")))
              .addFormDataPart("recipient","{"id":"USER_ID"}")
              .addFormDataPart("message", "{"attachment":{"type":"TYPE_OF_FILE", "payload":{}}}")//as in Facebook Docs
              .build();
    
          Request request = new Request.Builder()
              .url("https://graph.facebook.com/v2.6/me/messages?access_token=ACCESS_TOKEN")
              .post(requestBody)
              .build();
    
    
          Response response = client.newCall(request).execute();
    
          System.out.println(response.body().string());
    

    One needs to add exception handling for IOException


  2. Take a look into restFb library. With it you will be able to do:

    String fileName = YOUR_FILE_NAME;
    byte[] fileBytes = multipartFile.getBytes(); // could be from MultipartFile, for exaple file upload from browser
    String fileType = "text/csv";
    
    BinaryAttachment binaryAttachment = BinaryAttachment.with("file", fileName, fileBytes, fileType);
    // FacebookClient from the library, look in the library docs how to use it; 
    // also there are several overloaded publish methods you can use
    facebookClient.publish(endpoint, String.class, binaryAttachment);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search