eBay API always returning invalid_client on production but sandbox works fine [closed]
Asked Answered
C

3

6

I've been at this for several days now, and I can't seem to find the solution.

I'm trying to authorize via ebay API to get a user token for further actions.

When I use sandbox environment, it all works great, however as soon as I use production, I get the following error:

{"error":"invalid_client","error_description":"client authentication failed"}

My file structure is as follows:

config.php:

<?php
    /* SAndbox 
$config = [
    'client_id' => 'xxxxx-xxxxxx-SBX-e55b66fda-63c7e331',
    'client_secret' => 'SBX-xxxxxx-dxxxxxb-47cb-9bee-f33b',
    'ru_name' => 'xxxxxxxxx-oxxxxas-xxxxxxx-tsuggc',
    'login_url' => 'https://auth.sandbox.ebay.com/oauth2/authorize',
    'oauth_url' => 'https://api.sandbox.ebay.com/identity/v1/oauth2/token',
    'api_scopes' => ['https://api.ebay.com/oauth/api_scope/sell.inventory'],
];
*/

$config = [
    'client_id' => 'xxxxxx-CxxxxxxT-PRD-455bfe8ea-7e445131',
    'client_secret' => 'PRD-797xxxx7bf-d5xxxc-4a19-axxade-ab8xx6',
    'ru_name' => 'xxxxxxx-osxxxxxxas-CxxxxS-hjlalp',
    'login_url' => 'https://auth.ebay.com/oauth2/authorize',
    'oauth_url' => 'https://api.ebay.com/identity/v1/oauth2/token',
    'api_scopes' => ['https://api.ebay.com/oauth/api_scope/sell.inventory'],
];

getLogin.php:

<?php
include_once 'config.php';

$url = $config['login_url'];
$url .= '?client_id='.$config['client_id'];
$url .= '&response_type=code';
$url .= '&redirect_uri='.urlencode($config['ru_name']);
$url .= '&scope='.implode(' ', $config['api_scopes']);
echo "<a href='{$url}'>login</a><br/><br/>";


die();

login.php(where I get redirected after authorization):

<?php
include_once 'config.php';
echo "<pre>";
$code = $_GET['code'];
$authorization = 'Basic '.base64_encode($config['client_id'].':'.$config['client_secret']);
print_r($config);
$curl = curl_init();

curl_setopt_array($curl, [
    CURLOPT_URL => $config['oauth_url'],
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_POSTFIELDS => "grant_type=client_credentials&code=".$code."&redirect_uri=".$config['ru_name'],
    CURLOPT_HTTPHEADER => [
        "Authorization: ".$authorization,
        "Content-Type: application/x-www-form-urlencoded",
        "cache-control: no-cache",
    ],
]);

$response = curl_exec($curl);
$err = curl_error($curl);
$info = curl_getinfo($curl);
curl_close($curl);




print_r($info);
if ($err) {
    echo "cURL Error #:".$err;
} else {
    echo 'resp: '.$response;
}

Any and all help would be appreciated, as i'm about to pull my hair out over this!

Corvette answered 29/3, 2019 at 7:54 Comment(1)
$url .= '&scope='.implode(' ', $config['api_scopes']); will create a corrupted url, check out http_build_query()Lines
P
0

You seem to have all valid information needed,the only thing that's seems to be fishy is : you have fetched scope in

$url .= '&scope='.implode(' ', $config['api_scopes']);

but i think you haven't include this scope in

  CURLOPT_POSTFIELDS => "grant_type=client_credentials&code=".$code."&redirect_uri=".$config['ru_name'],
Phasia answered 2/4, 2019 at 15:50 Comment(0)
M
0
curl_setopt($curl_handle, CURLOPT_POSTFIELDS,http_build_query($post_data));

kindly include http_build_query($post_data) in your Curl post requests.

Maidenhead answered 7/4, 2019 at 16:43 Comment(0)
C
0

I don't thing that it has anything to do with your code. It is probably related to API call limits. Can you test it with another API client, or use the second link to verify your quota.

Calia answered 8/4, 2019 at 4:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.