首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将facebook与java桌面应用程序连接起来

如何将facebook与java桌面应用程序连接起来
EN

Stack Overflow用户
提问于 2011-02-03 15:42:46
回答 3查看 9.8K关注 0票数 5

我需要连接和认证用户从java桌面应用程序,我已经尝试facebook-java-api使用facebookjsonclient和facebookRestclient,但无法获得会话密钥。在facebook中有没有什么变化导致我们无法连接,或者有没有其他最好的java api或者如何连接的例子。我的代码是

代码语言:javascript
复制
private static void getUserID(String email, String password){
        String session = null;
        try {

            HttpClient http = new HttpClient();

            http.getHostConfiguration().setHost("www.facebook.com");
            String api_key = "key";
            String secret = "sec";
            FacebookJaxbRestClient client = new FacebookJaxbRestClient(api_key, secret);
                System.out.println("====>"+client.isDesktop());

            String token = client.auth_createToken();
            System.out.println(" :::::::"+token);
            System.out.println(" :::::::::: "+token);
            PostMethod post = new PostMethod("/login.php?");

            post.addParameter("api_key", api_key);


            post.addParameter("email", email);
            post.addParameter("pass", password);


            int postStatus = http.executeMethod(post);
                System.out.println("url : " + post.getURI());
            System.out.println("Response : " + postStatus);
            for (Header h : post.getResponseHeaders()) {
                System.out.println(h);
            }
            session = client.auth_getSession(token); // Here I am getting error
            System.out.println("Session string: " + session);
            long userid = client.users_getLoggedInUser();
            //System.out.println("User Id is : " + userid);*/
        } catch (FacebookException fe) {

            fe.printStackTrace();

        }catch(Exception e){
            e.printStackTrace();
        }
    }
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-03-18 10:36:01

首先,您需要获取access_token,然后我建议您使用restfb库。为了获得令牌,我建议阅读以下内容:https://developers.facebook.com/docs/authentication/

一个简单的总结:

  1. HTTP GET:https://www.facebook.com/dialog/oauth?client_id=YOUR_APP_ID&redirect_uri=YOUR_URL&scope=email,response_type=token
  2. Use read_stream&你从上面得到的代码,然后执行:https://graph.facebook.com/oauth/access_token?从restfb client_secret=YOUR_APP_SECRET&code=THE_CODE_FROM_ABOVE
  3. Exact client_id=YOUR_APP_ID&redirect_uri=YOUR_URL& access_token并使用FacebookClient发出接口请求。
票数 -1
EN

Stack Overflow用户

发布于 2012-05-03 17:15:10

AFAIK,到目前为止,还没有办法直接从“桌面应用”连接到facebook。您可以使用apache http客户端库来模拟浏览器并完成它。但是不能保证它总是能工作,我也曾经尝试过用一些库来做这件事,但它们似乎已经失败了。

票数 0
EN

Stack Overflow用户

发布于 2012-08-14 08:16:06

我在这方面取得了一些成功。我的方法是使用嵌入式浏览器向用户显示身份验证。Facebook处理身份验证,并将您重定向到“登录成功”页面,并将访问令牌和过期时间作为GET数据附加到URL上。下面的大部分代码用于使用org.eclipse.swt库创建和显示浏览器。

代码语言:javascript
复制
private static final String APP_ID = "###########";
private static final String PERMISSIONS =
        "COMMA SEPARATED LIST OF REQUESTED PERMISSIONS";
private String access_token;
private long expirationTimeMillis;
/**
 * Implements facebook's authentication flow to obtain an access token. This
 * method displays an embedded browser and defers to facebook to obtain the
 * user's credentials.
 * According to facebook, the request as we make it here should return a
 * token that is valid for 60 days. That means this method should be called
 * once every sixty days.
 */
private void authenticationFlow() {
    Display display = new Display();
    Shell shell = new Shell(display);
    final Browser browser;
    GridLayout gridLayout = new GridLayout();
    gridLayout.numColumns = 3;
    shell.setLayout(gridLayout);

    try {
        browser = new Browser(shell, SWT.NONE);
    } catch (SWTError e){
        System.err.println("Could not instantiate Browser: " + e.getMessage());
        display.dispose();
        display = null;
        return;
    }
    browser.setJavascriptEnabled(true);

    GridData data = new GridData();
    data.horizontalAlignment = GridData.FILL;
    data.verticalAlignment = GridData.FILL;
    data.horizontalSpan = 3;
    data.grabExcessHorizontalSpace = true;
    data.grabExcessVerticalSpace = true;
    browser.setLayoutData(data);

    final ProgressBar progressBar = new ProgressBar(shell, SWT.MOZILLA);
    data = new GridData();
    data.horizontalAlignment = GridData.END;
    progressBar.setLayoutData(data);

    /* Event Handling */
    browser.addProgressListener(new ProgressListener(){
        public void changed(ProgressEvent event){
            if(event.total == 0) return;
            int ratio = event.current * 100 / event.total;
            progressBar.setSelection(ratio);
        }
        public void completed(ProgressEvent event) {
            progressBar.setSelection(0);
        }
    });
    browser.addLocationListener(new LocationListener(){
        public void changed(LocationEvent e){
            // Grab the token if the browser has been redirected to
            // the login_success page
            String s = e.location;
            String token_identifier = "access_token=";
            if(s.contains("https://www.facebook.com/connect/login_success.html#access_token=")){
                access_token = s.substring(s.lastIndexOf(token_identifier)+token_identifier.length(),s.lastIndexOf('&'));
                String expires_in = s.substring(s.lastIndexOf('=')+1);
                expirationTimeMillis = System.currentTimeMillis() + (Integer.parseInt(expires_in) * 1000);
            }
        }
        public void changing(LocationEvent e){}
    });

    if(display != null){
        shell.open();
        browser.setUrl("https://www.facebook.com/dialog/oauth?"
                + "client_id=" + APP_ID
                + "&redirect_uri=https://www.facebook.com/connect/login_success.html"
                + "&scope=" + PERMISSIONS
                + "&response_type=token");
        while(!shell.isDisposed()) {
            if(!display.readAndDispatch()){
                display.sleep();
                if(access_token != null && !access_token.isEmpty()){
                    try{ Thread.sleep(3000);}catch(Exception e){}
                    shell.dispose();
                }
            }
        }
        display.dispose();
    }
}

因此,您所要做的就是找出您的应用程序需要拥有哪些权限才能工作。请注意,“第二个对话”权限可以由用户选择,因此不能保证您确实拥有这些权限。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4883549

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档