Reputation: 1794
I'm trying to create a simple facebook app which can show user albums.
I've created a simple java application deployed it on local glassfish instance and entered this localhost url in newly created facebook application settings.
But to use facebook api on server side I have to specify user Access Token. Which if I'm wright, should be generated on client side.
For this purpose I'm using javascript api on the first page of my app to pass this access token as one of request params:
$(document).ready(function () {
FB.init({
appId: '*****************',
cookie: true,
xfbml: true,
status: true });
FB.getLoginStatus(function (response) {
if (response.authResponse) {
$('#buttonRedirectToAlbums').onclick(new function () {
window.location.replace("<c:url value="/helloController/testfacebookapi"/>?access_token=" + response.authResponse.accessToken);
})
}
});
});
The problem is that in getLoginStatus I get response object without any data except the authorized field which is false.
So I have two questions:
1) Is it a correct approach for developing a facebook app? Or am I using their api in wrong way?
2) How to "authorize" a user and is it actually required? Maybe I'm missing some permissions stuff...
Upvotes: 0
Views: 42
Reputation: 3674
Yes, it is necessary for a user to authenticate you application.
Regarding the approach, I would suggest you to go though this document. I think you are missing the of checking the status of the response. The document will tell you the correct approach for handling 3 different cases related to a user's status with respect to your app.
In your code, you're missing the part to handle the case where the user is not connected to your app or is logged out. You can modify your code to make it something like:
$(document).ready(function () {
FB.init({
appId: '*****************',
cookie: true,
xfbml: true,
status: true });
FB.getLoginStatus(function (response) {
if (response.authResponse) {
$('#buttonRedirectToAlbums').onclick(new function () {
window.location.replace("<c:url value="/helloController/testfacebookapi"/>?access_token=" + response.authResponse.accessToken);
})
}
else{
//Ask user to login or allow your App to access data.
}
});
});
For further reference on how actually to implement it, you can refer this example.
Upvotes: 1