question

thegraysun avatar image
0 Likes"
thegraysun asked ·

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
PHPuser tokencurl
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.

davidtsadler avatar image
1 Like"
davidtsadler answered ·
You are using the format `header=value` for your HTTP headers, when in fact it needs to `header:value`. See the [PHP documentation]( http://php.net/manual/en/function.curl-setopt.php) 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]( https://github.com/davidtsadler/ebay-sdk-php) that simplifies integrating the API into a PHP project. $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 );
· Share
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.

keepcool-hvac avatar image
0 Likes"
keepcool-hvac answered ·
at line 16 : $request->code = $_GET['code']; Where does this 'code' come from? can you give me all the explanations on this mysterious line?
· Share
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.

sahan.hasitha17 avatar image
0 Likes"
sahan.hasitha17 answered ·
What should i use for $_GET['code'] ?
· Share
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.

thegraysun avatar image
0 Likes"
thegraysun answered ·
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!
· Share
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.