0

I'm trying to get facebook user access token using Login Dialog and without using Javascript SDK.

fetch(`https://www.facebook.com/v2.12/dialog/oauth?
client_id=APP_ID
&redirect_uri=https://www.facebook.com/connect/login_success.html
&scope=manage_pages,publish_pages
&response_type=token`, {
mode: 'no-cors',
});

It redirects to https://www.facebook.com/login.php and has cancel_url param:

cancel_url: "https://www.facebook.com/connect/login_success.html?error=access_denied&error_code=200&error_description=Permissions+error&error_reason=user_denied#_=_"

But I didn't cancelled login, no login window at all. Why this error happens and how to fix it?

user2950602
  • 395
  • 1
  • 7
  • 21
  • you can´t just do an ajax call to the login dialog...what exactly are you trying to achieve? why not just use the official sdk and FB.login or the login button? – andyrandy Mar 13 '18 at 16:09
  • @luschn I only need to get user access token. don't want to include sdk that has lots of things. So it is not possible to do without sdk? – user2950602 Mar 13 '18 at 16:19
  • of course it´s possible, see my answer – andyrandy Mar 13 '18 at 16:26

1 Answers1

0

https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow#login

Your app must initiate a redirect to an endpoint which will display the login dialog...

You have to redirect to that URL, instead of trying to use it with an AJAX call. Also, make sure you are using a valid App ID (client_id=APP_ID).

andyrandy
  • 72,880
  • 8
  • 113
  • 130
  • how can I get location response header after redirect? it contains token – user2950602 Mar 13 '18 at 17:13
  • you mean, the fragment? there are many tutorials for this, for example: https://stackoverflow.com/questions/29934606/how-to-get-access-token-from-the-url – andyrandy Mar 13 '18 at 17:15
  • also, check out the links in my answer and search for "Handling Login Dialog Response". i suggest you read the whole page, if you really want to implement manual login. – andyrandy Mar 13 '18 at 17:16
  • no, not parsing, I mean I need to get response headers after redirect. I use window.location.href. – user2950602 Mar 13 '18 at 17:28
  • exactly, just take a look at the link in my answer, it explains how to handle the response. – andyrandy Mar 13 '18 at 17:31