Home > other >  Integration with MSAL(Azure AD) and GoLang: undefined
Integration with MSAL(Azure AD) and GoLang: undefined

Time:02-27

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!

CodePudding user response:

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

CodePudding user response:

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

  • Related