Solved

Create metafield for page using REST API

epetroel
Shopify Partner
4 0 0

I'm trying to create a metafield on a page from a private app using the REST API, but getting back an unexpected HTML response that appears to be an attempt to redirect to an OAuth authentication page.

I'm doing a POST to /api/2021-10/pages/{id}/metafields.json where {id} is the id of the page I'm trying to add the metafield to.  I'm using HTTP basic authentication.

Here's the data I'm posting:

{
    "metafield": {
        "namespace": "test",
        "key": "test",
        "value": 25,
        "owner_id": 90910130356,    
        "owner_resource": "page",
        "type": "json"
    }
}

 

And here's the response I'm getting back:

<html>

<body>
	<noscript>
		<a
			href="https://accounts.shopify.com/oauth/authorize?client_id=7ee65a63608843c577db8b23c4d7316ea0a01bd2f7594f8a9c06ea668c1b775c&amp;destination_uuid=850a687c-9452-44f8-8029-c0c5a9272e0f&amp;nonce=635792b1cb47b39bbd7561f29cac01e6&amp;prompt=merge&amp;redirect_uri=https%3A%2F%2Fcql-dev-01.myshopify.com%2Fadmin%2Fauth%2Fidentity%2Fcallback&amp;response_type=code&amp;scope=email%20https%3A%2F%2Fapi.shopify.com%2Fauth%2Fdestinations.readonly%20openid%20profile%20https%3A%2F%2Fapi.shopify.com%2Fauth%2Fpartners.collaborator-relationships.readonly%20https%3A%2F%2Fapi.shopify.com%2Fauth%2Fbanking.manage%20https%3A%2F%2Fapi.shopify.com%2Fauth%2Fshopify-chat.admin.graphql%20https%3A%2F%2Fapi.shopify.com%2Fauth%2Fflow.workflows.manage%20https%3A%2F%2Fapi.shopify.com%2Fauth%2Forganization-identity.manage&amp;state=f0b8446b73f90dbef792a4ef208ad181&amp;ui_locales=en&amp;ux=shop">Continue</a>
	</noscript>

	<script type="text/javascript" defer>
		window.location = "https:\/\/accounts.shopify.com\/oauth\/authorize?client_id=7ee65a63608843c577db8b23c4d7316ea0a01bd2f7594f8a9c06ea668c1b775c\u0026destination_uuid=850a687c-9452-44f8-8029-c0c5a9272e0f\u0026nonce=635792b1cb47b39bbd7561f29cac01e6\u0026prompt=merge\u0026redirect_uri=https%3A%2F%2Fcql-dev-01.myshopify.com%2Fadmin%2Fauth%2Fidentity%2Fcallback\u0026response_type=code\u0026scope=email%20https%3A%2F%2Fapi.shopify.com%2Fauth%2Fdestinations.readonly%20openid%20profile%20https%3A%2F%2Fapi.shopify.com%2Fauth%2Fpartners.collaborator-relationships.readonly%20https%3A%2F%2Fapi.shopify.com%2Fauth%2Fbanking.manage%20https%3A%2F%2Fapi.shopify.com%2Fauth%2Fshopify-chat.admin.graphql%20https%3A%2F%2Fapi.shopify.com%2Fauth%2Fflow.workflows.manage%20https%3A%2F%2Fapi.shopify.com%2Fauth%2Forganization-identity.manage\u0026state=f0b8446b73f90dbef792a4ef208ad181\u0026ui_locales=en\u0026ux=shop";
	</script>
</body>

</html>

 

If it helps, I can GET the metafields for a page just fine (it correctly returns a json content type with an empty array).  Attempting to create a metafield fails as described above though.

Accepted Solution (1)
GrahamS
Shopify Staff (Retired)
193 36 53

This is an accepted solution.

Hey @epetroel ,

How are you managing cookies? Making a post request (doesn't happen with get) and receiving a response with a login url/window is a common outcome for users who attempt to make a request with basic http auth and include cookies, if that's something you haven't checked yet

See this post

To learn more visit the Shopify Help Center or the Community Blog.

View solution in original post

Replies 5 (5)

Jason
Shopify Expert
11190 225 2283

Where does this call come from? I really hope that it's not the storefront via some JS - that would be really bad.
I would assume your request has cookies in it so those are blocked.

Did you test the same call in a different stack (such as desktop postman - making sure cookies are not sent there too!) 

★ I jump on these forums in my free time to help and share some insights. Not looking to be hired, and not looking for work. http://freakdesign.com.au ★
epetroel
Shopify Partner
4 0 0

Hi Jason, I'm making the request via a server side application (Python) - I get the same results via Postman as well.

Jesse_Vogt
Shopify Staff
14 2 10

Hi @epetroel,

Jesse from the Metafields team here. The only way I am able to reproduce your issue is if I leave off the trailing ".json" - could you double check that you are attempting to POST to "/2021-10/pages/PAGE_ID/metafields.json"? Also double check that your app has Read and Write access to Store content.

To learn more visit the Shopify Help Center or the Community Blog.

GrahamS
Shopify Staff (Retired)
193 36 53

This is an accepted solution.

Hey @epetroel ,

How are you managing cookies? Making a post request (doesn't happen with get) and receiving a response with a login url/window is a common outcome for users who attempt to make a request with basic http auth and include cookies, if that's something you haven't checked yet

See this post

To learn more visit the Shopify Help Center or the Community Blog.

epetroel
Shopify Partner
4 0 0

Cookies were the issue, not sure how Postman ended up with cookies, but after deleting those it seems to be working.  Thanks!