question

aguruzde-0 avatar image
0 Likes"
aguruzde-0 asked

Error getting ebay code. “Invalid_grant”

I'm trying to get User Access Token to use Ebay API, now I'm testing with Sandbox enviorment but I can't complete this step: https://developer.ebay.com/api-docs/static/oauth-auth-code-grant-request.html I can make the previous step, making the logging with a sandbox test user and getting a code like this (v^1.1%23....D%3D) from the URL. But when I try to do this step and I paste all the info in postman, it's returns me the following error: { "error": "invalid_grant", "error_description": "the provided authorization grant code is invalid or was issued to another client" } I'm filling redirect_uri with RuName, as documentation says. Where is the error? Do I have to do something with the code before making the post call? Thanks in advance.
user tokenuser token generate login error
10 |600 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

michab2003 avatar image
0 Likes"
michab2003 answered
you can find some infos about that issue [in this thread][1], maybe it helps.... [1]: https://github.com/davidtsadler/ebay-sdk-php/issues/212
10 |600 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

tanjiybhu_0 avatar image
0 Likes"
tanjiybhu_0 answered

Hello Mr. @ ,

I have used this answer in my code. I am trying to authorize eBay user for exchanging token. Unfortunately, it's not working and returning 2type of error for different aspect. The errors are: 'invalid grant' and 'invalid client'. I have attached screenshot of my code, headers, body, and response.
Whenever, I am putting authorization to bearer, it's showing invalid_grant error. When, put authorization to basic. It's showing invalid_client. Both are returning response code 200 on browser, but in insomnia/postman. it's showing the error.

Please take a look on the screenshots and code.

Body, Header_1, Header_2

Code:

    <?php

    $clientID     = 'client_id';
    $clientSecret = 'client_secret';
    $authCode     = 'authorization_code';

    $url = 'https://api.sandbox.ebay.com/identity/v1/oauth2/token';
    $redirectUrlName = 'redirect_uri';

    $body = http_build_query([
        'grant_type'  => 'authorization_code',
        'code'        => $authCode,
        'redirect_uri'=> $redirectUrlName
    ]);

    $headers = [
        'Cache-Control: no-cache',
        'Accept       : application/json',
        'Content-Type : application/x-www-form-urlencoded',
        'Authorization: Basic '.base64_encode($clientID.':'.$clientSecret)
    ];

    $curl = curl_init();

    curl_setopt_array($curl, array(
        CURLOPT_URL            => $url,
        CURLOPT_RETURNTRANSFER => true,
        // CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_CUSTOMREQUEST  => 'POST',
        CURLOPT_POSTFIELDS     => $body,
        CURLOPT_HTTPHEADER     => $headers
    ));

    $response = curl_exec($curl);
    $err      = curl_error($curl);

    curl_close($curl);

    if ($err) {
        echo "cURL Error #:" . $err;
    } else {
        echo $response."\n";
    }

    ?>

Same thing worked for me, when I was trying for client credentials. Added the screenshot. Please take a look.

Body, Header

    <?php

    $clientID     = 'client_id';
    $clientSecret = 'client_secret';

    $url = 'https://api.sandbox.ebay.com/identity/v1/oauth2/token';
    $redirectUrlName = 'http://localhost';

    $body = http_build_query([
        'grant_type'  => 'client_credentials',
        'redirect_uri'=> $redirectUrlName
    ]);

    $headers = [
        'Cache-Control: no-cache',
        'Accept       : application/json',
        'Content-Type : application/x-www-form-urlencoded',
        'Authorization: Basic '.base64_encode($clientID.':'.$clientSecret)
    ];

    $curl = curl_init();

    curl_setopt_array($curl, array(
        CURLOPT_URL            => $url,
        CURLOPT_RETURNTRANSFER => true,
        // CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_CUSTOMREQUEST  => 'POST',
        CURLOPT_POSTFIELDS     => $body,
        CURLOPT_HTTPHEADER     => $headers
    ));

    $response = curl_exec($curl);
    $err      = curl_error($curl);

    curl_close($curl);

    if ($err) {
        echo "cURL Error #:" . $err;
    } else {
        echo $response."\n";
    }

    ?>
10 |600 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.