简体   繁体   English

如何使用带有chrome.identity的Oauth2获取Facebook令牌

[英]How get Facebook token using Oauth2 with chrome.identity

I'm using chrome.identity in a packaged app to get a user token using Facebook. 我在打包的应用程序中使用chrome.identity来使用Facebook获取用户令牌。 I call chrome.identity.launchWebAuthFlow, and I need to take the access token, send it to the server, and then access token is used to verify the user. 我调用chrome.identity.launchWebAuthFlow,我需要获取访问令牌,将其发送到服务器,然后访问令牌用于验证用户。

Works great with Google+. 适用于Google+。 But for some reason, it doesn't work for Facebook. 但出于某种原因,它不适用于Facebook。 For some reason, Facebook's OAuth appears to be special. 出于某种原因,Facebook的OAuth似乎很特别。 I've added the extensionid.chromiumapp.org to the list of redirect URLs in the API. 我已将extensionid.chromiumapp.org添加到API中的重定向网址列表中。 Added 添加

"https://extensionid.chromiumapp.org/*",
    "https://facebook.com/*",
    "https://www.facebook.com/dialog/",
    "https://graph.facebook.com/*"

to manifest.json. 到manifest.json。 But nothing changed. 但没有改变。

In calling chrome.identity.launchWebAuthFlow I use URL like this 在调用chrome.identity.launchWebAuthFlow时,我使用这样的URL

"https://www.facebook.com/dialog/oauth/access_token?" +
                   "client_id=myclientid&client_secret=myclientsecret&" + 
                  "response_type=token&grant_type=client_credentials"

When I try to invoke it, I get the following error message: 当我尝试调用它时,我收到以下错误消息:

«launchWebAuthFlow completed Object {message: "Authorization page could not be loaded."} »

And when I use URL like 当我使用URL时

«"https://www.facebook.com/dialog/oauth?client_id=myclientid&redirect_uri=https://myextensionid.chromiumapp.org/facebook.com=token&scope=user"» 

I get the next error: 我收到下一个错误:

«launchWebAuthFlow completed Object {message: "User interaction required."} undefined »

I try to get facebook token in 4 days. 我尝试在4天内获得facebook令牌。 I am tired. 我累了。 What I do wrong? 我做错了什么? Why chrome.identity.getAuthToken work great for Google+ and chrome.identity.launchWebAuthFlow dont with Facebook? 为什么chrome.identity.getAuthToken适用于Google+和chrome.identity.launchWebAuthFlow不适用于Facebook?

I hope someone have done the same things and can help me. 我希望有人做了同样的事情,可以帮助我。

I have solved the problem. 我已经解决了这个问题。 Here is an example https://github.com/blackgirl/Facebook_Oauth2_sample_for_extensions 这是一个示例https://github.com/blackgirl/Facebook_Oauth2_sample_for_extensions

The "User interaction required." 需要“用户交互”。 message means that the user needs to sign in to Facebook, and/or approve the requested OAuth scopes. 消息表示用户需要登录Facebook,和/或批准所请求的OAuth范围。 The setting the { interactive: true } flag would allow identity.launchWebAuthFlow to display a window where the user would perform these steps. 设置{interactive:true}标志将允许identity.launchWebAuthFlow显示用户将执行这些步骤的窗口。 Since you are passing the { interactive: false } flag, identity.lauchWebAuthFlow fails. 由于您传递了{interactive:false}标志,因此identity.lauchWebAuthFlow失败。

https://www.facebook.com/dialog/oauth is most likely the URL you want to use to start your flow. https://www.facebook.com/dialog/oauth很可能是您要用来启动流程的URL。 The client_credentials grants are normally for cases where you are accessing resources owned by your app, rather than resources owned by a specific user. client_credentials授权通常用于您访问应用程序拥有的资源的情况,而不是特定用户拥有的资源。

However if you did want to debug that "Authorization page could not be loaded" case, the way to do it would be to open up chrome://net-internals and look for error responses coming back from Facebook. 但是,如果您确实想调试“无法加载授权页面”的情况,那么执行此操作的方法是打开chrome:// net-internals并查找从Facebook返回的错误响应。

chrome.identity.launchWebAuthFlow(
{'url': 'https://www.facebook.com/dialog/oauth?client_id=myclientid&redirect_uri=https://myextensionid.chromiumapp.org/facebook.com=token&scope=user, 'interactive': true},
function(redirect_url) { 
    /* Extract token from redirect_url */ 
    console.log(redirect_url);
});

//'interactive': true //'互动':是的

what will this flag do, will show a dialog box asking username and password upon successful login it will ask for grant access just press that and you will receive redirect url in above function body. 这个标志会做什么,会在成功登录时显示一个询问用户名和密码的对话框,它会要求授予访问权限,只需按下该按钮,您将在上面的功能体中收到重定向URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM