登录
首页 >  Golang >  Go问答

集成GoLang和MSAL(Azure AD):完全指南

来源:stackoverflow

时间:2024-02-13 15:45:24 140浏览 收藏

亲爱的编程学习爱好者,如果你点开了这篇文章,说明你对《集成GoLang和MSAL(Azure AD):完全指南》很感兴趣。本篇文章就来给大家详细解析一下,主要介绍一下,希望所有认真读完的童鞋们,都有实质性的提高。

问题内容

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

我得到了 "undefined: publicclientapp"in 这行:

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

有谁知道为什么会发生这种情况吗? go和aad有成功的例子吗?

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


正确答案


这似乎工作得很好。

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

希望您在使用之前已经声明了变量。 请检查客户端 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-authentication
  2. microsoft-authentication-library-for-go
  3. go/azure-sdk-authorization

终于介绍完啦!小伙伴们,这篇关于《集成GoLang和MSAL(Azure AD):完全指南》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~golang学习网公众号也会发布Golang相关知识,快来关注吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>