【问题标题】:Shopify integration: trouble on making authenticated requestsShopify 集成:发出经过身份验证的请求时遇到问题
【发布时间】:2015-03-11 22:24:13
【问题描述】:

很抱歉在 SO 中交叉发帖 this issue

所以我跟着“https://docs.shopify.com/api/authentication/oauth”;指导并成功进入“发出经过身份验证的请求”部分,然后我就卡在那里了。这是我的代码(Java):

String payload = "{\"script_tag\":{\"src\":\"http:\\/\\/localhost:8080\\/js\\/shopify.js\",\"event\":\"onload\"}}";
String url = "https://pixolut-shopify-test.myshopify.com/admin/script_tags.json";
HttpPost post = new HttpPost(url);
post.setHeader("X-Shopify-Access-Token", accessToken);
post.setEntity(new StringEntity(payload, ContentType.APPLICATION_JSON));
HttpResponse resp = HttpClientBuilder.create().build().execute(post);
StatusLine statusLine = resp.getStatusLine();
if (statusLine.getStatusCode() != 200) {
    throw new RuntimeException("Error inject script tag: %s", statusLine.getReasonPhrase());
}

我正在使用 apache httpclient (v4.3.1) 将我的请求发布到 Shopify。我发现的问题是我总是得到HTTP/1.1 422 Unprocessable Entity,我不知道我错在哪里。

如果我使用 postman 使用完全相同的有效负载、url 和访问令牌进行测试,我会得到以下响应:

{
    "errors": {
        "script_tag": "Required parameter missing or invalid"
    }
}

有人可以帮忙吗?

更新

我收到了422 回复的内容:

{"errors":{"src":["is invalid"]}}

【问题讨论】:

    标签: json oauth shopify apache-httpclient-4.x apache-commons-httpclient


    【解决方案1】:

    我有同样的问题,使用 curl。缺少的成分是为请求设置 Content-Type:

    curl -H "X-Shopify-Access-Token: {token}" -H "Content-Type: application/json" -d "{\"script_tag\":{\"event\":\"onload\",\"src\":\"{script_uri}\"}}" https://{shop}.myshopify.com/admin/script_tags.json
    

    你应该可以对 Postman 做同样的事情:https://www.getpostman.com/docs/requests#headers

    【讨论】:

      【解决方案2】:

      我在没有包装器的情况下本机运行 shopify api 时遇到了类似的错误。 我最终使用了一个有帮助的节点模块。我知道你正在用 Java 编写,所以不确定他们是否有类似的包装器。

      看看他们如何在 node 中实现对 shopify api 的 ping 操作可能会有所帮助。

      https://github.com/christophergregory/shopify-node-api

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-23
        • 1970-01-01
        • 1970-01-01
        • 2019-03-29
        • 2011-11-01
        • 1970-01-01
        相关资源
        最近更新 更多