簡體   English   中英

Auth0“未找到服務”錯誤

[英]Auth0 "service not found" error

我正在嘗試使用 Auth0 頒發 JWT 令牌以訪問我的 API(以便 Auth0 處理所有 OAuth 和安全問題等,而我的 API 只需要檢查令牌)。 當我嘗試測試客戶端的授權代碼流以接收訪問令牌(使用 Node + Express)時,會發生以下情況:

  • 授權代碼請求工作正常,客戶端被重定向回我的 redirect_uri 並將代碼附加到查詢中。 都好。

  • 然后令牌請求總是失敗。 如果我包含audience參數,請求將返回一個access_denied錯誤,其中包含以下詳細信息: Service not found: {the audience parameter} ,無論我為audience參數設置了什么值。

  • 如果我不包含audience參數, server_error收到server_error消息,並顯示Service not found: https://oauth.auth0.com/userinfo

我已經檢查了每個 Auth0 設置並徹底閱讀了每個文檔頁面,但到目前為止沒有任何效果。 我還在 Auth0 的 API 調試器中測試了授權代碼流程,它運行良好。 我的測試遵循完全相同的參數,但仍然收到請求令牌的錯誤。 我正在本地主機上測試。 客戶端憑據和隱式流工作正常。

這是我創建的一個測試端點,它從 Auth0 檢索授權代碼:

 const qs = require('querystring'); const getCode = (req, res) => { const params = { audience, // the value of the API Audience setting for the client client_id, // the client ID redirect_uri, // the redirect_uri, which is also listed in the Allowed Callback URLs field response_type: `code`, scope: `offline_access open` // ask to return ID token and refresh token, state: `12345`, }; const authDomain = `mydomain.auth0.com/oauth`; res.redirect(`${authDomain}/oauth/authorize?${qs.stringify(params)}`); };

redirect_uri然后重定向到以下端點,我在那里請求訪問令牌:

 const https = require('https'); const callback = (req, res) => { const body = { client_id, client_secret, code: req.query.code, grant_type: `authorization_code`, redirect_uri, // same value as provided during the code request }; const opts = { headers: { 'Content-Type': `application/json` }, hostname: `mydomain.auth0.com`, method: `POST`, path: `/oauth/token`, }; const request = https.request(opts, response => { let data = ``; response.on(`data`, chunk => { data += chunk; }); response.on(`error`, res.send(err.message)); response.on(`end`, () => res.json(JSON.parse(data))); // this executes, but displays the error returned from Auth0 }); request.on(`error`, err => res.send(err.message)); request.end(JSON.stringify(body), `utf8`); };

關於我可能做錯了什么的任何建議?

問題是我在 Auth0 調用了錯誤的 URL。 我錯誤地認為授權和令牌端點都以/oauth開頭,而實際上授權端點只是/authorize ,而令牌端點是/oauth/authorize 更正我的代碼中的 URL 解決了這個問題。

我的解決方案是找不到api的標識符。 如果它不准確,它就不會找到它。 我在我的“觀眾”上有一個額外的反斜杠,其中標識符沒有。 很容易出錯,但錯誤在 Auth0 中不是很清楚。

就我而言,我使用的是 auth0 反應鈎子。 所以示例代碼如下所示:

const getUserMetadata = async () => {
        const domain = process.env.REACT_APP_AUTH0_DOMAIN

        try {
            const accessToken = await getAccessTokenSilently({
                audience: `https://${domain}/api/v2/`,
                scope: 'read:current_user',
            })
            console.log('accessToken', accessToken)
            localStorage.setItem('access_token', accessToken)

            setUserAuthenticated(true)
        } catch (e) {
            console.log('error in getting access token', e.message)
        }
    }

我對此的解決方案是在audience字段中默認使用 Auth0 Audience 值

 const getUserMetadata = async () => {
    const auth0audience = process.env.REACT_APP_AUTH0_AUDIENCE

    try {
        const accessToken = await getAccessTokenSilently({
            audience: auth0audience,
            scope: 'read:current_user',
        })
        console.log('accessToken', accessToken)
        localStorage.setItem('access_token', accessToken)

        setUserAuthenticated(true)
    } catch (e) {
        console.log('error in getting access token', e.message)
    }
}

因為它在配置自定義域的 auth0 文檔中說明,您需要默認使用 API 受眾

來源 - https://auth0.com/docs/brand-and-customize/custom-domains/configure-features-to-use-custom-domains

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM