【问题标题】:Why do I get Error 401 trying to patch Auth0 app_metadata with the Management API?为什么我在尝试使用 Management API 修补 Auth0 app_metadata 时收到错误 401?
【发布时间】:2020-11-16 15:09:07
【问题描述】:

我正在努力解决在获得访问令牌后发出补丁请求的问题。同样的访问令牌适用于对 https://${appDomain}/api/v2/users/${userid} 的获取请求。但是在尝试使用它来修补 app_metadata 时,它会失败并显示“请求失败,状态码为 401”。

使用 NodeJS 和 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)
          })

【问题讨论】:

    标签: node.js authentication oauth axios auth0


    【解决方案1】:

    在获得影子拳击文档后,我在 axios.patch 调用中发现了一个语法错误。格式应该如下,它解决了我的问题。我在传递 data:{...} 时应该是这样的:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-15
      • 2015-04-25
      • 2021-03-27
      • 1970-01-01
      • 2022-01-26
      • 2021-06-21
      • 2016-04-03
      • 2021-08-04
      相关资源
      最近更新 更多