【问题标题】:How to fetch Shopify store orders using Shopify's API如何使用 Shopify 的 API 获取 Shopify 商店订单
【发布时间】:2018-04-03 00:59:33
【问题描述】:

我正在努力使用 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 状态码。但是,这可行,但返回 xml 而不是 json

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

如何以 JSON 而不是 XML 格式检索结果?

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

【问题讨论】:

  • 只是问:应该是 admin/orders.json 吗? help.shopify.com/api/reference/order
  • 射击,这是一个复制粘贴错误。然而,我发现我做错了什么。即将发布答案。

标签: r shopify httr


【解决方案1】:

你已经接近了。试试这个:

library(httr)

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

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

content(orders)

【讨论】:

  • 不起作用,但我想出了解决方案。很快就会发布我的答案。
【解决方案2】:

2019 年 5 月 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

诀窍是在 url 中明确放置“https://...”,否则 httr 会在 url 前添加“http://”,导致我的 401 问题。

【讨论】:

    猜你喜欢
    • 2021-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-01
    • 2015-07-07
    • 1970-01-01
    相关资源
    最近更新 更多