L
L
lexstile2018-03-26 00:52:38
PHP
lexstile, 2018-03-26 00:52:38

Getting a token for authorization through facebook?

I try to get a success token for authorization, it doesn't work.
1. I try like this:

$token = json_decode(file_get_contents(API_OAUTH_TOKEN_URL . '?client_id=' . CLIENT_ID . '&redirect_uri=' . REDIRECT_URI . '&client_secret=' . CLIENT_SECRET . '&code=' . $_GET['code']),true);

Result:
Warning: file_get_contents( https://graph.facebook.com/oauth/access_token?...
failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in login.php on line 14
2. I do this:
$api_host = API_OAUTH_TOKEN_URL;
$access_token_parameters = array(
'client_id'                =>     CLIENT_ID,
'client_secret'            =>     CLIENT_SECRET,
'redirect_uri'             =>     REDIRECT_URI,
'code'                     =>     $_GET['code']
);
$jResult = $ins->getAccessTokenUrl($api_host, $access_token_parameters);

  public function getAccessTokenUrl($api_host, $access_token_parameters){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $api_host);
    curl_setopt($ch, CURLOPT_POST,true); 
    curl_setopt($ch, CURLOPT_POSTFIELDS,$access_token_parameters); 
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_TIMEOUT, 90);		
        $jsonData = json_decode(curl_exec($ch));
        if (!$jsonData) {
            exit('Не получилось получить токен.');
        }
        curl_close($ch);
    return $jsonData;
  }

Nothing happens.
If you go to the address in manual mode, the answer is:
{
   "error": {
      "message": "The request is invalid because the app is configured as a desktop app",
      "type": "OAuthException",
      "code": 1,
      "fbtrace_id": "FhvSAYwHN1M"
   }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Philipp, 2018-03-26
@lexstile

You don't have to make homemade gardens when Facebook already has a ready-made PHP-SDK.
See
https://developers.facebook.com/docs/reference/php
https://developers.facebook.com/docs/php/howto/exa...
getjump.github.io/en-php-the-right -way/#composer_%...

V
Vadim Shadrin, 2019-01-29
@juno124

If you received an error with code 400, then you passed some parameter incorrectly, which will be indicated by a detailed error message from the facebook server.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question