【问题标题】:How to connect to facebook from my application如何从我的应用程序连接到 Facebook
【发布时间】:2014-08-02 02:25:04
【问题描述】:

我正在开发 android 应用程序,我希望用户在第一次使用 Facebook SDK 打开应用程序时连接 Facebook。 然后我想从我的应用程序中发布特定消息到 Facebook 墙上。我尝试将 Facebook sdk 与我的应用程序一起使用。我已经在 mp 应用程序和 Facebook sdk 之间进行了集成,但我不知道如何执行登录 Facebook 并使用特定消息发布到墙上的任务。我在 stackoverflow 中搜索这项任务,我找到了这段代码,但我无法理解 它不适合我

public class MainActivity extends Activity {

    Facebook facebookClient;    
    SharedPreferences mPrefs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        facebookClient=new Facebook("fb_App_id");
          ImageButton facebookButton = (ImageButton) findViewById(R.id.button_FacebookShare);
            facebookButton.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    loginToFacebook();

                    if (facebookClient.isSessionValid()) {
                        postToWall();
                    }
                }
            });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        facebookClient.authorizeCallback(requestCode, resultCode, data);
    }

    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) {
            facebookClient.setAccessToken(access_token);
        }

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

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

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

                @Override
                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", facebookClient.getAccessToken());
                    editor.putLong("access_expires", facebookClient.getAccessExpires());
                    editor.commit();

                    postToWall();
                }

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

                }

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

                }

            });
        }
    }

    private void postToWall() {
        Bundle parameters = new Bundle();
        parameters.putString("name", "Battery Monitor");
        parameters.putString("link", "https://play.google.com/store/apps/details?id=com.ck.batterymonitor");
        parameters.putString("picture", "link to the picture");
        parameters.putString("display", "page");
        // parameters.putString("app_id", "228476323938322");

        facebookClient.dialog(MainActivity.this, "feed", parameters, new DialogListener() {

            @Override
            public void onFacebookError(FacebookError e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onError(DialogError e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onComplete(Bundle values) {
                // TODO Auto-generated method stub
            }

            @Override
            public void onCancel() {
                // TODO Auto-generated method stub
            }
        });
    }
}

【问题讨论】:

标签: android


【解决方案1】:

您应该参考以下关于 SO 的问题。
Facebook login and post
此代码有效。一定能解决你的问题。

【讨论】:

  • 我使用此链接中的代码并添加类,但是当我在模拟器中运行时,我得到以下无法找到 com.facebook.katana.provider.attributionidprovider 的提供者信息。然后我想知道在我的应用中将 app_id 和 key hash 放在哪里
【解决方案2】:

尝试使用 Android Simple Facebook (https://github.com/sromku/android-simple-facebook) - 这是一个让 Facebook SDK 的使用更容易的层。根据文档,登录过程是这样的:

初始化回调监听器:

OnLoginListener onLoginListener = new OnLoginListener() {
    @Override
    public void onLogin() {
        // change the state of the button or do whatever you want
        Log.i(TAG, "Logged in");
    }

    @Override
    public void onNotAcceptingPermissions(Permission.Type type) {
        // user didn't accept READ or WRITE permission
        Log.w(TAG, String.format("You didn't accept %s permissions", type.name()));
    }

    /* 
     * You can override other methods here: 
     * onThinking(), onFail(String reason), onException(Throwable throwable)
     */ 
};

登录:

mSimpleFacebook.login(onLoginListener);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-24
    • 1970-01-01
    • 2019-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多