Why am I getting "status code 401" attempting patch app_metadata?

I’m banging my head against the wall trying to solve issuing a patch request after getting an access token. This same access token works for get requests to https://${appDomain}/api/v2/users/${userid}. But it fails with “Request failed with status code 401” when trying to use it to patch app_metadata.

Using NodeJS and Axios.

     axios
      .post(`https://${appDomain}/oauth/token`, {
        grant_type: 'client_credentials',
        client_id: clientId,
        client_secret: clientSecret,
        audience: `https://${appDomain}/api/v2/`,
      })
      .then(({ data: { access_token, token_type } }) => {
        const jwt = jwtDecode(access_token)

        axios
          .patch(`https://${appDomain}/api/v2/users/${userid}`, {
            data: {
              app_metadata: { stripeCustomerId: customer.id },
            },
            headers: {
              Authorization: `${token_type} ${access_token}`,
            },
          })
          .then(({ data }) => {
            console.warn('patch response', data)
          })
          .catch((err) => {
            console.error('patch error', err) // <--- ERROR 401 happens here
            res.send(err)
          })
      })
      .catch((err) => {
        console.error('token error', err)
        res.send(err)
      })

After shadow boxing documentation I discovered a syntax error in my axios.patch call. Format should have been the following, which fixed my problems. I was passing data:{…} when it should have been like this:

              .patch(
            `https://${appDomain}/api/v2/users/${userid}`,
            {
              app_metadata: { stripeCustomerId: customer.id },
            },
            {
              headers: {
                Authorization: `${token_type} ${access_token}`,
              },
            }
          )
1 Like

Glad you have figured it out and thanks for sharing with the rest of community!

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.