eBay Developers Program
  • Forums Sign in
  • My Account
    • My Developer Account
    • Solutions Directory
    • Applications Settings
  • Forums
    • Orders, Returns and Feedback
    • Java SDK for Trading API
    • Search
    • Selling
    • Talk to Your Fellow Devs
    • .Net SDK for Trading API
    • Tokens, Notifications, Messages
    • Feedback & Suggestions
  • Documentation
    • Release Notes
    • Finding API
    • Shopping API
    • Trading API
    • Large Merchant Services
    • Marketing Handbook
    • All Features Comparison
    • All API Documentation
  • Support
    • eBay SDKs
    • API Site Status
    • Program & News Blog
    • Compatible Application Check
    • Support Ticket
    • Knowledge Base
    • Developer Help Center
    • Search Details
  • Home
  • eBay Forums
  • Token, Messaging, Sandbox related issues /
avatar image

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

People who like this

0
Comment
10 |600 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

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
 );
Comment

People who like this

0 · Share
10 |600 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image

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!

Comment

People who like this

0 · Share
10 |600 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image

Answer by sahan.hasitha17 · Jan 04 at 02:28 PM

What should i use for $_GET['code'] ?

Comment

People who like this

0 · Share
10 |600 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

28 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

General question about how tokens work 1 Answer

Getting user token (oauth) without go to Grant App Access page - alternative flow? 0 Answers

Why is Developer get User Token page frozen on server? 0 Answers

does sandbox api work at a moment? 0 Answers

Auth' n Auth Giving empty data on return 1 Answer


Support

Developer Help Center
eBay Developers Program Blog
API Site Status
Request Support
Knowledge Base
Developer Forums
eBay Software Development Kits

API Information

API License Agreement
API Documentation
API Call Limits
Marketing Handbook
API Features Comparison

Apps Center

Applications Directory
Solutions Directory

Social Media

     

Programs

About Us
Success Stories
Affiliate Developers
eBay Market Data Program
Developer Directory
Developer Jobs
Copyright 1999 - 2018 eBay Inc. All rights reserved. User Agreement | Privacy Policy.    Site Feedback
  • Anonymous
  • Login with eBay
  • Create
  • Ask a question
  • Forums
  • Associating your listings with the eBay Catalog using eBay APIs
  • Buy APIs (BETA) - Browse
  • Buy APIs (BETA) - Order
  • Feedback, Comments, Suggestions
  • New Sell APIs - Account, Inventory, Catalog and Compliance
  • New Sell APIs - Fulfillment
  • New Sell APIs - Marketing, Analytics, Metadata
  • Post Order APIs - Cancellation
  • Post Order APIs - Inquiry, Case Management
  • Post Order APIs - Return
  • Token, Messaging, Sandbox related issues
  • eBay APIs: .NET SDK for Trading API
  • eBay APIs: Java SDK for Trading API
  • eBay APIs: Orders, resolutions and feedback
  • eBay APIs: Search
  • eBay APIs: Selling
  • eBay APIs: Talk to your fellow developers
  • Explore
  • Tags
  • Questions
  • Users
  • Badges