How to add Facebook App's accessToken to Graph

2019-07-07 19:54发布

问题:

This question already has an answer here:

  • Accessing Facebook Graph API without login 2 answers

I copied the code below from Facebook Graph Api console. However, Android Studio doesn't recognize accessToken.

I have alread created a Facebook App and I got its acesstoken. But I don't know where to add it in the code. If I just copy and paste. It shows this error

If I use AccessToken.getCurrentAccessToken() method, it will show this error:

errorMessage: An access token is required to request this resource

Here is my code:

GraphRequest request = GraphRequest.newGraphPathRequest(
               accessToken,
                "/ibm/events",
                new GraphRequest.Callback() {
                    @Override
                    public void onCompleted(GraphResponse response) {

                        Log.d(TAG, "onCompleted: " + response.toString());
                    }});

Bundle parameters = new Bundle();
        parameters.putString("fields", "name,cover,start_time,end_time,place,description");
        parameters.putString("limit", "3");
        request.setParameters(parameters);
        request.executeAsync();

    }

回答1:

Firstly :

Make a Facebook app with these simple steps I have written below:

  1. Go to Developer tab and click on it.
  2. Then go to Website Option.
  3. Enter the app name which you have want.
  4. Click on Create Facebook App. After this you have to choose category, you can choose App for Pages. Your AppId and Appkey is created automatically. The AppSecretKey is obfuscated. You can click on the show button to see your AppId and AppSecurityKey.

Then Check this link to generate access token

1.Copy and Paste Your App ID & Your App Secret into the generator below. You can get your App ID & your App Secret by clicking on the Apps main menu option and then choosing your newely created App.

  1. Next click the Tools Menu and then choose Graph API Exploer. page1 On this page you will click the Graph API Explorer select option.

  2. Then choose your Application from the list. In this example we'll use Custom Feed.

  3. Now you click on the Get Access Token. This will show a Select Permissions popup as you will see next.

  4. Next Click on the Extended Permissions menu option and click the checkbox read_stream and click Get Access Token. DO NOT CHECK read_stream if you are using our plugin to display a Page on facebook.

6.Then click the Okay button to continue.

Fetch Event IN Android : You have to download Android facebook sdk and put you access token there getCurrentAccessToken() in this method.

/* make the API call */
new GraphRequest(
    AccessToken.getCurrentAccessToken(),
    "/{event-id}",
    null,
    HttpMethod.GET,
    new GraphRequest.Callback() {
        public void onCompleted(GraphResponse response) {
            /* handle the result */
        }
    }
).executeAsync();

Event Details