3

我想使用 facebook sdk 3.5 来获取用户数据,并允许我的应用程序发布在用户墙上

我已经成功制作了一个简单的应用程序,该应用程序使用已在移动设备上安装的 fb 应用程序来获取活动会话(如果找到),但会话始终以默认权限打开,我想让它以发布权限打开以便能够在用户墙上发布

我总是得到(200#)用户没有验证这个应用程序

这是我的代码

public class FBSharingManager {

private static final String GRAPH_USER_LOADED = "graph_user_loaded";
private static final int REAUTH_ACTIVITY_CODE = 100;
private static final String[] PERMISSIONS = new String[] { "access_token",
        "status_update", "user_birthday", "publish_stream",
        "publish_actions", "email" };

private Activity mActivity;
private Session mSession;
private GraphUser mGraphUser;
private Bundle mSharingParameters;

public FBSharingManager(Activity activity) {

    this.mActivity = activity;
    loginAndGetSession();
}

public void synchPersonalProfileWithFaceBook() {

    ConnectivityManager connectivityManager = (ConnectivityManager) mActivity
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
    if (networkInfo.isConnected()) {

        if (mGraphUser == null) {

            getPersonalData();
        } else {

            broadcastGraphUser();
        }
    } else {

        Log.i("Myservices2", "network unavailable");
        String message = mActivity.getResources().getString(
                R.string.network_unavailable);

    }
}

private void loginAndGetSession() {

    Session.openActiveSession (mActivity, true,
            new Session.StatusCallback() {

                @Override
                public void call(final Session session, SessionState state,
                        Exception exception) {

                    if (session.isOpened()) {

                        mSession = session;
                        Session.setActiveSession(mSession);
                    }
                }
            });
}

private void requestPublishPermissions() {

    if (mSession != null && mSession.isOpened()) {

        Session.NewPermissionsRequest reauthRequest = new Session.NewPermissionsRequest(mActivity, PERMISSIONS).setRequestCode(REAUTH_ACTIVITY_CODE);
        reauthRequest.setLoginBehavior(SessionLoginBehavior.SUPPRESS_SSO);
        mSession.requestNewPublishPermissions(reauthRequest);
    }
}

private void getPersonalData() {

    if (mSession != null && mSession.isOpened()) {

        Request.newMeRequest(mSession, new Request.GraphUserCallback() {
            @Override
            public void onCompleted(GraphUser user, Response response) {
                if (user != null) {

                    mGraphUser = user;
                    broadcastGraphUser();
                }
            }
        }).executeAsync();
    }

}

private void broadcastGraphUser() {

    // mActivity.showDialog(((ProfileActivity)mActivity).LOADING_DIALOIG);
    Log.i("fb user name", mGraphUser.getFirstName());
    Toast.makeText(mActivity.getApplicationContext(),mGraphUser.getFirstName(), Toast.LENGTH_SHORT).show();
    Intent intent = new Intent(GRAPH_USER_LOADED);
    LocalBroadcastManager.getInstance(mActivity).sendBroadcast(intent);
    // mActivity.removeDialog(((ProfileActivity)mActivity).LOADING_DIALOIG);

}

public void shareOnFB(Bundle bundle) {

    ConnectivityManager connectivityManager = (ConnectivityManager) mActivity
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();

    if (networkInfo.isConnected()) {

        mSharingParameters = bundle;

        if (mSession != null && mSession.isOpened()) {

            requestPublishPermissions();
            postToWallWraper();
        }

    } else {

        String message = mActivity.getResources().getString(
                R.string.network_unavailable);
    }
}

private void postToWallWraper() {

    if (mSession != null && mSession.isOpened()){

        Request.Callback callback = new Request.Callback() {
            public void onCompleted(Response response) {

                FacebookRequestError error = response.getError();
                if (error != null) {
                    Toast.makeText(mActivity.getApplicationContext(),
                            error.getErrorMessage(), Toast.LENGTH_SHORT)
                            .show();
                } else {
                    Toast.makeText(mActivity.getApplicationContext(),
                            "Done", Toast.LENGTH_LONG).show();
                }
            }
        };

        Request request = new Request(mSession, "me/feed",
                mSharingParameters, HttpMethod.POST, callback);

        RequestAsyncTask task = new RequestAsyncTask(request);
        task.execute();
    }

}

public GraphUser getUser() {
    return mGraphUser;
}

public void setUser(GraphUser user) {
    this.mGraphUser = user;
}

public Session getSession() {
    return mSession;
}

public void setSession(Session mSession) {
    this.mSession = mSession;
}
}

这是活动代码

public class MainActivity extends FragmentActivity {

    private FBSharingManager mFBSharingManager;
    private TextView welcome;
    private Button getuserdata, publishtowall;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mFBSharingManager = new FBSharingManager(this);

        welcome = (TextView) findViewById(R.id.welcome);
        getuserdata = (Button) findViewById(R.id.getuserdata);
        publishtowall = (Button) findViewById(R.id.publishtowall);


        getuserdata.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                mFBSharingManager.synchPersonalProfileWithFaceBook();
            }
        });

        publishtowall.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Bundle postParams = new Bundle();
                postParams.putString("name", "Facebook SDK for Android");
                postParams.putString("caption", "Build great social apps and get more installs.");
                postParams.putString("description", "The Facebook SDK for Android makes it easier and faster to develop Facebook integrated Android apps.");
                postParams.putString("link", "https://developers.facebook.com/android");
                postParams.putString("picture", "https://raw.github.com/fbsamples/ios-3.x-howtos/master/Images/iossdk_logo.png");

                mFBSharingManager.shareOnFB(postParams);
            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        Session session = Session.getActiveSession();
        if (session != null) {
            session.onActivityResult(this, requestCode, resultCode, data);
            mFBSharingManager.setSession(session);
        }
    }

}

当我请求向我的会话添加权限时,它会返回到我的活动 onActivityResult 但它不会更改权限

请提供任何帮助

4

1 回答 1

1

请参阅 HelloFacebook 示例应用程序,了解如何正确请求发布权限和发布。

您的代码的问题是您正在请求发布权限(这是一个异步请求),然后立即发出发布请求,但是在发出发布请求时,您还没有收到来自发布权限的响应请求尚未,这就是帖子失败的原因。

另外,顺便说一句,您为什么在发出发布权限请求时禁止 SSO?

于 2013-09-18T17:57:41.317 回答