skip to Main Content

I’ve made progress (I think) with getting delegate access scope working on my custom app for my store. However, I keep getting this error:

Error: GraphQL Error (Code: 422):
{"response":{"errors":{"delegate_access_scope":["The access scope
can’t be empty."]},

To simply get this working I’m using the example from the docs:

const accessToken = gql`mutation {
    delegateAccessTokenCreate(input: { delegateAccessScope: ["write_orders" ], expiresIn: 3600 }){
      delegateAccessToken {
        accessToken
      }
      shop {
        id
      }
      userErrors {
        field
        message
      }
    }
  }`

I’m kind of at my wits end here as I finally realized that the delegate.json actually needs the ADMIN key rather than the storefront even though I want to use this with the storefront API (which is weird). All I want to be able to do is create a new customer with a password via this API.

Thanks in advance.

2

Answers


  1. Chosen as BEST ANSWER

    Ok, so I figured it out. I don't actually want a delegateAccessToken to create a user but to have a Storefront API Access Token. This may get a bit confusing but you use your Admin Access Token to request a storefront access token like so (I use graphql-request on npm):

     const accessToken = gql`mutation storefrontAccessTokenCreate($input: StorefrontAccessTokenInput!) {
        storefrontAccessTokenCreate(input: $input) {
          shop {
            id
          }
          storefrontAccessToken {
            accessToken
          }
          userErrors {
            field
            message
          }
        }
      }`
    
      const accessTokenVars = {
        "input": {
          "title": "my-access-token"
        }
      }
    
      const queryAccessTokenData = await adminGraphQLClient.request(accessToken, accessTokenVars);
    

    From here you now have a Storefront Access Token and can make a call via Oauth to create a customer like so:

    const shopifyUserQuery = gql`mutation customerCreate($input: CustomerCreateInput!) {
        customerCreate(input: $input) {
          customer {
            id
            firstName
            lastName
            acceptsMarketing
          }
          customerUserErrors {
            field
            message
            code
          }
        }
      }`
    
      const shopifyUserVars = 
      {
        "input": {
          "firstName": values.firstName,
          "lastName": values.lastName,
          "email": values.email,
          "password": values.password,
          "acceptsMarketing": false,
        }
      }
    
      // We need to switch the default storefront headers over to our private API key
      sfGraphQLClient.headers = {
          'X-Shopify-Storefront-Access-Token': queryAccessTokenData.storefrontAccessTokenCreate.storefrontAccessToken.accessToken,
          'Content-Type': 'application/json'
      }
    
      const shopifyCreateCustData = await sfGraphQLClient.request(shopifyUserQuery, shopifyUserVars);
    

    This will now create the user using the Storefront API.


  2. Maybe you just need to change delegateAccessScope: to delegate_access_scope per doc, https://shopify.dev/apps/auth/oauth/delegate-access-tokens/create

    {
    "delegate_access_scope": [
    "write_orders"
    ],
    "expires_in": 3600
    }

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search