简体   繁体   中英

Apprequest of Android Facebook sdk 3.0

I am using the updated Facebook SDK V3.0 final for Android. I am trying to send app requests to facebook frineds who do not have the application. The code is:

public void fbookinvite(){

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

        Session session = Session.getActiveSession();

        postParams.putString("access_token", session.getAccessToken());

        if (session==null||session.getState().isClosed()){
            System.out.println("session is null");
        }else{
            //meaning we are good to go.
            Request request = new Request(session,"/friend facebook id/apprequests", postParams, HttpMethod.POST, new Request.Callback() {

                @Override
                public void onCompleted(Response response) {

                    System.out.println("response was: "+response.toString());
                }

            });

            Request.executeBatchAsync(request);
        }       
    }

When the code runs I get one of the following from the printed response:

responseCode: 400, graphObject: null, error: {HttpStatus: 400, errorCode: 2, errorType: OAuthException, errorMessage: (#2) Failed to create any app request}, isFromCache:false

... or:

Response:  responseCode: unknown, graphObject: null, error: {HttpStatus: -1, errorCode: -1, errorType: null, errorMessage: null}, isFromCache:false}

I have the following permissions: publist_stream and publish_actions .

After solving the problem I'd also like to know how to add a group of recipients to the app request.

I do not want to use the Facebook dialog.

Try this instead:

        Bundle params = new Bundle();   
        params.putString("message",
                "Learn how to make your Android apps social");
        params.putString("to", "YOUR_FRIEND_UID_HERE");
        WebDialog requestsDialog = (new WebDialog.RequestsDialogBuilder(
                this, Session.getActiveSession(), params))
                .setOnCompleteListener(new OnCompleteListener() {

                    @Override
                    public void onComplete(Bundle values,
                            FacebookException error) {
                        // your code here 
                    }

                }).build();
        requestsDialog.show();

This is the proper way to send an app request explicitly to someone. If you do not include the to parameter, the user can choose who they want to send to.

Response:  responseCode: unknown, graphObject: null, error: {HttpStatus: -1, errorCode: -1, errorType: null, errorMessage: null}, isFromCache:false}

This is a scenario usually related with running simultaneous FB requests on your own threads. I learnt the hard way to use either

Request.executeAsync()

or

RequestAsyncTask

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM