How get Facebook token using Oauth2 with chrome.id

2019-02-05 07:58发布

问题:

I'm using chrome.identity in a packaged app to get a user token using 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.

Works great with Google+. But for some reason, it doesn't work for Facebook. For some reason, Facebook's OAuth appears to be special. I've added the extensionid.chromiumapp.org to the list of redirect URLs in the API. Added

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

to manifest.json. But nothing changed.

In calling chrome.identity.launchWebAuthFlow I use URL like this

"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

«"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. I am tired. What I do wrong? Why chrome.identity.getAuthToken work great for Google+ and chrome.identity.launchWebAuthFlow dont with Facebook?

I hope someone have done the same things and can help me.

回答1:

I have solved the problem. Here is an example https://github.com/blackgirl/Facebook_Oauth2_sample_for_extensions



回答2:

The "User interaction required." message means that the user needs to sign in to Facebook, and/or approve the requested OAuth scopes. The setting the { interactive: true } flag would allow identity.launchWebAuthFlow to display a window where the user would perform these steps. Since you are passing the { interactive: false } flag, identity.lauchWebAuthFlow fails.

https://www.facebook.com/dialog/oauth is most likely the URL you want to use to start your flow. 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.

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.



回答3:

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.