使用scribe库将图像发布到Facebook页面

时间:2012-05-23 21:10:36

标签: java facebook scribe

我正在尝试使用scribe库将图片发布到Facebook。 我的代码是:

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

    }

但我也得到了

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

任何人都可以请求帮助,如果我拿出Multipart,文本“测试自动更新..请我......”更新到Facebook。我需要上传一张图片并作为字节[]。

由于

我正在使用:

 <dependency>
<groupId>org.scribe</groupId>
<artifactId>scribe</artifactId>
<version>1.3.0</version>
</dependency>

1 个答案:

答案 0 :(得分:1)

Scribe使用addBodyParameteraddPayload,而非两者。请参阅this method

如果你采用addPayload方式,那么我想你必须弄清楚如何将message引入多部分。

注意:请求的实际内容的字符串输出(使用Request#getBodyContents)会很好。