Lusi
Lusi

Reputation: 411

Facebook Graph api notification send throws exception

I want to send notification from my web application to my facebook friends. I use graph api. This is my code but it throws exception java.lang.IllegalArgumentException: Parameter 'access_token' is reserved for RestFB use - you cannot specify it yourself.

    public void sendNotification(List<Long> mediaIds, String username){

    FacebookClient facebookClient = new DefaultFacebookClient(ACCESS_TOKEN);
    Connection<User> myFriends = facebookClient.fetchConnection(
            "me/friends", User.class);

    String app_access_token = facebookClient.obtainAppAccessToken(API_KEY, SECRET).getAccessToken();
    for (List<User> myFriend : myFriends)
        for (User user : myFriend) {
            for (int i = 0; i < mediaIds.size(); i++) {
                String link = "user/view_contest_media_fb.action?guest="
                    + username + "&contMediaId=" + mediaIds.get(i);
                facebookClient.publish(user.getId() + "/notifications",
                        FacebookType.class,
                        Parameter.with("template", "Notification text"),
                        Parameter.with("href", link),
                        Parameter.with("access_token", app_access_token));
            }
        }

}

Upvotes: 0

Views: 1287

Answers (3)

Adam B
Adam B

Reputation: 1774

I ran into this problem trying to subscribe to Page webhooks. I ended up using the BatchRequestBuilder to specify the request body.

BatchRequest postRequest = new BatchRequest.BatchRequestBuilder(pageId + "/subscribed_apps")
                    .method("POST")
                    .body(Parameter.with("access_token","the token")).build();

List<BatchResponse> batchResponses = fb.executeBatch(postRequest);

Upvotes: 0

Deepak
Deepak

Reputation: 41

Don't add access_token as parameter, change it like this:

facebookClient.publish(user.getId() + "/notifications**?access_token="+ACCESS_TOKEN**,
                    FacebookType.class,
                    Parameter.with("template", "Notification text"),
                    Parameter.with("href", link));

Upvotes: 4

AboQutiesh
AboQutiesh

Reputation: 1716

just remove the access token parameter from your code

comment this line //Parameter.with("access_token", app_access_token)); and try again

Upvotes: 0

Related Questions