代表用户从 Android 游戏在 Facebook 上自动 post

Auto post on Facebook from Android game on user behalf

我使用 Processing 为 android 开发了一款基于物理的 (Box2d) 游戏,我想为其添加分数共享选项,以便用户可以在他们的 Facebook 时间线上分享 his/her 最佳分数赛后。我在 Eclipse 中设置了 Facebook SDK。我在互联网上搜索并找到了这个解决方案:

public class FacebookConnector {
        private static final String APP_ID = "*************";
        private Facebook facebook;
        private AsyncFacebookRunner mAsyncRunner;
        String FILENAME = "AndroidSSO_data";
        SharedPreferences mPrefs;

        public FacebookConnector() {
            facebook = new Facebook(APP_ID);
            mAsyncRunner = new AsyncFacebookRunner(facebook);
        }

        public void loginToFacebook() {
            mPrefs = getPreferences(MODE_PRIVATE);
            String access_token = mPrefs.getString("access_token", null);
            long expires = mPrefs.getLong("access_expires", 0);

            if (access_token != null) {
                facebook.setAccessToken(access_token);
            }

            if (expires != 0) {
                facebook.setAccessExpires(expires);
            }

            if (!facebook.isSessionValid()) {
                facebook.authorize(LocalPakistaniGames.this,
                        new String[] { "email",
                        "publish_stream" }, new DialogListener() {

                    public void onCancel() {
                        // Function to handle cancel event
                    }

                    public void onComplete(Bundle values) {
                        // Function to handle complete event
                        // Edit Preferences and update facebook acess_token
                        SharedPreferences.Editor editor = mPrefs.edit();
                        editor.putString("access_token",
                                facebook.getAccessToken());
                        editor.putLong("access_expires",
                                facebook.getAccessExpires());
                        editor.commit();
                    }

                    public void onError(DialogError error) {
                        // Function to handle error

                    }

                    public void onFacebookError(FacebookError fberror) {
                        // Function to handle Facebook errors

                    }

                });
            }
        }

        public void logoutFromFacebook() {
            mAsyncRunner.logout(LocalPakistaniGames.this,
                    new RequestListener() {
                @Override
                public void onComplete(String response, Object state) {
                    Log.d("Logout from Facebook", response);
                    if (Boolean.parseBoolean(response) == true) {
                        // User successfully Logged out
                    }
                }

                @Override
                public void onIOException(IOException e, Object state) {
                }

                @Override
                public void onFileNotFoundException(FileNotFoundException e,
                        Object state) {
                }

                @Override
                public void onMalformedURLException(MalformedURLException e,
                        Object state) {
                }

                @Override
                public void onFacebookError(FacebookError e, Object state) {
                }
            });
        }

        public void getProfileInformation() {
            mAsyncRunner.request("me", new RequestListener() {
                public void onComplete(String response, Object state) {
                    Log.d("Profile", response);
                    String json = response;
                    try {
                        JSONObject profile = new JSONObject(json);
                        // getting name of the user
                        final String name = profile.getString("name");
                        // getting email of the user
                        final String email = profile.getString("email");

                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                Toast.makeText(getApplicationContext(),
                                        "Name: " + name + "\nEmail: " + email,
                                        Toast.LENGTH_LONG).show();
                            }

                        });

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

                public void onIOException1(IOException e, Object state) {
                }

                public void onFileNotFoundException1(FileNotFoundException e,
                        Object state) {
                }

                public void onMalformedURLException1(MalformedURLException e,
                        Object state) {
                }

                @Override
                public void onFacebookError(FacebookError e, Object state) {
                }

                @Override
                public void onIOException(IOException e, Object state) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onFileNotFoundException(FileNotFoundException e,
                        Object state) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onMalformedURLException(MalformedURLException e,
                        Object state) {
                    // TODO Auto-generated method stub

                }
            });
        }

        public void postToWall(int level, int score) {
            // post on user's wall.
            String msg = "I just made new best score in Level " + level
                    + ". My new Best Score is " + score + ". Beat my score!";
            final Bundle parameters = new Bundle();
            parameters.putString("description", msg);
            parameters.putString("picture", "http://i57.tinypic.com/fui2o.png");
            parameters.putString("link",
                    "https://www.facebook.com/LocalPakistaniGamesAndroid");
            parameters.putString("name", "Local Pakistani Games");
            parameters.putString("caption",
                    "Share this. Be a part of preserving Pakistani culture.");
            LocalPakistaniGames.this.runOnUiThread(new Runnable() {
                public void run() {
                    facebook.dialog(LocalPakistaniGames.this, "feed",
                            parameters, new DialogListener() {

                                @Override
                                public void onComplete(Bundle values) {
                                    // TODO Auto-generated method stub
                                    if (values != null) {
                                    Toast.makeText(LocalPakistaniGames.this,
                                            "Shared successfully on your timeline!",
                                            Toast.LENGTH_SHORT).show();
                                    } else {
                                        Toast.makeText(
                                                LocalPakistaniGames.this,
                                                "Share cancelled!",
                                                Toast.LENGTH_SHORT).show();
                                    }
                                }

                                @Override
                                public void onFacebookError(FacebookError e) {
                                    // TODO Auto-generated method stub
                                    Toast.makeText(LocalPakistaniGames.this,
                                            "Facebook Error!",
                                            Toast.LENGTH_SHORT)
                                            .show();
                                }

                                @Override
                                public void onError(DialogError e) {
                                    // TODO Auto-generated method stub
                                    Toast.makeText(LocalPakistaniGames.this,
                                            "Error!", Toast.LENGTH_SHORT)
                                            .show();
                                }

                                @Override
                                public void onCancel() {
                                    // TODO Auto-generated method stub
                                    Toast.makeText(LocalPakistaniGames.this,
                                            "Share cancelled!",
                                            Toast.LENGTH_SHORT).show();
                                }
                            });
                }
            });
        }
    }

它工作正常,并提供了一个预填充的对话框,用户可以在其中共享或关闭对话框。我已经检查过它在 Facebook 时间线上正确共享。但问题是它没有使用设备上安装的 Facebook 应用程序。它在我的设备上使用 chrome 登录 Facebook。 有没有办法强制它使用 Facebook 应用程序 android 而不是去 chrome (或任何其他浏览器)??

你使用Facebook SDK有很多问题,但我会直接跳到你的问题。

您正在使用 "feed" 对话框进行共享。这是一个网络对话框,这就是它弹出 WebView(而不是实际的浏览器应用程序)的原因。您也不会将任何会话或访问令牌传递给提要对话框,这就是用户需要先登录才能共享的原因。

如果您想使用 Facebook 应用程序进行分享,我会推荐此文档:https://developers.facebook.com/docs/android/share

如果您想正确使用 Facebook SDK(而不是旧的已弃用的 SDK),请从这里开始:https://developers.facebook.com/docs/android/getting-started/

Here 是您问题的解决方案。如果你想在facebook wall 上自动发布,你必须使用graph api。 此外,您要做的第一件事是使用 facebook 登录,然后,您必须再次请求权限。 如果您使用 LoginManager:

LoginManager.getInstance().logInWithPublishPermissions(this, Arrays.asList("publish_actions"));

这行代码授予您的应用代表用户发布内容的权限