如何通过Google Chrome Extention中的JavaScript获取Facebook的新OAuth 2.0 access_token? [英] How do I get Facebook's new OAuth 2.0 access_token with JavaScript in Google Chrome Extention?
问题描述
我想创建简单的谷歌浏览器扩展,从Facebook显示新闻。
可以通过调用这个来检索新闻提要: graph.facebook.com/me/home?access_token = ...
但是我需要 access_token 。 / p>
有人可以提供如何定义JavaScript函数,该函数在给予YOUR_APP_ID时返回 access_token 。此功能也可以在Chrome Extention中运行。
互联网上的其他示例无效,因为:
- 我没有服务器。
- FB.getLoginStatus似乎不起作用。
EDIT1 :
我已经尝试过:
function getAccessToken2(YOUR_APP_ID){
alert(before FB.init);
FB.init({
appId:YOUR_APP_ID,
status:true,//检查登录状态
cookie:true,//启用Cookie以允许服务器访问会话
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);
}
});
}
我有:
- alert(before FB.init);
- alert(before FB.login);
-
弹出窗口已打开。它的URL是这个。内容是:
API错误代码:191
API错误说明:指定的URL不属于应用程序
错误消息:无效的redirect_uri:应用程序配置不允许给定的URL。
但是我没有获取:
- alert(response);
- alert(access_token) ;
- 在调试模式下看不到其他JavaScript错误。
strong>
这里的问题是我需要像Facebook一样在桌面应用程序上进行身份验证。 IMHO。
Facebook允许桌面流验证,请参阅此文档
如果使用chrome扩展名,则需要指定一个特殊的return_uri。 https://www.facebook.com/connect/login_success.html
)
您还需要添加选项卡
和您的清单权限的URL - 即:
权限 :[
tabs,
https://facebook.com/connect/*
]
当用户点击登录/验证按钮时,您需要执行两个步骤:
步骤1
将用户重定向到OAUTH页面:
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);
步骤2
添加一个监听器标签更新搜索所有选项卡的成功URL:
chrome.tabs.onUpdated.addListener(function(){
var lis = this;
chrome.tabs.getAllInWindow(null,function(tabs){
for(var i = 0; i< tabs.length; i ++){
if标签[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;
}
}
});
});
请注意,我有一个相关问题关于使用提供的 javascript api进行桌面流程,因为我一直收到相同的API错误(191)
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:
- I don't have a server.
- FB.getLoginStatus does not seem to work.
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:
- alert("before FB.init");
- alert("before FB.login");
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:
- alert("response");
- alert(access_token);
- no other JavaScript errors seen in debug mode.
Edit2 The issue here is that I need to authenticate at Facebook like a desktop application. IMHO.
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:
Step 1
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);
Step 2
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)
这篇关于如何通过Google Chrome Extention中的JavaScript获取Facebook的新OAuth 2.0 access_token?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!