问题
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:
- Go to Developer tab and click on it.
- Then go to Website Option.
- Enter the app name which you have want.
- 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.
Next click the Tools Menu and then choose Graph API Exploer. page1 On this page you will click the Graph API Explorer select option.
Then choose your Application from the list. In this example we'll use Custom Feed.
Now you click on the Get Access Token. This will show a Select Permissions popup as you will see next.
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
来源:https://stackoverflow.com/questions/43382064/how-to-add-facebook-apps-accesstoken-to-graphrequest-newgraphpathrequest-method