facebook checkin权限错误

时间:2014-04-23 11:09:10

标签: android facebook facebook-graph-api facebook-android-sdk android-facebook

在我的应用程序中,我正在尝试检查具体位置。

但每次我都有这个错误

{Response:  responseCode: 403, graphObject: null, error: {HttpStatus: 403, errorCode: 200, errorType: OAuthException, errorMessage: (#200) Requires extended permission: publish_actions}, isFromCache:false}

我确信我在我的权限列表中添加了publish_actions权限。

我在这里做错了什么?为什么我每次都有这样的错误?

这是我的代码:

 private Session.StatusCallback statusCallback = new SessionStatusCallback();
    private class SessionStatusCallback implements Session.StatusCallback {
        @Override
        public void call(Session session, SessionState state, Exception exception) {
            if (session.isOpened()){
                checkIn(session);
                PrefsCacheManager.getInstance().putInCashe("accessToken" , session.getAccessToken());
            }
        }
    }

    private UiLifecycleHelper uiHelper;
    private LocationManager locationManager;
    private Location lastKnownLocation;
    private UiLifecycleHelper lifecycleHelper;
    private Location pickPlaceForLocationWhenSessionOpened = null;
    private static final int PLACE_ACTIVITY = 1;
    private String mPlaceId;
    private static final Location LOCATION = new Location("") {{}};

    private Session.StatusCallback callback = new Session.StatusCallback() {
        @Override
        public void call(Session session, SessionState state,
                         Exception exception) {
        }
    };

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        uiHelper = new UiLifecycleHelper(this, callback);
        uiHelper.onCreate(savedInstanceState);
        lifecycleHelper = new UiLifecycleHelper(this, new Session.StatusCallback() {
            @Override
            public void call(Session session, SessionState state, Exception exception) {
                onSessionStateChanged(session, state, exception);
            }
        });
        lifecycleHelper.onCreate(savedInstanceState);
        ensureOpenSession();
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        init();
    }

    @Override
    public void onStart() {
        super.onStart();
        mTracker.activityStart(this);
    }

    @Override
    public void onStop() {
        super.onStop();
        mTracker.activityStop(this);
    }

    private boolean ensureOpenSession() {
        if (Session.getActiveSession() == null ||
                !Session.getActiveSession().isOpened()) {
            Session.openActiveSession(this, true, new Session.StatusCallback() {
                @Override
                public void call(Session session, SessionState state, Exception exception) {
                    onSessionStateChanged(session, state, exception);
                }
            });
            return false;
        }
        return true;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        uiHelper.onActivityResult(requestCode, resultCode, data, new FacebookDialog.Callback() {
            @Override
            public void onError(FacebookDialog.PendingCall pendingCall, Exception error, Bundle data) {
                Toast.makeText(FacebookCheckIn.this, getString(R.string.fb_share_failed), Toast.LENGTH_LONG).show();
            }

            @Override
            public void onComplete(FacebookDialog.PendingCall pendingCall, Bundle data) {
            }
        });
        lifecycleHelper.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PLACE_ACTIVITY && resultCode == Activity.RESULT_OK){
            displaySelectedPlace(resultCode);
        } else if (requestCode == PLACE_ACTIVITY && resultCode == Activity.RESULT_CANCELED){
            finish();
        }
    }

    private void displaySelectedPlace(int resultCode) {
        String results = "";
        SmartBonusApplication application = (SmartBonusApplication) getApplication();

        GraphPlace selection = application.getSelectedPlace();
        if (selection != null) {
            GraphLocation location = selection.getLocation();
            mPlaceId = selection.getId();
            checkFacebookSession();

        }
    }

    private void setLocation() {
        try {
            LOCATION.setLatitude(mBrench.getLat());
            LOCATION.setLongitude(mBrench.getLng());
            startPickPlaceActivity(LOCATION);
        } catch (Exception ex) {
            onError(ex);
        }
    }
    private void onError(Exception exception) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Error").setMessage(exception.getMessage()).setPositiveButton("OK", null);
        builder.show();
    }

    private void checkFacebookSession() {
        final  String[] PERMISSION_ARRAY_READ = {"publish_actions" , "user_checkins"};
        final List<String> permissionList = Arrays.asList(PERMISSION_ARRAY_READ);
        new Session.NewPermissionsRequest(FacebookCheckIn.this, permissionList);
        Session session = Session.getActiveSession();
        if (session != null){
            if (!session.isOpened() && !session.isClosed()) {
                session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback));
            }else {
                Session.openActiveSession(this, true, statusCallback);
            }
        } else {
            Session.openActiveSession(this, true, statusCallback);
        }
    }

    private void checkIn(final Session pSession) {
        Bundle params = new Bundle();
        if (pSession.getAccessToken() != null) {
            params.putString("place", String.valueOf(mPlaceId));
            params.putString("message", mBrench.getCompany() + " " + mBrench.getAddress());
            params.putString("coordinates", "{\"longitude\":" + mBrench.getLng() + ",\"latitude\":" + mBrench.getLat() + "}");
            Request.Callback callback6 = new Request.Callback() {
                public void onCompleted(Response response) {
                    checkInSuccess();
                }
            };
            Request request6 = new Request(pSession, "me/checkins", params, HttpMethod.POST, callback6);
            RequestAsyncTask task6 = new RequestAsyncTask(request6);
            task6.execute();
        }
    }

    private void checkInSuccess() {
        Intent intent = new Intent(FacebookCheckIn.this, CheckinSuccessActivity.class);
        intent.putExtra("branche", mBrench);
        intent.putExtra("type", VKONTAKTE);
        startActivity(intent);
        finish();
        overridePendingTransition(0, 0);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
        overridePendingTransition(0, 0);
    }

    private void init() {
        mTracker = EasyTracker.getInstance(this);
        mBrench = getIntent().getParcelableExtra("branche");
        setLocation();
    }

    private void onSessionStateChanged(Session session, SessionState state, Exception exception) {
        if (pickPlaceForLocationWhenSessionOpened != null && state.isOpened()) {
            Location location = pickPlaceForLocationWhenSessionOpened;
            pickPlaceForLocationWhenSessionOpened = null;
            startPickPlaceActivity(location);
        }
    }

    private void startPickPlaceActivity(Location location) {
        if (ensureOpenSession()) {
            SmartBonusApplication application = (SmartBonusApplication) getApplication();
            application.setSelectedPlace(null);
            Intent intent = new Intent(this, PickPlaceActivity.class);
            PickPlaceActivity.populateParameters(intent, location, null);
            startActivityForResult(intent, PLACE_ACTIVITY);
        } else {
            pickPlaceForLocationWhenSessionOpened = location;
        }
    }
}

2 个答案:

答案 0 :(得分:0)

您必须稍后添加权限。并且由于会话已经创建,因此不会要求publish_action权限。

<击>

所以,通过登出facebook然后再次登录来销毁会话;或者从settings删除该应用程序,然后尝试。这样,它将要求发布权限,您将能够成功发布!


修改

现在不推荐使用/ checkins发布。检查 - /{user-id}/checkins

您现在必须使用Open Graph发布带位置的故事。

答案 1 :(得分:0)

你必须使用

POST /{user_id}/feed

如此处所述:https://developers.facebook.com/docs/graph-api/reference/post#publishing

在那里你可以添加place_id作为参数。