【问题标题】:Invalid Grant (Error Code 70000) refreshing token Azure AD无效授权(错误代码 70000)刷新令牌 Azure AD
【发布时间】:2021-07-15 05:30:41
【问题描述】:

我正在使用 Azure AD 登录来获取访问令牌,并能够使用其 API REST 向 SharePoint Online 发出请求。

我能够获取此访问令牌,但是当我尝试获取刷新令牌时,出现错误。

此时我正在使用 Postman 进行测试。

我正在做以下事情:

  • 我开始调用下一个 URL 来获取代码:
https://login.microsoftonline.com/{tenant}/oauth2/authorize?client_id={client_id}&client_secret={client_secret}&response_type=code
  • 收到代码后,我会执行以下 POST:
POST /{租户}/oauth2/token HTTP/1.1 主机:login.microsoftonline.com 用户代理:PostmanRuntime/7.13.0 接受: */* 缓存控制:无缓存
grant_type:authorization_code
client_id:{client_id}
client_secret:{client_secret}
代码:{code_generated_previous_request}
redirect_uri:https://myapplication.com
资源:https://{mycompany}.sharepoint.com/
  • 我收到此回复:
{ "token_type": "承载者", "范围": "AllSites.FullControl Directory.ReadWrite.All Group.ReadWrite.All Sites.FullControl.All Sites.Read.All User.Invite.All User.Read.All User.ReadWrite.All", "expires_in": "3599", "ext_expires_in": "3599", "expires_on": "1559291698", "not_before": "1559287798", "资源": "https://{mycompany}.sharepoint.com/", "access_token": "XXXXXXXX...", "refresh_token": "YYYYYYYY...", "id_token": "ZZZZZZZZ..." }
  • 最后,我尝试通过以下 POST 刷新令牌:
POST /{租户}/oauth2/token HTTP/1.1
主机:login.microsoftonline.com
用户代理:PostmanRuntime/7.13.0
接受: */*
缓存控制:无缓存
grant_type:refresh_token
client_id:{client_id}
client_secret:{client_secret}
refresh_token:YYYYYYYY...
redirect_uri:https://myapplication.com
  • 但是在这一步,我得到一个错误:
“错误”:“invalid_grant”, "error_description": "AADSTS70000: 提供的授权无效或格式错误。\r\n跟踪 ID: XXXX\r\n相关 ID: XXXXX\r\n时间戳: 2019-05-31 09:35:39Z", “错误代码”:[ 70000 ], “时间戳”:“2019-05-31 09:35:39Z”, "trace_id": "XXXX", “correlation_id”:“XXXX” }

我已经检查了 URL 编码,没有 URL 编码,删除了 client_secret 和 redirect_uri 参数和其他东西,但我总是得到同样的错误。当然,我犯了一个愚蠢而明显的错误,但我看不出是哪一个。

【问题讨论】:

    标签: azure post login oauth-2.0 azure-active-directory


    【解决方案1】:

    您似乎正在尝试使用authorization coderefresh_tokengrant_type 更新您的访问令牌。

    我已经部分重现了您遇到的错误。如下图:

    可能的错误原因:

    1. 虽然具体原因不明!
    2. 我假设您粘贴的刷新令牌无效。在这种情况下 您可能会遇到此错误。
    3. 你必须粘贴你之前得到的refresh_token 步骤原样。确保你确实做到了。

    错误解决:

    我正在分享实现您目标的确切方法。请看下面的步骤

    步骤:1

    我在这种情况下使用PostMan。请启动 PostMan 并单击 Authorization 选项卡并选择 Type 作为 OAuth 2.0 最后单击 Get New Access Token。请看下面的屏幕截图:

    步骤:2

    当您单击Get New Access Token 时,将提示新窗口,如下图所示。使用您的凭据填写此内容。

    请求格式:

    https://login.microsoftonline.com/{TenantId/Name}/oauth2/authorize?client_id={applicationId}&response_type=code&redirect_uri={yourURI}&response_mode=query&scope={yourScope}
    

    当您单击Request Token 按钮时,您将出现登录窗口。使用您的凭据登录。

    步骤:3

    在登录前打开您的postman console,如下所示:

    登录成功后。你会得到你的代码。

    步骤:4

    登录成功后转到postman consolerequest body段。只需打开它并复制您的代码。请参阅下面的屏幕截图:

    步骤:5

    复制您的code 并粘贴到authorization_code 令牌请求code 文本框部分。你会得到你的access_tokenrefresh_tokenid_token

    请求格式:

    client_id:{ApplicationId}

    范围:{YourTokenScope}

    redirect_uri:{YourAppURI}

    grant_type:authorization_code

    client_secret:{YourApplicationSecret}

    代码:{CodeOfPreviousStep}

    就像下面这样:

    步骤:6

    此步骤从上一阶段 (step 5) 复制您的 refresh token 并在请求 grant_type:refresh_token 请求时粘贴到 refresh_token 文本框旁边。作为回应,您将获得新的 access_tokenrefresh_token

    请求格式:

    client_id:{ApplicationId}

    范围:{YourTokenScope}

    redirect_uri:{YourAppURI}

    grant_type:refresh_token

    client_secret:{YourApplicationSecret}

    refresh_token:{上一步的refresh_token}

    请看下面的屏幕截图:

    注意

    1. 确保您正确应对coderefresh token
    2. Scope 可用于令牌更新。您可以在没有范围的情况下更新令牌,因为您的刷新令牌已经包含您的范围。

    【讨论】:

    • 在步骤中有问题,在 windows 上工作的相同代码在 ubuntu 中不工作。
    • 此解决方案是基于未在 ubuntu 上测试的 windows 提供的。如果您相应地设置了 azure 凭据,它应该可以在任何地方使用。
    猜你喜欢
    • 1970-01-01
    • 2019-10-17
    • 2017-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-07
    • 2018-08-20
    相关资源
    最近更新 更多