Reputation: 11555
I want to create simple Google Chrome extention which would display News from Facebook.
News feed can be retrieved by calling this:
https://graph.facebook.com/me/home?access_token=...
But I need access_token.
Could someone give a hint how to define a JavaScript function which would return access_token when given YOUR_APP_ID. This function also should work inside Chrome Extention.
Other examples on the internet did not work, because:
EDIT1:
I've tried this:
function getAccessToken2(YOUR_APP_ID) {
alert("before FB.init");
FB.init({
appId : YOUR_APP_ID ,
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
alert("before FB.login");
FB.login( function (response) {
alert("response");
if ( response.authResponse ) {
alert("response.authResponse");
var access_token = response.authResponse.access_token;
alert(access_token);
}
} );
}
And I've got:
Popup opened. It's URL is this. The content is:
API Error Code: 191
API Error Description: The specified URL is not owned by the application
Error Message: Invalid redirect_uri: Given URL is not allowed by the Application configuration.
But I've did NOT got:
Edit2 The issue here is that I need to authenticate at Facebook like a desktop application. IMHO.
Upvotes: 2
Views: 5164
Reputation: 1117
Facebook does allow desktop flow authentication, see this documentation
You need to specify a special return_uri if using a chrome extension. https://www.facebook.com/connect/login_success.html
)
You also need to add tabs
and the URL to your manifest permissions - i.e.:
"permissions": [
"tabs",
"https://facebook.com/connect/*"
]
When a user clicks the login/auth button, you need to perform two steps:
Redirect the user to the OAUTH page:
chrome.tabs.create(
{
'url': "https://www.facebook.com/dialog/oauth?client_id=client_id>&redirect_uri=https://www.facebook.com/connect/login_success.html"
}, null);
Add a listener to Tab updates that searches all tabs for the success URL:
chrome.tabs.onUpdated.addListener(function() {
var lis = this;
chrome.tabs.getAllInWindow(null, function(tabs) {
for (var i = 0; i < tabs.length; i++) {
if (tabs[i].url.indexOf("https://www.facebook.com/connect/login_success.html") == 0) {
var token = tabs[i].url.match(/[\\?&#]auth_token=([^&#])*/i)
chrome.tabs.onUpdated.removeListener(lis);
return;
}
}
});
});
Note that I've got a related question on using the provided javascript api for desktop flow, as I've always received the same API error (191)
Upvotes: 4
Reputation: 3726
FB.login( function (response) {
if ( response.authResponse ) {
var access_token = response.authResponse.access_token;
alert(access_token);
}
} );
Upvotes: -1