Shopify集成:制作经过身份验证的请求时出现问题

时间:2015-03-11 22:24:14

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

很抱歉在SO

中交叉发布this issue

所以我按照" https://docs.shopify.com/api/authentication/oauth&#34 ;;指导并成功进入"进行经过身份验证的请求"部分,然后我卡在那里。这是我的代码(用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"]}}

2 个答案:

答案 0 :(得分:3)

我有同样的问题,使用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

你应该能够对邮递员做同样的事:https://www.getpostman.com/docs/requests#headers

答案 1 :(得分:1)

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

看看他们如何实现ping节点中的shopify api可能会有所帮助。

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