猿问

如何使用 GoLang API 在 Azure 中创建虚拟网络?

目前我正在尝试使用 GoLang 代码创建虚拟网络:


client := network.NewVirtualNetworksClient(subscriptionID)

var parameters network.VirtualNetwork

c := make(<-chan struct{})


c1, err := client.CreateOrUpdate(resourceGroupName, virtualNetworkName, parameters, c)


utils.Info.Println(err.Error())

utils.Info.Println(c1)

但是代码不能正常工作。它抛出以下异常:


发送请求失败:StatusCode=401 -- 原始错误:长时间运行的操作以“失败”状态终止:代码=“AuthenticationFailed”消息=“身份验证失败。缺少“授权”标头。”


您的帮助将不胜感激。


慕妹3146593
浏览 224回答 2
2回答

动漫人物

有一些关于使用 Azure 身份验证的一般文档,您可以在此处找到:azure-sdk-for-node/Documentation/Authentication虽然还没有专门针对虚拟网络的示例,但您可以在此处找到Azure SDK for Go 示例。特别是对于 Go,您可以更新客户端以使用身份验证令牌,因此您的代码将如下所示:c := make(chan struct{})authConfig, err0 := azure.PublicCloud.OAuthConfigForTenant(<yourTenantID>)token, err1 := azure.NewServicePrincipalToken(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *authConfig,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <yourClientID>,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <yourClientSecret>,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; azure.PublicCloud.ResourceManagerEndpoint)&nbsp;client := network.NewVirtualNetworkClient(subscriptionID)client.Authorizer = tokenvar parameters network.VirtualNetworkc1, err2 := client.CreateOrUpdate(resourceGroupName, virtualNetworkName, parameters, c)

繁星点点滴滴

我假设您使用GoLang SDK for Azure?错误的原因是当您发出请求时,它没有 Authorization 标头。授权标头是您可以从 Azure Active Directory 获取的 JSON Web 令牌1)查看Fiddler中的请求,看看你的请求是否符合规则。2) 阅读有关如何获取令牌的教程(1和2)。
随时随地看视频慕课网APP

相关分类

Go
我要回答