How do I publish a check in to facebook with andro

2019-04-14 15:07发布

问题:

The docs are terrible. I want to publish a users check in to facebook. According to the docs creating a checkin object is deprecated

https://developers.facebook.com/docs/reference/api/checkin

and instead you're supposed to add a post with location data attached. So that's what I'm trying to do. Or maybe i'm supposed to try to publish and open graph story?

Anyways here's what I have, it's basically the code to publish a post that is in their SDK sample, the post is created but there is no location data attached.

 private void publishStory() {

    Session session = Session.getActiveSession();

    if (session != null) {
        Bundle placeBundle = new Bundle();
        Bundle locationBundle = new Bundle();
        Bundle postParams = new Bundle();

        locationBundle.putString("latitude",String.valueOf(place.getLat()));
        locationBundle.putString("longitude",String.valueOf(place.getLng()));

        placeBundle.putString("id", place.getPage_id());
        placeBundle.putString("name", place.getName());
        placeBundle.putBundle("location", locationBundle);

        postParams.putBundle("place", placeBundle);
        postParams.putString("message", "test message");

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

                String postId = null;

                try {

                JSONObject graphResponse = response.getGraphObject().getInnerJSONObject();


                    postId = graphResponse.getString("id");
                }
                catch (JSONException e) {
                    Log.i(app.TAG, "JSON error " + e.getMessage());
                }
                catch(Exception e){
                    e.printStackTrace();
                }
                FacebookRequestError error = response.getError();
                if (error != null) {
                    Toast.makeText(mContext, error.getErrorMessage(), Toast.LENGTH_SHORT).show();
                }
                else {
                    Toast.makeText(mContext, postId, Toast.LENGTH_LONG).show();
                }
            }
        };

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

        RequestAsyncTask task = new RequestAsyncTask(request);
        task.execute();
    }
    else {
        toast("no session to publish");
    }

}

the session does have publish permissions and it WILL publish a post but all that is there is the "test message" string. The place object is from facebook's servers so it is an actual place with a page_id. When i'm debugging the post params look something like this

 Bundle[{message=test message, place=Bundle[{id=171229079554355, location=Bundle[{longitude=-122.434568, latitude=37.797314}], name=The Brixton San Francisco}]}]

回答1:

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

        // callback when session changes state
        @Override
        public void call(Session session, SessionState state,
                Exception exception) {
            if (session != null && session.isOpened()) {

                // Check for publish permissions
                List<String> permissions = session.getPermissions();
                if (!isSubsetOf(PERMISSIONS, permissions)) {
                    Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(
                            activity, PERMISSIONS);
                    session.requestNewPublishPermissions(newPermissionsRequest);
                    return;
                }

                Bundle postParams = new Bundle();
                postParams.putString("message", message);

                    postParams.putString("tags",tag);
                         postParams.putString("place",place_id);



                Request.Callback callback = new Request.Callback() {
                    private String toastmessage;

                    public void onCompleted(Response response) {
                        try {
                            JSONObject graphResponse = response
                                    .getGraphObject().getInnerJSONObject();
                            String postId = null;

                            postId = graphResponse.getString("id");
                        } catch (Exception e) {
                            Log.i("Test", "JSON error " + e.getMessage());
                        }
                        FacebookRequestError error = response.getError();
                        if (error != null) {
                            isPosted(false);
                            Toast.makeText(
                                    activity.getApplicationContext(),
                                    error.getErrorMessage(),
                                    Toast.LENGTH_SHORT).show();
                        } else {
                            isPosted(true);
                            toastmessage = "Posted Successfully";
                            Toast.makeText(activity, toastmessage,
                                    Toast.LENGTH_SHORT).show();
                            {

                            }
                        }
                    }
                };

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

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


        }
    });