Go调用Azure AD获取应用注册凭据时编译错误求助
问题解决:Azure AD应用注册密码凭证读取的编译错误
错误根源
app.GetPasswordCredentials()返回的是切片类型[]models.PasswordCredentialable,而非指针类型,所以不能用*进行间接引用。而GetDisplayName()、GetAppId()这类方法返回的是指针(如*string),因此可以用*解引用,这就是两者的区别。
修正后的核心代码
修改遍历应用的逻辑,正确处理密码凭证切片:
err = pageIteratorApps.Iterate(context.Background(), func(app models.Applicationable) bool { // 安全解引用基本信息(先判空避免panic) displayName := "未知名称" if app.GetDisplayName() != nil { displayName = *app.GetDisplayName() } appId := "未知ID" if app.GetAppId() != nil { appId = *app.GetAppId() } fmt.Printf("应用名称: %s | App ID: %s\n", displayName, appId) // 处理密码凭证 passwordCreds := app.GetPasswordCredentials() if len(passwordCreds) > 0 { fmt.Println(" 密码凭证:") for _, cred := range passwordCreds { credName := "无名称" if cred.GetDisplayName() != nil { credName = *cred.GetDisplayName() } keyId := "无密钥ID" if cred.GetKeyId() != nil { keyId = *cred.GetKeyId() } fmt.Printf(" - %s (密钥ID: %s)\n", credName, keyId) } } else { fmt.Println(" 无密码凭证") } // 可选:处理证书凭证(逻辑和密码凭证一致) certCreds := app.GetCertificateCredentials() if len(certCreds) > 0 { fmt.Println(" 证书凭证:") for _, cert := range certCreds { certName := "无名称" if cert.GetDisplayName() != nil { certName = *cert.GetDisplayName() } fmt.Printf(" - %s\n", certName) } } else { fmt.Println(" 无证书凭证") } fmt.Println("---") return true })
关键注意点
- 所有返回指针的字段(如
DisplayName、KeyId),解引用前务必先判断是否为nil,避免空指针 panic。 - 证书凭证
GetCertificateCredentials()同样返回切片类型,处理逻辑和密码凭证完全一致。
内容的提问来源于stack exchange,提问作者andre_ri
相关产品推荐
相关产品推荐

