卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章35568本站已运行397

“租户 GUID X 的租户不存在”,对于自己的电子邮件帐户(我是唯一的成员)

“租户 guid x 的租户不存在”,对于自己的电子邮件帐户(我是唯一的成员)

php小编鱼仔在解决电子邮件帐户问题时,可能会遇到错误信息“租户 GUID X 的租户不存在”,特别是当自己是唯一成员时。这个错误信息可能会导致无法登录或访问电子邮件帐户。针对这个问题,我们有一些解决方案可以尝试,以恢复对电子邮件帐户的访问和正常使用。在下文中,我们将介绍一些可能的解决方法和步骤,希望能对您解决问题提供帮助。

问题内容

我想阅读我的个人帐户的电子邮件,但收到“租户 guid x 的租户不存在”消息。

  • 我在 https://entra.microsoft.com/ 上使用单租户创建了一个应用程序

  • 然后我授予它发送电子邮件的权限

  • 然后我创建了一个客户端密钥

  • 然后我使用此代码来获取令牌:

    import(""github.com/azuread/microsoft-authentication-library-for-go/apps/confidential")
     cred, err := confidential.newcredfromsecret("{secret value}")
     if err != nil {
         log.println(err)
         return
     }
     confidentialclient, err := confidential.new("https://login.microsoftonline.com/{tenant id}", "{client id}", cred)
     if err != nil {
         log.println(err)
         return
     }
     scopes := []string{"https://graph.microsoft.com/.default"}
     result, err := confidentialclient.acquiretokensilent(context.todo(), scopes)
     if err != nil {
         result, err = confidentialclient.acquiretokenbycredential(context.todo(), scopes)
         if err != nil {
         log.println(err)
             return
         }
     }
  • 我使用该代码成功获取了令牌

    {
         "account": {
             "additionalfields": null
         },
         "idtoken": {
             "rawtoken": "",
             "additionalfields": null
         },
         "accesstoken": "{token}",
         "expireson": "2023-12-13t14:57:09.4905758-05:00",
         "grantedscopes": [
             "https://graph.microsoft.com/.default"
         ],
         "declinedscopes": null
     }
  • 然后我获取用户 id(我是唯一的用户):

    req, err := http.newrequest("get", "https://graph.microsoft.com/v1.0/users", nil)
     if err != nil {
         log.println(err)
         return
     }
     req.header.add("authorization", "{token}")
     client := http.client{}
     resp, err := client.do(req)
     if err != nil {
         log.println(err)
         return
     }
     body, err := io.readall(resp.body)
     if err != nil {
         log.println(err)
         return
     }
  • 但是当我尝试获取电子邮件时:

    req, err := http.newrequest("get", "https://graph.microsoft.com/v1.0/users/{user_id}/messages", nil)
         if err != nil {
             log.println(err)
             return
         }
         req.header.add("authorization", "{token}")
         client := http.client{}
         resp, err := client.do(req)
         if err != nil {
             log.println(err)
             return
         }
         body, err := io.readall(resp.body)
         if err != nil {
             log.println(err)
             return
         }

我得到:

{
    "error": {
        "code": "OrganizationFromTenantGuidNotFound",
        "message": "The tenant for tenant guid '0a6ac917-332a-4f47-881e-0b35fb1b2ab5' does not exist.",
        "innerError": {
            "oAuthEventOperationId": "c096c5c9-e743-4daa-9a97-d14d915e9842",
            "oAuthEventcV": "N0nHeUJm9gwnrFZefuEA4w.1.1",
            "errorUrl": "https://aka.ms/autherrors#error-InvalidTenant",
            "requestId": "c0272999-9743-44ee-98b5-947acc52e7d8",
            "date": "2023-12-13T19:11:22"
        }
    }
}

错误上的id 0a6ac917-332a-4f47-881e-0b35fb1b2ab5是tenand id

解决方法

要阅读个人 outlook 帐户的邮件,需要切换到委托流程(例如交互流程或生成访问令牌的授权码流程)并调用 /me/ messages 端点。

注册多租户应用程序,帐户类型为“任何组织目录中的帐户(任何 microsoft entra id 租户 - 多租户)和个人 microsoft 帐户(例如 skype、xbox)“:

如果您使用交互流生成令牌,请确保启用公共客户端选项:

现在,根据您的需要在您的应用注册中添加委托类型的mail.readmail.readwrite权限:

要使用交互流生成访问令牌,您可以参考此示例go代码,然后使用它来调用 /me/messages 端点:

package public_test

import (
    "context"

    "github.com/azuread/microsoft-authentication-library-for-go/apps/public"
)

func example() {
    client, err := public.new("client_id", public.withauthority("https://login.microsoftonline.com/common"))
    if err != nil {
    }

    var result public.authresult
    scopes := []string{"https://graph.microsoft.com/.default"}

    accounts, err := client.accounts(context.todo())
    if err != nil {
        // todo: handle error
    }
    if len(accounts) > 0 {
        result, err = client.acquiretokensilent(context.todo(), scopes, public.withsilentaccount(accounts[0]))
    }
    if err != nil || len(accounts) == 0 {
        result, err = client.acquiretokeninteractive(context.todo(), scopes)
        if err != nil {
        }
    }
    _ = result.account
    _ = result.accesstoken
}

您还可以登录graph explorer< /strong> 使用该帐户并运行以下查询来获取电子邮件:

GET https://graph.microsoft.com/v1.0/me/messages

回应:

参考:

微软身份验证-library-for-go/apps/public/example_test.go 位于 main · azuread/microsoft-authentication-library-for-go · github

卓越飞翔博客
上一篇: 加载 brotli 压缩的 WASM
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码: