0
votes

I'm struggling to import the orders for a Shopify development store using the httr package in R. Here's what I've tried.

  1. I've created a development store and made some fake orders.
  2. Within my development store, I added a private app and generated my API key and Password
  3. Following this article, I tried implementing the following request

Code

apikey <- "foo"
pass <- "bar"

shop <- GET(
  url = "my-test-store.myshopify.com/orders.json", 
  authenticate(user = apikey, password = pass)
)

But this gives a 401 status code. However, this works but returns xml instead of json

shop <- GET(
  url = "my-test-store.myshopify.com/orders", 
  authenticate(user = apikey, password = pass)
)

How can I retrieve the results as JSON instead of XML?

Note that I can also fetch the orders using the R package shopifyr but would rather not use that package as it is no longer maintained.

2
just asking: should it be admin/orders.json? help.shopify.com/api/reference/orderchinsoon12
Shoot, that was a copy-paste error. However, I figured out what I was doing wrong. About to post an answer.Ben

2 Answers

0
votes

You're close. Try this:

library(httr)

apikey <- "foo"
pass <- "bar"

orders <- GET(
  url = "https://yourshop.myshopify.com/admin/orders.json", 
  authenticate(user = apikey, password = pass)
)

content(orders)
0
votes

Update 2019-05-13

I created an R package called shopr for querying data via the Shopify API. Fetching orders looks like this

library(shopr)

shopr_get_orders(
  shopURL = "https://my-test-store.myshopify.com", 
  APIKey = "abc123", 
  APIPassword = "def456"
)

Old Answer

Figured it out.

orders <- GET(
  url = "https://my-test-store.myshopify.com/admin/orders",
  add_headers(Accept = "application/json"),
  authenticate(user = apikey, password = pass)
)
orders

The trick was to explicitly put "https://..." in the url otherwise httr was prepending "http://" to the url, causing my 401 problem.