Allegro API can't Unauthorized: 401 {"error":"Unauthorized","error_description":"Unauthorized"}

96 Views Asked by At

I have app on allegrosandbox. But I can't authorize it. On screen you can see that my app - device. So I try code for auth for device. But it doesn't work.

Here my app: enter image description here

Here my code:

<?php
ini_set('error_reporting', E_ALL);
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
define('CLIENT_ID', ''); // enter Client_ID
define('CLIENT_SECRET', ''); // enter Client_Secret
define('CODE_URL', 'https://allegro.pl/auth/oauth/device');
define('TOKEN_URL', 'https://allegro.pl/auth/oauth/token');

function getCurl($url, $headers, $content) {
    $ch = curl_init();
    curl_setopt_array($ch, array(
        CURLOPT_URL => $url,
        CURLOPT_HTTPHEADER => $headers,
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_POST => true,
        CURLOPT_POSTFIELDS => $content
    ));
    return $ch;
}


function getCode(){
    $authorization = base64_encode(CLIENT_ID.':'.CLIENT_SECRET);
    $headers = array("Authorization: Basic {$authorization}","Content-Type: application/x-www-form-urlencoded");
    $content = "client_id=" .CLIENT_ID;
    $ch = getCurl(CODE_URL, $headers, $content);
    $result = curl_exec($ch);
    $resultCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    if ($result === false || $resultCode !== 200) {
        exit ("Something went wrong:  $resultCode $result");
    }
    return json_decode($result);
}


function getAccessToken($device_code) {
    $authorization = base64_encode(CLIENT_ID.':'.CLIENT_SECRET);
    $headers = array("Authorization: Basic {$authorization}","Content-Type: application/x-www-form-urlencoded");
    $content = "grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Adevice_code&device_code=${device_code}";
    $ch = getCurl(TOKEN_URL, $headers, $content);
    $tokenResult = curl_exec($ch);
    curl_close($ch);
    return json_decode($tokenResult);
}


function main(){
    $result = getCode();
    echo "User, open this address in the browser: \n" . $result->verification_uri_complete ."\n";
    $accessToken = false;
    $interval = (int)$result->interval;
     do {
         sleep($interval);
         $device_code = $result->device_code;
         $resultAccessToken = getAccessToken($device_code);
          if (isset($resultAccessToken->error)) {
               if ($resultAccessToken->error == 'access_denied') {
                   break; 
              } elseif ($resultAccessToken->error == 'slow_down') {
                   $interval++; 
                }
            } else {
                $accessToken = $resultAccessToken->access_token;
                echo "access_token = ", $accessToken;
            }
        } while ($accessToken == false);

    }


main();

?>

And here my answer: Something went wrong: 401 {"error":"Unauthorized","error_description":"Unauthorized"}

I try change curl to GuzzleHttp - but didn't help. The same problem: 401 Unauthorized

0

There are 0 best solutions below