0

I am trying to post an image to facebook using scribe library. My code is:

String apiKey = "MY_API_KEY";
        String apiSecret = "MY_SECRET_KEY";
        OAuthService service = new ServiceBuilder().provider(FacebookApi.class).apiKey(apiKey)
                .apiSecret(apiSecret)
                .callback("MY_CALL_BACK_URL")
                .build();

        Token accessToken = new Token("MY_ACCESS_TOKEN","");
        OAuthRequest request = new OAuthRequest(Verb.POST, PROTECTED_RESOURCE_URL_STREAM);       
        request.addHeader("Content-Type", "text/html");      
        request.addBodyParameter("message", "Testing auto update.. Please ignore " + new DateTime());           
        //MultiPart for pic
        Multipart mp = new MimeMultipart();
        MimeBodyPart htmlPart = new MimeBodyPart();
        ByteArrayOutputStream out = new ByteArrayOutputStream();    
        try {
            byte[] imagePayLoad = msgBody.getBytes("UTF-8");    //msgBody contains html string   
            byte[] mpByte = new byte[imagePayLoad.length];
            htmlPart.setContent(msgBody, "text/html");
            mp.addBodyPart(htmlPart);           
            //here I get the bytes[] of mp to out
            mp.writeTo(out);
            out.write(mpByte);      

            request.addPayload(mpByte);
        } catch (Exception e) {         
            e.printStackTrace();
        }   
        service.signRequest(accessToken, request);
        Response response = request.send();
        System.out.println("Response");
        System.out.println();    
        System.out.println(response.getCode());
        System.out.println(response.getBody());

    }

But I also get

400
{"error":{"message":"(#100) Missing message or attachment","type":"OAuthException","code":100}}

Can anyone please help, If I take out the Multipart , The text "Testing auto update.. Please i.." is updated to Facebook. I need to upload a picture and as a byte[].

Thanks

I am using:

 <dependency>
<groupId>org.scribe</groupId>
<artifactId>scribe</artifactId>
<version>1.3.0</version>
</dependency>
Babajide Prince
  • 552
  • 1
  • 10
  • 25

1 Answers1

1

Scribe uses either addBodyParameter or addPayload, not both of them. See this method.

If you go the addPayload way, then I guess you'll have to figure out how to get message into the multipart as well.

Note: a string output of the actual contents of the request (using Request#getBodyContents) would be nice.

Pablo Fernandez
  • 103,170
  • 56
  • 192
  • 232
  • thanks @Pablo Was about to ask that I don't see some methods and no org.scribe.model.Request in the lib I have, I just saw your update now about version 1.3.1. I also noticed byte[] getByteBodyContents() is not accessible , in fact Eclipse warns me that org.scribe.model.Request is not visible. thanks for any help :) – Babajide Prince May 23 '12 at 22:04
  • `getBodyContents` should be public (see the link). It's the variant that returns a string. – Pablo Fernandez May 24 '12 at 00:59
  • Since I am sending a binary content, I am interested in `getByteBodyContents()` which is not public as seen in `org.scribe.model.Request` line 256. It does not have a modifier hence not accessible from `org.scribe.model.OAuthRequest` - is this by design? Can I fork the repo and fix ? - Thanks – Babajide Prince May 24 '12 at 08:29
  • I have been able to verify my request sent as a byte[] but I still get 400 error. request sent: [B@772a7557 Response `400{"error":{"message":"(#100) Missing message or attachment","type":"OAuthException","code":100}}` – Babajide Prince May 24 '12 at 12:08
  • For further information I really need to see the multipart message with `getBodyContents`. Forking the lib for getting the bytes is an overkill since you can easily transform a string into a byte array. – Pablo Fernandez May 25 '12 at 03:07