首页 文章

通过OpenGraph发布到Facebook页面

提问于
浏览
1

通过所有的文章和指南,我可以'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 2751023 ? The main thing is that I can'弄清楚如何获得 acces_token . 问题是:

  • 我需要先创建某种 facebook application 吗?

  • 应用程序会以页面管理员的身份发布到页面的墙上,还是作为应用程序,换句话说 - 如何以页面管理员的身份进行操作?

2 回答

  • 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将在流程结束时重定向 . 您还可以传输数据参数,该参数将在整个过程中保持不变 . 用于跟踪用户 .

    • 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> 代码参数由上一步调用重定向URI给出

    • 调用 https://graph.facebook.com/me?access_token=<access_token> ,使用前一个重定向返回的ad-hoc access_token,然后得到真正的 access_token .

    更多信息:

    http://developers.facebook.com/docs/authentication/

  • 0

    您需要从opengraph explorer找出您的用户ID和访问令牌

    以下Java代码(使用apache http client)在facebook墙上发布指定用户标识的消息 .

    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
    

相关问题