Question by thegraysun · May 13, 2017 at 04:05 AM · user tokencurlPHP
HOW do I retrieve an user authorization token with PHP?! HOLY CRAP!
I have looked at the guides, been performing what feels to be the same thing over and over and going through loops until I have spin myself sick! I am so irritated at this point that I have lost hope that it will ever work. This seems like such an easy tasks but for some reason, I am getting nothing but errors.
I have a page that submits the url request for the user to accept and then returns to the below accepted url.
Here is the code that is in my authorization accepted url:
$config = require 'config.php'; // Pulling from external config file - It does work!
$appID = $config['sandbox']['credentials']['appId']; // These are valid values
$certID = $config['sandbox']['credentials']['certId'];
$ruName = $config['sandbox']['ruName'];
$endpoint = 'https://api.sandbox.ebay.com/identity/v1/oauth2/token'; // URL to call
// Create the request to be POSTed
$request = "grant_type=authorization_code&code=".urlencode($_GET['code'])."&redirect_uri=".$ruName; // I have tried urlencode, no urlencode, etc
// Set up the HTTP headers
$headers = array(
'Content-Type = application/x-www-form-urlencoded',
'Authorization = Basic '.base64_encode($appID.":".$certID)
);
$session = curl_init($endpoint); // create a curl session
curl_setopt($session, CURLOPT_POST, true); // POST request type
curl_setopt($session, CURLOPT_HTTPHEADER, $headers); // set headers using $headers array
curl_setopt($session, CURLOPT_POSTFIELDS, $request); // set the body of the POST
curl_setopt($session, CURLOPT_RETURNTRANSFER, true); // return values as a string, not to std out
$response = curl_exec($session); // send the request
curl_close($session); // close the session
echo $response;
For some reason, $response always returns: {"error":"invalid_request","error_description":"request is missing a required parameter or malformed."}1
I don't really understand what is missing or malformed since I followed the guide, as far as I know. Can someone help me before I go insane?
Thanks,
Jay
Answer by davidtsadler · May 15, 2017 at 09:24 AM
You are using the format header=value
for your HTTP headers, when in fact it needs to header:value
. See the PHP documentation regarding the CURLOPT_HTTPHEADER option.
$headers = [
'Content-Type: application/x-www-form-urlencoded',
'Authorization: Basic '.base64_encode($appID.":".$certID)
];
In case you are interested I have developed a SDK that simplifies integrating the API into a PHP project.
<?php
require __DIR__.'/vendor/autoload.php';
$config = require __DIR__.'/config.php';
use \DTS\eBaySDK\OAuth\Services;
use \DTS\eBaySDK\OAuth\Types;
$service = new Services\OAuthService([
'credentials' => $config['sandbox']['credentials'],
'ruName' => $config['sandbox']['ruName'],
'sandbox' => true
]);
$request = new Types\GetUserTokenRestRequest();
$request->code = $_GET['code'];
$response = $service->getUserToken($request);
printf(
"%s\n%s\n%s\n%s\n\n",
$response->access_token,
$response->token_type,
$response->expires_in,
$response->refresh_token
);
People who like this
Answer by thegraysun · May 15, 2017 at 10:05 AM
Nice. I should have known it was going to be something stupid. :| Thank you!
As far as your SDK, I actually attempted to use it and when I went to install it with composer, it kept barking at me. Something about the version of one of the requirements. Honestly, I would much rather use a pre-designed API but, I got irritated fooling with that also. This whole project is being a pain in my butt. :(
Thanks for your help!
People who like this
Answer by sahan.hasitha17 · Jan 04 at 02:28 PM
What should i use for $_GET['code'] ?
People who like this
Your answer
