英文:
How to create Virtual Network in Azure using GoLang API?
问题
目前我正在尝试使用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 -- 原始错误:长时间运行的操作以状态'失败'终止:Code="AuthenticationFailed" Message="身份验证失败。'Authorization' 头丢失。"
非常感谢您的帮助。
英文:
Currently I am trying to create Virtual Network using the GoLang code:
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)
But the code isn't working fine. It throws the following exception:
Failure sending request: StatusCode=401 -- Original Error: Long running operation terminated with status 'Failed': Code="AuthenticationFailed" Message="Authentication failed. The 'Authorization' header is missing."
Your help will be highly appreciated.
答案1
得分: 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(
*authConfig,
<yourClientID>,
<yourClientSecret>,
azure.PublicCloud.ResourceManagerEndpoint)
client := network.NewVirtualNetworkClient(subscriptionID)
client.Authorizer = token
var parameters network.VirtualNetwork
c1, err2 := client.CreateOrUpdate(resourceGroupName, virtualNetworkName, parameters, c)
英文:
There is some good documentation in general for using Azure authentication that you can find here: azure-sdk-for-node/Documentation/Authentication
While there isn't an example for Virtual Networks specifically yet, you can find Azure SDK for Go samples here.
For Go in particular, you can update a Client to use an authentication token, so your code would look something like this:
c := make(chan struct{})
authConfig, err0 := azure.PublicCloud.OAuthConfigForTenant(<yourTenantID>)
token, err1 := azure.NewServicePrincipalToken(
*authConfig,
<yourClientID>,
<yourClientSecret>,
azure.PublicCloud.ResourceManagerEndpoint)
client := network.NewVirtualNetworkClient(subscriptionID)
client.Authorizer = token
var parameters network.VirtualNetwork
c1, err2 := client.CreateOrUpdate(resourceGroupName, virtualNetworkName, parameters, c)
答案2
得分: 1
我假设你使用的是GoLang SDK for Azure。错误的原因是在发送请求时,请求中没有包含授权头部(Authorization header)。授权头部是你可以从Azure Active Directory获取的JSON Web Token。
英文:
I assume you use GoLang SDK for Azure ? The reason of the error is that when you make a request, it does not have the Authorization header. Authorization header is the JSON Web Token that you can obtain from Azure Active Directory
-
Take a look at the request in the Fiddler and see if your request is aligned with the rules.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论