如何使用 Shopify 的 API 获取 Shopify 商店订单

How to fetch Shopify store orders using Shopify's API

我正在努力使用 R 中的 httr 包导入 Shopify 开发商店的订单。这是我尝试过的方法。

  1. 我创建了一个 development store 并下了一些假订单。
  2. 在我的开发商店中,我添加了一个私人应用程序并生成了我的 API 密钥和密码
  3. this article 之后,我尝试实现以下请求

代码

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

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

但这给出了 401 状态代码。但是,这有效但 returns xml 而不是 json

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

如何将结果检索为 JSON 而不是 XML?

请注意,我也可以使用 R 包 shopifyr 获取订单,但我不想使用该包,因为它已不再维护。

你很接近。试试这个:

library(httr)

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

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

content(orders)

更新2019-05-13

我创建了一个名为 shopr 的 R 包,用于通过 Shopify API 查询数据。获取订单看起来像这样

library(shopr)

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

旧答案

想通了。

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

诀窍是明确地将 "https://..." 放在 url 中,否则 httr 会在 url 前面加上 "http://",导致我的 401 问题。