Reputation: 14393
Sorry for cross posting this issue here in SO
So I follow the "https://docs.shopify.com/api/authentication/oauth"; guide and successfully proceed to "Making authenticated requests" part, then I stuck at there. Here is my code (in 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());
}
I am using apache httpclient (v4.3.1) to post my request to Shopify. The problem I've found is I always get HTTP/1.1 422 Unprocessable Entity
, I don't know where I am wrong.
If I use postman to test with exactly the same payload, url and access token, I get the following response:
{
"errors": {
"script_tag": "Required parameter missing or invalid"
}
}
Anyone can help?
Update
I got content of the 422
response:
{"errors":{"src":["is invalid"]}}
Upvotes: 2
Views: 869
Reputation: 2166
I had the same problem, using curl. The missing ingredient was setting the Content-Type for the request:
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
You should be able to do the same with Postman: https://www.getpostman.com/docs/requests#headers
Upvotes: 3
Reputation: 245
I got a similar error running the shopify api natively without a wrapper. I ended up using a node module that helped. I know you're writing in Java so not sure if they have a similar wrapper.
It might help to take a look at how they implement pinging the shopify api in node.
https://github.com/christophergregory/shopify-node-api
Upvotes: 1