bhushankummar / eBay-node-client

Ebay NodeJS Wrapper
MIT License
55 stars 66 forks source link

Unable to get User Token to make connection to ebay #24

Closed doverradio closed 4 years ago

doverradio commented 4 years ago

Hello,

I have been unable to connect to Ebay with any of the examples in Javascript.

I tried the retrieveUserToken.js example, like so:

'use strict';

var clientId = process.env.EBAY_CLIENT_ID || 'xxxx';
var clientSecret = process.env.EBAY_CLIENT_SECRET || 'xxxx';

var eBay = require('ebay-node-client')(clientId, clientSecret);

var userRequest = async function () {
    var token = {};
    try {
        var options = {
            redirectURI: 'Bhushankumar_L-Bhushank-DemoAp-onqph',
            code: 'v^1.1#i^1#f^0#r^1#p^3#I^3#t^Ul41Xzg6QTgwNEM4REE5RDUwODMyNzgyNjJFNUVBRTk1OTQ3QzlfMl8xI0VeMTI4NA=='
        };
        token = await eBay.user.retrieveUserToken();
        console.log('response ', token);
    } catch (error) {
        console.log('error ', error);
        return;
    }
    console.log('user token ', token.access_token);
    eBay.setUserToken(token.access_token);
    var data = {
        q: 'drone'
    };
    try {
        var response = await eBay.catalog.search(data);
        console.log('response', response);
    } catch (error) {
        console.log('error ', error);
        return;
    }
};
userRequest();

I kept the options the same because I don't know what to put there. What are you supposed to put there? And where can I find it?

The result was this

PS C:\Users\User\eBay-node-client> node .\retrieveUserToken.js
error  Error: Response Error: 400 Bad Request
    at internals.Client._shortcut (C:\Users\User\Desktop\wiki\e\eBay-node-client\node_modules\wreck\lib\index.js:635:11)
    at processTicksAndRejections (internal/process/task_queues.js:85:5)
    at async Object.request (C:\Users\User\Desktop\wiki\e\eBay-node-client\node_modules\simple-oauth2\lib\core.js:64:20) {
  data: {
    isResponseError: true,
    headers: {
      'content-length': '103',
      cneonction: 'close',
      date: 'Wed, 04 Dec 2019 04:29:10 GMT',
      rlogid: 't6ldssk67%3D9vjdldssk67*0667%3B1%3F%29pqtfwpu%29pie%29fgg%7E-fij-16ecf2b487e-0x122',
      'set-cookie': [Array],
      'x-content-type-options': 'nosniff',
      'x-ebay-c-version': '1.0.0',
      'x-ebay-client-tls-version': 'CLIENT.SSL.VERSION+", "+CLIENT.IP.SRC',
      'x-ebay-esb-upstream': 'oauth31.vip.ebay.com',
      'x-ebay-request-id': '16ecf2b486e.a9e0b57.1ed59.f488cd7b![',
      'x-frame-options': 'SAMEORIGIN',
      'x-xss-protection': '1; mode=block',
      'content-type': 'application/json',
      connection: 'keep-alive',
      'strict-transport-security': 'max-age=31536000'
    },
    res: IncomingMessage {
      _readableState: [ReadableState],
      readable: false,
      _events: [Object: null prototype],
      _eventsCount: 2,
      _maxListeners: undefined,
      socket: [TLSSocket],
      connection: [TLSSocket],
      httpVersionMajor: 1,
      httpVersionMinor: 1,
      httpVersion: '1.1',
      complete: true,
      headers: [Object],
      rawHeaders: [Array],
      trailers: {},
      rawTrailers: [],
      aborted: false,
      upgrade: false,
      url: '',
      method: null,
      statusCode: 400,
      statusMessage: 'Bad Request',
      client: [TLSSocket],
      _consuming: false,
      _dumped: false,
      req: [ClientRequest]
    },
    payload: {
      error: 'invalid_request',
      error_description: 'request is missing a required parameter or malformed.'
    }
  },
  isBoom: true,
  isServer: false,
  output: {
    statusCode: 400,
    payload: {
      statusCode: 400,
      error: 'Bad Request',
      message: 'Response Error: 400 Bad Request'
    },
    headers: {}
  },
  reformat: [Function],
  typeof: [Function: Error]
}

How do I solve this?

tleung927 commented 4 years ago

I have the same problem, able to get the application token without any problem, But how to setup and get the user token????

doverradio commented 4 years ago

@tleung927 funny you should mention this. I have literally been banging my head trying to solve this for the past 24 hours.

In this part (taken from my first post above):

var options = {
            redirectURI: 'Bhushankumar_L-Bhushank-DemoAp-onqph',
            code: 'v^1.1#i^1#f^0#r^1#p^3#I^3#t^Ul41Xzg6QTgwNEM4REE5RDUwODMyNzgyNjJFNUVBRTk1OTQ3QzlfMl8xI0VeMTI4NA=='
        };

I found that I was able to get the redirect URL from this script below and put the resulting URL in the above code:

'use strict';

var clientId = process.env.EBAY_CLIENT_ID || 'XXXXX';
var clientSecret = process.env.EBAY_CLIENT_SECRET || 'XXXXX';

var eBay = require('ebay-node-client')(clientId, clientSecret);

var userRequest = async function () {
    try {
        var DEFAULT_SCOPE = 'https://api.ebay.com/oauth/api_scope/sell.inventory';

        var options = {
            scope: DEFAULT_SCOPE,
            redirectURI: 'https://auth.ebay.com/oauth2/authorize?client_id=XXXXX&response_type=code&redirect_uri=XXXXX&scope=https://auth.ebay.com/oauth2/authorize?client_id=XXXXX&response_type=code&redirect_uri=XXXXX&scope=https://api.ebay.com/oauth/api_scope/sell.inventory'
        };
        var response = await eBay.user.getRedirectUrl(options);
        console.log('redirectUrl ', response);
    } catch (error) {
        console.log('error ', error);
    }
};
userRequest();

Then, I was able to go to my ebay developer account and pull the User Token there. Here is URL for that: https://developer.ebay.com/my/auth/?env=production&index=0&auth_type=oauth

So, after ALL THAT, I obtained a long User Token.

Unfortunately, after adding in both Redirect URL + User Token to that original script, it STILL will throw this error:

error  {
  error: 'invalid_scope',
  error_description: 'The requested scope is invalid, unknown, malformed, or exceeds the scope granted to the client',
  Headers: {
    'content-length': '142',
    cneonction: 'close',
    date: 'Mon, 13 Jan 2020 18:50:27 GMT',
    rlogid: 'XXXXX',
    'set-cookie': [
      'XXXXX'
    ],
    'x-content-type-options': 'nosniff',
    'x-ebay-c-version': '1.0.0',
    'x-ebay-client-tls-version': 'CLIENT.SSL.VERSION+", "+CLIENT.IP.SRC',
    'x-ebay-esb-upstream': 'oauth31.vip.ebay.com',
    'x-ebay-request-id': 'XXXXX',
    'x-frame-options': 'SAMEORIGIN',
    'x-xss-protection': '1; mode=block',
    'content-type': 'application/json',
    connection: 'keep-alive',
    'strict-transport-security': 'max-age=31536000'
  },
  StatusCode: 400
}

My eyes immediately went to "The requested scope is invalid, unknown, malformed, or exceeds the scope granted to the client". But after logging into the same developer account and checking the oAuth scopes, I found all those scopes are shown as part of the allowed API scopes. Fail!

How about you? Able to get farther? I literally need this to list a single item!

tleung927 commented 4 years ago

@doverradio in order to allowed rest API call we need to use the user token. After lots google, i think ebay will return the user token from the redirect URL. I am thinking to use the app.get() instead to get the token.

doverradio commented 4 years ago

How were you able to get the user token after the redirect url? Were you successful?

bhushankummar commented 4 years ago

@doverradio @tleung927 This is an issue for passing wrong parameters. You can check with eBay forums or documentation.

doverradio commented 4 years ago

Please can you give a working example for us? I can confirm the example given in your library does not work now. Maybe in the past it worked for you but I have tried multiple ways and got no User Token.