davidtsadler / ebay-sdk-php

An eBay SDK for PHP. Use the eBay API in your PHP projects.
Apache License 2.0
349 stars 341 forks source link

"invalid_client" on oAuth when requesting user access token #256

Closed saulyx closed 4 years ago

saulyx commented 5 years ago

I've been at this for days, including a stand alone curl script( https://stackoverflow.com/questions/55412803/ebay-api-always-returning-invalid-client-on-production-but-sandbox-works-fine ) and by using this SDK I thought i'd finally win this battle ,

Seems like the SDK would be best bet, but i'm once again getting the same issue, I've hacked together this code, after many hours of editing it and testing all sorts of different things, but hopefully someone can point me in the right direction as i'm about to pull my hair out after 3 days.

So the code I have right now is:

`require 'vendor/autoload.php';

$config = require 'configuration.php';

//print_r($config['production']);

use \DTS\eBaySDK\OAuth\Services as OauthService;
use \DTS\eBaySDK\OAuth\Types as OauthType;

use \DTS\eBaySDK\Constants;
use \DTS\eBaySDK\Trading\Services;
use \DTS\eBaySDK\Trading\Types;
use \DTS\eBaySDK\Trading\Enums;

$service = new OauthService\OAuthService([
    'credentials' => $config['production']['credentials'],
    'ruName'      => $config['production']['ruName'],
    'sandbox'     => false
]);

$oauthParam = [
  'client_id' => $config['production']['credentials']['appId'],
  'redirect_uri' => $config['production']['redirect_uri'],
  'response_type' => 'code',
  'scope' => 'https://api.ebay.com/oauth/api_scope'
];

$url =  $service->redirectUrlForUser([
    'state' => 'bar',
    'scope' => [
        'https://api.ebay.com/oauth/api_scope/sell.account',
        'https://api.ebay.com/oauth/api_scope/sell.inventory'
    ]
]);

@session_start();

if(isset($_SESSION['ebay_oauth_token'])) {
    $token = $_SESSION['ebay_oauth_token']['code'];
}
else {
    if(isset($_GET['code'])) {
        $token = $_GET['code'];
        $_SESSION['ebay_oauth_token']['code'] = $token;

        $request = new OauthType\GetUserTokenRestRequest();
        $request->code = $token;
        print_r($request);
        $response = $service->getUserToken($request);
        print_r($response);
        print_r($service);
        exit;
        if ($response->getStatusCode() !== 200) {
            //Error
        } else {
            $_SESSION['ebay_oauth_token']['access_token'] = $response->access_token;
        }
    } else {
       exit("<a href='{$url}'>{$url}</a>");
    }

}
print_r($_SESSION);

$userOauthToken = $_SESSION['ebay_oauth_token']['access_token'];`

And what ever I do, or what ever I change, I get the following:

[values:DTS\eBaySDK\Types\BaseType:private] => Array ( [error] => invalid_client [error_description] => client authentication failed )

Can someone assist? I've read so many different documentations, tried so many different samples of codes and examples and still dead stuck on this error. Interestingly enough, my stand alone sample works perfectly on sandbox, but not production..

Sorry if my post doesn't make sense, need some sleep.

michabbb commented 5 years ago

maybe my little ebay oauth playground does help you in some way....

saulyx commented 5 years ago

@michabbb yeah, we tried.. Here's the outcome:

https://www.dropbox.com/s/bnwsjrfqw6anjpx/Screen%20Shot%202019-04-02%20at%2008.24.52.png?dl=0

not sure what on earth is going on..

michabbb commented 5 years ago

as always: when things don´t work as expected: do it RAW. don´t use any frameworks where you don´t know what´s going on inside. grab postman and do the requests there to be 100% sure, that it´s not a config or SDK problem. beside that, you should have checked if you enabled oauth for your production keys in your ebay dev account, did you ? ;)

saulyx commented 5 years ago

@michabbb thats why I linked the stackoverflow article, as it's mine, and I did it in raw PHP, in postman I get same issue as well. As far as enabling, i'm guessing it's here: http://prntscr.com/n6enqh ? Still no dice :(

michabbb commented 5 years ago

first of all, there is no problem with ebay, i am able to get a user token:

image

postman generates this code for me:

<?php

$request = new HttpRequest();
$request->setUrl('https://api.ebay.com/identity/v1/oauth2/token');
$request->setMethod(HTTP_METH_POST);

$request->setHeaders(array(
  'Postman-Token' => '784bae81-d5e6-402e-8d51-1c2c7d4220ad',
  'cache-control' => 'no-cache',
  'Authorization' => 'Basic xxxxxxxxxxxxxxxxxxxxxx==',
  'Accept' => 'application/json',
  'Content-Type' => 'application/x-www-form-urlencoded'
));

$request->setContentType('application/x-www-form-urlencoded');
$request->setPostFields(array(
  'grant_type' => 'authorization_code',
  'code' => 'v^1.1xxxxxxxxxxxxxxxxxx',
  'redirect_uri' => 'MACxxxxxxxxxxxxxx',
  'undefined' => null
));

try {
  $response = $request->send();

  echo $response->getBody();
} catch (HttpException $ex) {
  echo $ex;
}

unless you are not able to make this working with that code (or postman) you may use wrong scopes or maybe there is problem with the ebay user itself, maybe use a different ebay-account (for login) to be sure that your problem is not related to that specific ebay user you are dealing with right now.

michabbb commented 5 years ago

i also tested with my playground a little bit a got confusing results, i also got several times a "bad request", i just called the oauth-login several times again, and after 6 or 7 tries, i got my access token:

image

maybe ebay has issues here, i don´t know 🤔

when you play with my "playground" you can add "dumps" inside the function \League\OAuth2\Client\Provider\AbstractProvider::getParsedResponse to see the request and the response, that helps debugging:

public function getParsedResponse(RequestInterface $request)
    {
        try {
            dump($request);
            dump((string)$request->getBody());
            $response = $this->getResponse($request);
            dump($response);
            dump((string)$response->getBody());
        } catch (BadResponseException $e) {
            $response = $e->getResponse();
        }

        $parsed = $this->parseResponse($response);

        $this->checkResponse($response, $parsed);

        return $parsed;
    }
mikkame commented 5 years ago

In my case, Time will solve everything