Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
facebook post photo
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js"></script>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script type="text/javascript">
<!--
window.fbAsyncInit = function(){
FB.init({
appId: '${fbInfo.appId}',
cookie: true,
status: true
});
};
//-->
</script>
<title>score</title>
</head>
<body>
<div id="fb-root"></div>
<form method="post" enctype="multipart/form-data" action="/facebook-sample/server/photo">
<input type="file" id="wallPhoto" name="wallPhoto"><br>
<input type="text" id="test" name="test" value="testtest">
<input type="hidden" name="signed_request" value="${signedRequestParam}">
<input type="submit" value="upload">
</form>
</body>
</html
import java.io.InputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jp.ne.hachi.facebook.bean.FacebookInfo;
import jp.ne.hachi.facebook.bean.SignedRequestInfo;
import jp.ne.hachi.facebook.service.FacebookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import com.restfb.BinaryAttachment;
import com.restfb.DefaultFacebookClient;
import com.restfb.FacebookClient;
import com.restfb.Parameter;
@Controller
public class IndexController {
@Autowired
private FacebookService facebookService ;
@Autowired
private FacebookInfo facebookInfo;
@ModelAttribute(value="fbInfo")
public FacebookInfo getFacebookInfo(){
return facebookInfo;
}
@RequestMapping("/")
public String index(@RequestParam("signed_request") String signedRequestParam,
ModelMap modelMap, HttpServletRequest request, HttpServletResponse response){
SignedRequestInfo signedRequestInfo = facebookService.getSignedRequestInfo(signedRequestParam);
FacebookClient facebookClient = new DefaultFacebookClient(signedRequestInfo.getOauthToken());
modelMap.addAttribute("signedRequestInfo", signedRequestInfo);
modelMap.addAttribute("signedRequestParam", signedRequestParam);
return "index";
}
@RequestMapping(value = "/photo", method = RequestMethod.POST)
public @ResponseBody Object post(
@RequestParam("signed_request") String signedRequestParam,
@RequestParam("test") String test,
@RequestParam("wallPhoto") MultipartFile multipartFile)throws Exception{
SignedRequestInfo signedRequestInfo = facebookService.getSignedRequestInfo(signedRequestParam);
FacebookClient facebookClient = new DefaultFacebookClient(signedRequestInfo.getOauthToken());
Parameter params = Parameter.with("name", test);
InputStream data = multipartFile.getInputStream();
BinaryAttachment binaryAttachment = BinaryAttachment.with("animal-land", data);
String publish = facebookClient.publish("/me/photos", String.class, binaryAttachment, params);
return publish;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment