通过所有文章和指南,我似乎无法弄清楚如何通过Facebook进行身份验证,以便我可以从我们的网站发布到我们的Facebook页面。你能不能给我一些“如何为傻瓜做这个”?最重要的是我无法弄清楚如何获得acces_token
。问题是:
facebook application
?答案 0 :(得分:0)
是的,您必须创建一个Facebook应用程序,以便让您网站的用户在Facebook上发布他们的内容。
使用OAuth 2.0的身份验证过程非常简单:
https://graph.facebook.com/oauth/authorizeclient_id=<your app's client_id>&redirect_uri=<your redirect URU>
。如您所见,您必须传输您的client_id和一个URI,Facebook将在流程结束时重定向。您还可以传输数据参数,该参数将在整个过程中保持不变。用于跟踪用户。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>
代码参数由上一步调用重定向URI给出https://graph.facebook.com/me?access_token=<access_token>
,然后获得真正的 access_token 。此处有更多信息:
答案 1 :(得分:0)
您需要找到您的用户ID和访问令牌 opengraph explorer
以下Java代码(使用apache http client)在facebook墙上发布指定用户ID的消息。
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