allegro / allegro-api

Issue tracker and wiki for Allegro REST API
https://developer.allegro.pl/
217 stars 39 forks source link

Pobranie szczegółów aukcji po ID aukcji #9821

Open mario033c opened 2 months ago

mario033c commented 2 months ago

Proszę o pomoc / nakierowanie co robię nie tak. Chciałbym pobrać szczegóły aukcji po ID aukcji, poniżej kod:

define('REDIRECT_URI', 'http://localhost/allegro/index.php');
define('AUTH_URL', 'https://allegro.pl/auth/oauth/authorize');
define('TOKEN_URL', 'https://allegro.pl/auth/oauth/token');
define('CLIENT_ID', '...');
define('CLIENT_SECRET', '...');

function getAuthorizationCode() {
    $authorization_redirect_url = AUTH_URL . "?response_type=code&client_id=" 
    . CLIENT_ID . "&redirect_uri=" . REDIRECT_URI;
    ?>
    <html>
    <body>
    <a href="<?php echo $authorization_redirect_url; ?>">Zaloguj do Allegro</a>
    </body>
    </html>
    <?php
}

function getCurl($headers, $url, $content = null) {
    $ch = curl_init();
    curl_setopt_array($ch, array(
        CURLOPT_URL => $url,
        CURLOPT_HTTPHEADER => $headers,
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_RETURNTRANSFER => true
    ));
    if ($content !== null) {
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $content);
    }
    return $ch;
}

function getAccessToken($authorization_code) {
    $authorization = base64_encode(CLIENT_ID.':'.CLIENT_SECRET);
    $authorization_code = urlencode($authorization_code);
    $headers = array("Authorization: Basic {$authorization}","Content-Type: application/x-www-form-urlencoded");

    $content = "grant_type=client_credentials";
    $url = "https://allegro.pl/auth/oauth/token";
    $ch = getCurl($headers, $url, $content);
    $tokenResult = curl_exec($ch);
    $resultCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    if ($tokenResult === false || $resultCode !== 200) {
        exit ("Something went wrong");
    }
    return json_decode($tokenResult)->access_token;
}

function getMainCategories($token) {
    $headers = array("Authorization: Bearer " .$token, "Accept: application/vnd.allegro.public.v1+json","Content-Type: application/vnd.allegro.public.v1+json");

    $url = "https://api.allegro.pl/sale/?offer.id=98989999";
    $ch = getCurl($headers, $url);
    $mainCategoriesResult = curl_exec($ch);
    $resultCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    if ($mainCategoriesResult === false || $resultCode !== 200) {
        exit ("Something went wrong");
    }
    $categoriesList = json_decode($mainCategoriesResult);
    return $categoriesList;

}

function main(){
    if ($_GET["code"]) {
        $access_token = getAccessToken($_GET["code"]);
        $auctionStatus = getMainCategories($access_token);
       echo print_r($auctionStatus);
    } else {    
        getAuthorizationCode();
    }
}

main();
AureliuszBrussy commented 2 months ago

Żeby pobrać szczegółowe dane oferty (zautoryzowanego użytkownika), najlepiej, jeżeli skorzystasz z endpointu GET /sale/product-offers/{offerId}. W funkcji getMainCategories podmień url.

mario033c commented 2 months ago

Dziękuję za odpowiedź, zmieniłem i dostaję od wczoraj taki błąd:

Array ( [0] => stdClass Object ( [code] => INTERNAL_SERVER_ERROR [message] => Internal server error. [details] => [path] => [userMessage] => Internal server error. Try again in a few minutes. [metadata] => stdClass Object ( ) ) ) )

AureliuszBrussy commented 2 months ago

Podeślij proszę trace-id przykładowego wywołania - znajdziesz go w nagłówku naszego response'a.

AureliuszBrussy commented 2 months ago

Ok, widzę w czym rzecz - korzysztasz z nieprawidłowego grant_type. Pomieszałeś 2 typy autoryzacji.

Twoja funkcja GetAccessToken powinna wyglądać tak:

function getAccessToken($authorization_code) {
    $authorization = base64_encode(CLIENT_ID.':'.CLIENT_SECRET);
    $authorization_code = urlencode($authorization_code);
    $headers = array("Authorization: Basic {$authorization}","Content-Type: application/x-www-form-urlencoded");
    $content = "grant_type=authorization_code&code=${authorization_code}&redirect_uri=" . REDIRECT_URI;
    $ch = getCurl($headers, $content);
    $tokenResult = curl_exec($ch);
    $resultCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    if ($tokenResult === false || $resultCode !== 200) {
        exit ("Something went wrong $resultCode $tokenResult");
    }
    return json_decode($tokenResult)->access_token;
}