Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

authenticate.webhook() admin gives 401 GraphQL Client: Unauthorized

authenticate.webhook() admin gives 401 GraphQL Client: Unauthorized

JedLT
Shopify Partner
5 1 0

I am using the latest Remix template (3.3.2) and trying to make a Admin GraphQL request after receiving a webhook. However the graphql request returns a 401 - GraphQL Client: Unauthorized.

 

 

 

import type { ActionFunctionArgs } from "@remix-run/node";
import { authenticate } from "../shopify.server";

export const action = async ({ request }: ActionFunctionArgs) => {
  const { payload, admin } = await authenticate.webhook(request);

  if (admin) {
    try {
        const response = await admin.graphql(
            `#graphql
                query getDiscountData($id: ID!) {
                    discountNode(id: $id) {
                        id
                    }
                }
            `,
            {
                variables: {
                    id: payload.admin_graphql_api_id
                }
            }
        );
        const data = await response.json();
        console.log(data);
    } catch (error: any) {
        const errorObj = await error.json()
        console.log(errorObj.errors)
    }
  }

  return new Response();
};

 

 

 

I'm unsure where I went wrong.

Replies 3 (3)

SomeUsernameHe
Shopify Partner
517 57 110

Did you setup the correct API access scopes? Log in to your partner dashboard, go to the app section and select the app you are working on. Then head down to the API access section and check the configured scopes. It should look something like this:

7f5e98149aac45f99aae5231ea8cc312.png

Have I helped? Consider putting coffee in my mouth!
Buy Me a Coffee
JedLT
Shopify Partner
5 1 0

I currently have read_discounts access scope as the only scope (which I believe is the only one I need for that call) and confirmed it by looking in the app in my partner dashboard.

SomeUsernameHe
Shopify Partner
517 57 110
import type { ActionFunctionArgs } from "@remix-run/node";
import { authenticate } from "../shopify.server";

export const action = async ({ request }: ActionFunctionArgs) => {
  const { payload, admin } = await authenticate.webhook(request);

  if (admin) {
    try {
        const response = await admin.graphql(
            `#graphql
                query getDiscountData($id: ID!) {
                    discountNode(id: $id) {
                        id
                        discount {
                            ... on DiscountCodeBasic {
                                title
                            }
                            ... on DiscountCodeBxgy {
                                title
                            }
                            ... on DiscountCodeFreeShipping {
                                title
                            }
                            ... on DiscountAutomaticApp {
                                title
                            }
                            ... on DiscountAutomaticBasic {
                                title
                            }
                            ... on DiscountAutomaticBxgy {
                                title
                            }
                            ... on DiscountAutomaticFreeShipping {
                                title
                            }
                        }
                    }
                }
            `,
            {
                variables: {
                    id: payload.admin_graphql_api_id
                }
            }
        );
        const data = await response.json();
        console.log(data);
    } catch (error: any) {
        const errorObj = await error.json()
        console.log(errorObj.errors)
    }
  }

  return new Response();
};

Even if you use the above code do you still get the same error? Also, you can always try to use shopify app deploy and see if that helps.

The last thing you can do is verify that the appropriate authorization headers are included when making the GraphQL request. The admin.graphql() client should add these automatically if configured correctly. However, you might need to ensure the access token has not expired or is valid.

Have I helped? Consider putting coffee in my mouth!
Buy Me a Coffee