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

发布于 2025-01-09 21:04:20 字数 528 浏览 2 评论 0原文

我正在将我的 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有成功的例子吗?

官方的例子不太清楚,而且有很多错误,让我很困惑。 谢谢!

I'm integrating my Go Gin app with Azure AD for the company, ref: https://github.com/AzureAD/microsoft-authentication-library-for-go , however, it failed in the beginning step.

I got "undefined: publicClientApp"in this line:

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

Does anyone know why this happens?
Are there any successful examples for Go and AAD?

The official example is not clear and there are so many errors which quite confused me.
Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

痞味浪人 2025-01-16 21:04:20

这似乎工作得很好。

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)。

This seems to work just fine.

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

You show the error message "undefined: publicClientApp" but you are declaring the client as publicClientapp. If you attempt to use publicClientApp afterwards, you will see that error, as it has not been defined. (Note the uppercase A).

烙印 2025-01-16 21:04:20

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

如果声明的变量在分配了一些值之后没有在任何地方使用,就会发生这种类型的错误。
请在检查所有参数是否正确后检查是否使用了 publicclientapp。类似于参考文献中的步骤 2。

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-身份验证
  2. microsoft-authentication-library-放弃
  3. go/azure-sdk-授权

Hope you have declared variables before use .
Please check if the client id is given correct value of appId or try to take from environment variables or json file if present.

This type of error occurs if the declared variable is not used any where even after assigning some value .
Please check if publicclientapp is used after checking if all arguments are correct.something like in step 2 in your reference.

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

As we check error for nil value , similar to that try to check client app has some value .And see if any extra argument is missing or not cacheing.

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
    }

References:

  1. go/azure-sdk-authentication
  2. microsoft-authentication-library-for-go
  3. go/azure-sdk-authorization
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文