Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SPI_code Request does not work. #4

Open
phploader opened this issue Oct 11, 2022 · 0 comments
Open

SPI_code Request does not work. #4

phploader opened this issue Oct 11, 2022 · 0 comments

Comments

@phploader
Copy link

phploader commented Oct 11, 2022

Hello,
I tried to get this project running, however I had difficulties with some related components (probably the current combination is not working anymore) to get it running.

I basically don't need a template but only the function to make this work. I have written the following code for this and having trouble getting the necessary spapi_code at the last step (line 46).

Basically that is where I am trying to use the selling-partner-api, something is not working.

<?php
error_reporting(E_ALL);
ini_set('display_errors',1);
require_once __DIR__ . "/selling-partner-api/vendor/autoload.php";
$SET = [
	'LWA_CLIENT_ID'		=> 'amzn1.application-oa2-client.XXXX', #'lwaClientId',		
	'LWA_CLIENT_SECRET'	=> '7904a8a8XXXXXX', #'lwaClientIdSecret',
	'awsAccessKeyId'	=> 'AKIA4RXXXXX', #'awsAccessKey',
	'awsSecretAccessKey'=> 'sLX3zog3eJRiNaLwXXXXX', #'awsSecretKey'
	'REDIRECT_URL'	=> 'https://XXXX.XX/',
];
use GuzzleHttp\Psr7\Uri;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
$D = $_REQUEST;
if($D['amazon_callback_uri']) { #1. Rückmeldung	
	echo "<a href='{$D['amazon_callback_uri']}?redirect_uri={$SET['REDIRECT_URL']}oauth&amazon_state={$D['amazon_state']}&state=myState&version=beta'>Next 1</a>";
}else if($D['spapi_oauth_code']) { #2. Rückmeldung
	echo "Bitte Sichern:<br>";
	echo "spapi_oauth_code: <input value='{$D['spapi_oauth_code']}'><br>";
	echo "mws_auth_token: <input value='{$D['mws_auth_token']}'><br>";
	echo "selling_partner_id: <input value='{$D['selling_partner_id']}'><br>";
	echo "<br>";
	$_ENV = [
			'LWA_CLIENT_ID'		=> $SET['LWA_CLIENT_ID'], #'lwaClientId',		
			'LWA_CLIENT_SECRET'	=> $SET['LWA_CLIENT_SECRET'], #'lwaClientIdSecret',
			'awsAccessKeyId'	=> $SET['awsAccessKeyId'], #'awsAccessKey',
			'awsSecretAccessKey'=> $SET['awsSecretAccessKey'] #'awsSecretKey'
			];
		 $client = new GuzzleHttp\Client();
		$res = null;
	$res = $client->request("POST","https://api.amazon.com/auth/o2/token", [
		'headers' => [
						'content-type' => 'application/x-www-form-urlencoded;charset=UTF-8',
					],
		'body' => json_encode ( [
								"grant_type" => "authorization_code",
								"code" => $D['spapi_oauth_code'],
								'redirect_uri' => "{$SET['REDIRECT_URL']}oauth",
								"client_id" => $_ENV["LWA_CLIENT_ID"],
								"client_secret" => $_ENV["LWA_CLIENT_SECRET"],
								] )
		]);
        try {
			$res = $client->post("https://api.amazon.com/auth/o2/token", [ 
				GuzzleHttp\RequestOptions::JSON => [
				"grant_type" => "authorization_code",
				"code" => $D['spapi_oauth_code'],
				'redirect_uri' => "{$SET['REDIRECT_URL']}oauth",
				"client_id" => $_ENV["LWA_CLIENT_ID"],
				"client_secret" => $_ENV["LWA_CLIENT_SECRET"],
				]
			]);
			echo "<pre>";
			print_R($res);
			echo "</pre>";
		}
		catch (GuzzleHttp\Exception\ClientException $e) {
			echo "<textarea style='width: 1000px;height:200px;'>";
			#echo $e->getMessage() . "\n";
			#echo $e->getRequest()->getMethod();
			print_R($e->getResponse());
			echo "\nAntwort:-------------------\n";
			$info = json_decode($e->getResponse()->getBody()->getContents(), true);
			if ($info["error"] === "invalid_grant") {
				print_R($info);
			} else {
				throw $e;
			}
			echo "</textarea>";
		}
		echo "<textarea>".print_r($res)."</textarea>";
} else { #Start
	echo "<a href='https://sellercentral.amazon.com/apps/authorize/consent?application_id=amzn1.sp.solution.1bb3d307-6751-444c-9a14-86a48c11a4ac&version=beta'>Amazon Oautch</a>";
	}

I get the following error message at the end when I try to use the spapi_ouatch_code to read out more account credentials.

Fatal error: Uncaught GuzzleHttp\Exception\ClientException: Client error: `POST https://api.amazon.com/auth/o2/token` resulted in a `400 Bad Request` response: {"error_description":"The authorization grant type is not supported by the authorization server","error":"unsupported_gr (truncated...) in /home/xxxx/xxx.xx/core/platform/amazonSP/selling-partner-api/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php:113 Stack trace: #0 /home/xxxx/xxx.xx/core/platform/amazonSP/selling-partner-api/vendor/guzzlehttp/guzzle/src/Middleware.php(69): GuzzleHttp\Exception\RequestException::create() #1 /home/xxxx/xxx.xx/core/platform/amazonSP/selling-partner-api/vendor/guzzlehttp/promises/src/Promise.php(204): GuzzleHttp\Middleware::GuzzleHttp\{closure}() #2 /home/xxxx/xxx.xx/core/platform/amazonSP/selling-partner-api/vendor/guzzlehttp/promises/src/Promise.php(153): GuzzleHttp\Promise\Promise::callHandler() #3 /home/xxxx/xxx.xx/core/platform/amazonSP/selling-partner-api/vendor/guzzlehttp/promises/src/TaskQueue.php(48): GuzzleHttp\Promise\Promise::GuzzleHttp\Promise\{closure}() #4 /home/xxxx/xxx.xx/core/platform/amazonSP/selling-partner-api/vendor/guzzlehttp/promises/src/Promise.php(248): GuzzleHttp\Promise\TaskQueue->run() #5 /home/xxxx/xxx.xx/core/platform/amazonSP/selling-partner-api/vendor/guzzlehttp/promises/src/Promise.php(224): GuzzleHttp\Promise\Promise->invokeWaitFn() #6 /home/xxxx/xxx.xx/core/platform/amazonSP/selling-partner-api/vendor/guzzlehttp/promises/src/Promise.php(269): GuzzleHttp\Promise\Promise->waitIfPending() #7 /home/xxxx/xxx.xx/core/platform/amazonSP/selling-partner-api/vendor/guzzlehttp/promises/src/Promise.php(226): GuzzleHttp\Promise\Promise->invokeWaitList() #8 /home/xxxx/xxx.xx/core/platform/amazonSP/selling-partner-api/vendor/guzzlehttp/promises/src/Promise.php(62): GuzzleHttp\Promise\Promise->waitIfPending() #9 /home/xxxx/xxx.xx/core/platform/amazonSP/selling-partner-api/vendor/guzzlehttp/guzzle/src/Client.php(187): GuzzleHttp\Promise\Promise->wait() #10 /home/xxxx/xxx.xx/oauth.php(42): GuzzleHttp\Client->request() #11 {main} thrown in /home/xxxx/xxx.xx/core/platform/amazonSP/selling-partner-api/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php on line 113

Look at the code. The problem code starts from the following line:

$res = $client->request("POST","https://api.amazon.com/auth/o2/token ....

To note: I have only one Amazon account. So I created the app in the same Amazon account that I am trying to pair my app with. Is this the reason for the problem?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant