Facebook SDK 4.x Android Share to Facebook

Ok I figured it out already, but I wanted to put it there in case anyone else runs into problems. Basically, I needed to host the post as a page owned by a Facebook user (i.e. I am John Doe and I am the administrator for the Rum Ham page, I want to publish it on the Rum Ham page).

+3


source to share


1 answer


So basically the answer looks like this

First you need to login with this line

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

      

Then you need to get the access token on the page we want to publish in

 Bundle params = new Bundle();
            //ok so access token here is "app_ID|app_secret"
            params.putString("access_token", accessToken);
            params.putString("fields", "id");
            GraphRequest request = new GraphRequest(null, "me", params, HttpMethod.GET, new GraphRequest.Callback() {
                public void onCompleted(GraphResponse response) {
                    FacebookRequestError error = response.getError();
                    if (error != null) {
                        Log.e("Error", error.getErrorMessage());
                    } else {
                        JSONObject values = response.getJSONObject();
                        try {
                            //so I have to get the user ID first
                            String id = values.getString("id");
                            Bundle p = new Bundle();
                            p.putString("access_token", accessToken);
                            //yay nest the graph requests
                            //once we get the id we can get their pages
                            GraphRequest pagesRequest = new GraphRequest(null, "/" + id + "/accounts", p, HttpMethod.GET, new GraphRequest.Callback() {
                                public void onCompleted(GraphResponse response) {
                                    FacebookRequestError error = response.getError();
                                    if (error != null) {
                                        Log.e("Error", error.getErrorMessage());
                                    } else {
                                        //ok so here, we're getting the pages back in a few JSON wrappers
                                        JSONObject values = response.getJSONObject();
                                        JSONArray array = null;
                                        try {
                                            array = values.getJSONArray("data");
                                         //ok, so here we're just iterating through the pages a user has, obviously you can handle this accordingly..                             
                                         for (int i = 0; i < array.length(); i++) {
                                        //ok, here how to actually get the token                                                
                                       String access_token = array.getJSONObject(i).getString("access_token")   



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

                                    }
                                }
                            });
                            GraphRequest.executeAndWait(pagesRequest);

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

                    }
                }
            });
            GraphRequest.executeAndWait(request);
        }

      



So, once we have the token access page, here is where the real f *** kery comes into play that Facebook refuses to tell you about it on their help pages. So, forget everything you read about having to submit your app for viewing. All I had to do was create a new access token, for example

//create a new access token, facebook refers to this as a page access token
AccessToken token = new AccessToken("page_access_token", AccessToken.getCurrentAccessToken().getUserId(), Arrays.asList("publish_actions", "manage_pages", "publish_pages"), null, AccessTokenSource.FACEBOOK_APPLICATION_SERVICE,
                AccessToken.getCurrentAccessToken().getExpires(), AccessToken.getCurrentAccessToken().getLastRefresh());
//then we simply update our current access token
        AccessToken.setCurrentAccessToken(token);

      

Now we're not done yet. Finally, we need to make an API call to create the message:

  Bundle params = new Bundle();
            params.putString("message", "Contents of message");
            //here, token is our newly created AccessToken object
            GraphRequest request = new GraphRequest(token, "/pageid/feed", params, HttpMethod.POST, new GraphRequest.Callback() {
                public void onCompleted(GraphResponse response) {
                    FacebookRequestError error = response.getError();
                    if (error != null) {
                        Log.e("Error", error.getErrorMessage());

                    } else {
                        //do your own processing here for success
                    }


                }
            });
            GraphRequest.executeAndWait(request);
        }
    }

      

And that's pretty much it. Hope this helps someone!

+5


source







All Articles