与 MSAL(Azure AD) 和 GoLang 的集成:未定义

Integration with MSAL(Azure AD) and GoLang: undefined

我正在将我的 Go Gin 应用程序与公司的 Azure AD 集成,参考:https://github.com/AzureAD/microsoft-authentication-library-for-go,但是,它在开始步骤中失败了。

我得到了"undefined: publicClientApp"这一行:

publicClientapp, err := public.New("client_id", public.WithAuthority("https://login.microsoftonline.com/Enter_The_Tenant_Name_Here"))

有谁知道为什么会这样? Go和AAD有成功的例子吗?

官方的例子说的不是很清楚,错误太多,搞得我很困惑。 谢谢!

希望您在使用前声明了变量。 请检查客户端 ID 的 appId 值是否正确,或者尝试从环境变量或 json 文件中获取。

如果声明的变量在赋值后没有在任何地方使用,就会发生这种类型的错误。 请在检查所有参数是否像您参考中的步骤 2 中那样 correct.something 后检查是否使用了 publicclientapp。

var userAccount public.Account
accounts := publicClientApp.Accounts()

当我们检查 nil 值的错误时,类似于尝试检查客户端应用程序是否具有某些值。并查看是否缺少任何额外参数或未缓存。

if err != nil {
  // Complain or something here
}

publicClientapp, err := public.New(config.ClientID, public.WithCache(cacheAccessor), public.WithAuthority(config.Authority))
    if err != nil {
        //do something
    }

参考文献:

  1. go/azure-sdk-authentication
  2. microsoft-authentication-library-for-go
  3. go/azure-sdk-authorization

这似乎工作得很好。

package main

import (
    "fmt"

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

func main() {
    publicClientApp, err := public.New("client_id", public.WithAuthority("https://login.microsoftonline.com/Enter_The_Tenant_Name_Here"))
    if err != nil {
        panic(err)
    }
    fmt.Println(publicClientApp)
}

https://go.dev/play/p/WERsd46004p

您显示了错误消息 "undefined: publicClientApp",但您将客户端声明为 publicClientapp。如果您之后尝试使用 publicClientApp,您将看到该错误,因为它尚未定义。 (注意大写的 A)。