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

Receiving error while trying to use SDK with Meteor #406

Closed
mikeacre opened this issue Aug 13, 2020 · 25 comments
Closed

Receiving error while trying to use SDK with Meteor #406

mikeacre opened this issue Aug 13, 2020 · 25 comments

Comments

@mikeacre
Copy link

mikeacre commented Aug 13, 2020

Hey,

When I try to call the sdk I get the error:
"Exception while invoking method 'shop.get.customers' RequestError: Unexpected token < in JSON at position 0"
Which I assume is due to an authentication error.

I used ShopToken to generate a code and then an access token.

I am initializing the SDK as such:
`
const shopify = new Shopify({
shopName: {STORE_NAME},
apiKey: {API_KEY},
password: {ACCESS_TOKEN},

});
`

The store name is the name of the store I authenticated with the token and have the access token for.

The api key is the one in my shopify partners dashboard that shows the app has been installed and has the appropriate permissions.

The Access Token is the one I generated after making the code with the correct permissions

Any ideas what could be causing this?

@shank318
Copy link

Is it resolved? I am getting the same

@mikeacre
Copy link
Author

No I’ve had to go back to using square for my projects for now.

@lpinca
Copy link
Collaborator

lpinca commented Aug 18, 2020

Make sure password is the private app password and not the secret or the OAuth 2.0 access token.

@mikeacre
Copy link
Author

Thank you for the reply @lpinca , What is the password then?

password - Required for private apps - A string that specifies the private app password. This option must be used in conjunction with the apiKey option and is mutually exclusive with the accessToken option.

Doest that mean the password is the Api Key?

@lpinca
Copy link
Collaborator

lpinca commented Aug 18, 2020

You can find the password in the admin page of the private app of your shop:

screenshot

@mikeacre
Copy link
Author

Still receiving the same error this way. But also doesn't this negate me making a private app and creating the access token? Is there no need for the access token at all then?

@mikeacre
Copy link
Author

I can create a simple http request using those credentials, but this does not work for the sdk.

@lpinca
Copy link
Collaborator

lpinca commented Aug 18, 2020

But also doesn't this negate me making a private app and creating the access token? Is there no need for the access token at all then.

Yes, exactly, you don't need an access token for private apps.

I can create a simple http request using those credentials, but this does not work for the sdk.

I don't know, can you create test case to reproduce the issue? Something like this #386 (comment).

@lpinca
Copy link
Collaborator

lpinca commented Aug 18, 2020

This seems a duplicate of #386.

@mikeacre
Copy link
Author

I dont think its a duplicate.. I am able to access the API with an HTTP call using those credentials. But when I try to use the SDK i get the error, same key and secret from the private app.

But I was also trying to understand how to connect a custom app, and that is why I created the access key... which also didnt work with the sdk it seemed.

@lpinca
Copy link
Collaborator

lpinca commented Aug 18, 2020

Try to run this:

const Shopify = require('shopify-api-node');

const shopify = new Shopify({
  shopName: 'quuz',
  apiVersion: '2020-07',
  apiKey: 'b68c3a4bf44cd240511e5f4114f0be4d',
  password: '2d170167fc62551920ca60fe40a5e548'
});

(async function () {
  const shop = await shopify.shop.get();
  console.log(shop);
})().catch(console.error);

does it work on your machine? If so replace the shopName, apiKey, and password with your data and check if it works.

@mikeacre
Copy link
Author

mikeacre commented Aug 19, 2020

HTTPError: Response code 401 (Unauthorized)
at onResponse (/mnt/c/sites/unalome/console/node_modules/got/dist/source/as-promise/index.js:142:28)
at runMicrotasks ()
at processTicksAndRejections (internal/process/task_queues.js:97:5)
=> awaited here:
at Function.Promise.await (/home/mikeacre/.meteor/packages/promise/.0.11.2.iatt1i.xuutn++os+web.browser+web.browser.legacy+web.cordova/npm/node_modules/meteor-promise/promise_server.js:56:12)
at imports/startup/server/shopify.js:33:19
at /home/mikeacre/.meteor/packages/promise/.0.11.2.iatt1i.xuutn++os+web.browser+web.browser.legacy+web.cordova/npm/node_modules/meteor-promise/fiber_pool.js:43:40 {
name: 'HTTPError',
code: undefined,
timings: {
start: 1597841516762,
socket: 1597841516763,
lookup: 1597841516779,
connect: 1597841516808,
secureConnect: 1597841516828,
upload: 1597841516828,
response: 1597841516940,
end: 1597841516943,
error: undefined,
abort: undefined,
phases: {
wait: 1,
dns: 16,
tcp: 29,
tls: 20,
request: 0,
firstByte: 112,
download: 3,
total: 181
}
}
}

@mikeacre
Copy link
Author

When I load in my credentials I go back to the

RequestError: Unexpected token < in JSON at position 0

@lpinca
Copy link
Collaborator

lpinca commented Aug 19, 2020

Was it run on Meteor? That stack trace is super weird. Can you try with plain Node.js?

@lpinca
Copy link
Collaborator

lpinca commented Aug 19, 2020

@mikeacre
Copy link
Author

Hey @lpinca you are right, I am using Meteor. I ran this in plain Node and it did work without an issue.

@mikeacre
Copy link
Author

It also works with the access token this way, I guess the issue must be with Meteor or another package I have installed. I will have to play with it. Do you have any ideas?

@lpinca
Copy link
Collaborator

lpinca commented Aug 19, 2020

I have no clue. The package was designed for Node.js.

@mikeacre mikeacre changed the title Receiving error while trying to use SDK Receiving error while trying to use SDK with Meteor Aug 19, 2020
@jankapunkt
Copy link

jankapunkt commented Aug 20, 2020

@mikeacre when putting the example code from above in a new Meteor project in server/main.js then there is no error thrown.

import Shopify from 'shopify-api-node'

const shopify = new Shopify({
  shopName: 'your-shop-name',
  apiKey: 'your-api-key',
  password: 'your-app-password'
})
I20200820-09:19:45.633(2)? Shopify {          
I20200820-09:19:45.654(2)?   _events: [Object: null prototype] {},
I20200820-09:19:45.654(2)?   _eventsCount: 0,
I20200820-09:19:45.654(2)?   _maxListeners: undefined,
I20200820-09:19:45.654(2)?   options: {
I20200820-09:19:45.655(2)?     timeout: 60000,
I20200820-09:19:45.655(2)?     shopName: 'your-shop-name',
I20200820-09:19:45.655(2)?     apiKey: 'your-api-key',
I20200820-09:19:45.655(2)?     password: 'your-app-password'
I20200820-09:19:45.655(2)?   },
I20200820-09:19:45.655(2)?   callLimits: { remaining: undefined, current: undefined, max: undefined },
I20200820-09:19:45.655(2)?   callGraphqlLimits: {
I20200820-09:19:45.655(2)?     restoreRate: undefined,
I20200820-09:19:45.655(2)?     remaining: undefined,
I20200820-09:19:45.655(2)?     current: undefined,
I20200820-09:19:45.655(2)?     max: undefined
I20200820-09:19:45.656(2)?   },
I20200820-09:19:45.656(2)?   baseUrl: { hostname: 'your-shop-name.myshopify.com', protocol: 'https:' },
I20200820-09:19:45.656(2)?   baseHeaders: {
I20200820-09:19:45.656(2)?     'User-Agent': 'shopify-api-node/3.4.3',
I20200820-09:19:45.656(2)?     Authorization: 'Basic eW91ci1hcGkta2V5OnlvdXItYXBwLXBhc3N3b3Jk'
I20200820-09:19:45.656(2)?   },
I20200820-09:19:45.656(2)?   [Symbol(kCapture)]: false
I20200820-09:19:45.656(2)? }

Can you please add a minimal reproduction project (with some fake API credentials, that can be replaced with some real ones) where you can reproduce the erorr(s)?

Usually Meteor = Node so there should be no issue with integrating anything that is designed for node.
After getting your issue resolved, we can make a wiki entry in this project, helping others to integrate Shopify with Meteor.

@mikeacre
Copy link
Author

OK Well I figured it out... it is the inclusion of the mailgun-js api. Once I removed that package it worked no problem. I guess I need to look into mailgun now.

const mailgun = require("mailgun-js");

@lpinca
Copy link
Collaborator

lpinca commented Aug 20, 2020

On #386 the same issue is caused by @sentry/node.

@lpinca
Copy link
Collaborator

lpinca commented Aug 21, 2020

Here is a test case with only mailgun-js and got.

const mailgun = require('mailgun-js');
const got = require('got');

got(
  {
    hostname: 'httpbin.org',
    protocol: 'https:',
    pathname: '/json'
  },
  {
    responseType: 'json',
    retry: 0
  }
)
  .then(function (response) {
    console.log(response.body);
  })
  .catch(console.error);

See also mailgun/mailgun-js-boland#265.

I think this happens because mailgun-js uses proxy-agent which in turn uses agent-base@4.3.0 that monkey patches https.request() in an incompatible way.

There is not much we can do about it. Can you switch to https://www.npmjs.com/package/mailgun.js?

@cluelesscamel
Copy link

Had the same issue with shopify-api-node while using mailgun-js (which was last updated 2 years ago), moving over to an even more bloated package (mailgun.js) that is less maintained really wasn't an option.
After looking around a little bit I stumbled upon nodemailer and its well maintained mailgun-nodemailer-transport package.
My Mailgun and shopify API integrations are now both up-to-date and fully working.

@lpinca
Copy link
Collaborator

lpinca commented Oct 23, 2020

I'm closing as switching to nodemailer is a viable solution as per #406 (comment).

@lpinca lpinca closed this as completed Oct 23, 2020
@satyavh
Copy link

satyavh commented Dec 22, 2020

node-mailjet has the same issue. As mailjet is owned by mailgun it probably shares the same code.

But it's interesting that this shopify package is conflicting with a couple of packages. Sounds like something is not right

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

6 participants