Posting to a Facebook page via OpenGraph
through all the articles and guides, I can't seem to figure out how to authenticate with Facebook so that I could post from our website to our Facebook page. Can you please give me some sort of "how to do exactly this for dummies"? The main thing is that I can't figure out how to get that acces_token
. The questions are:
- do 开发者_如何学JAVAI need to create some sort of
facebook application
first? - will the app post to the page's wall as a page admin would, or as an application, in other words - how to do it as a page admin?
Yes, you have to create a Facebook application in order to let the users of your website post their stuff on Facebook.
The authenticate process, using OAuth 2.0, is quite simple:
- call
https://graph.facebook.com/oauth/authorizeclient_id=<your app's client_id>&redirect_uri=<your redirect URU>
. As you can see, you have to transmit your client_id and an URI where Facebook will redirect at the end of the process. You can also transmit a data parameter, which will go unchanged through the process. Useful to keep track of the user. - call
https://graph.facebook.com/oauth/access_token?client_id=<your app's client_id>&redirect_uri=h<your redirect URU>&client_secret=<your app's client_secret>&code=<code>
The code parameter is given by the call to the redirect URI on the previous step - call
https://graph.facebook.com/me?access_token=<access_token>
, using the ad-hoc access_token returned by the previous redirect, and then you get the real access_token.
More infos here:
http://developers.facebook.com/docs/authentication/
You need to find out your user id and an access token from the opengraph explorer
The following Java code (using apache http client), publishes a message on the facebook wall of the user id specified.
public class Main2 {
public static void main(String[] args) {
HttpClient httpclient = new DefaultHttpClient();
try {
String accessToken = "AAACEdEose0cBANzDaBq";
String message = "Hey Jude, don't make it bad";
String userId = "200501511023";
String requestURL = "https://graph.facebook.com/"+userId+"/feed";
HttpPost httpPost = new HttpPost( requestURL );
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("access_token", accessToken));
nameValuePairs.add(new BasicNameValuePair("message", message));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Create a response handler
ResponseHandler<String> rh = new ResponseHandler<String>() {
public String handleResponse(HttpResponse hr) throws ClientProtocolException, IOException {
return "\n" + hr.getStatusLine() + "\n\n"
+ dumpStream(hr.getEntity().getContent());
}
};
System.out.println("****************************************");
System.out.println("executing request " + httpPost.getURI());
System.out.println("****************************************");
String response = httpclient.execute(httpPost, rh);
System.out.println("----------------------------------------");
System.out.println(response);
System.out.println("----------------------------------------");
} catch (IOException e) {
e.printStackTrace();
}
}
public static String dumpStream(InputStream is) {
try {
byte[] theBytes = new byte[is.available()];
is.read(theBytes, 0, is.available());
is.close();
return new String(theBytes);
} catch (IOException ex) {
}
return null;
} // ()
} // class
精彩评论