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

huangapple go评论110阅读模式
英文:

Integration with MSAL(Azure AD) and GoLang: undefined

问题

我正在使用Azure AD为公司集成我的Go Gin应用程序,参考链接: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!

答案1

得分: 0

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

如果声明的变量在赋值后没有被使用,就会出现这种类型的错误。
请检查publicclientapp是否在检查所有参数是否正确后被使用,类似于您参考文档中的第2步。

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

当我们检查nil值的错误时,类似地尝试检查client app是否有一些值。并查看是否缺少任何额外的参数或未缓存。

if err != nil {
  // 在这里进行投诉或其他操作
}

publicClientapp, err := public.New(config.ClientID, public.WithCache(cacheAccessor), public.WithAuthority(config.Authority))
if err != nil {
  // 做一些操作
}

参考资料:

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

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

答案2

得分: 0

这似乎工作得很好。

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

你显示了错误信息"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).

huangapple
  • 本文由 发表于 2022年2月25日 17:05:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/71263433.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定