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

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

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(&lt;-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(&lt;yourTenantID&gt;)
token, err1 := azure.NewServicePrincipalToken(
                  *authConfig,
                  &lt;yourClientID&gt;,
                  &lt;yourClientSecret&gt;,
                  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(&lt;yourTenantID&gt;)
token, err1 := azure.NewServicePrincipalToken(
                  *authConfig,
                  &lt;yourClientID&gt;,
                  &lt;yourClientSecret&gt;,
                  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。

  1. 在Fiddler中查看请求,看看你的请求是否符合规则

  2. 参考有关如何获取令牌的教程(12)。

英文:

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

  1. Take a look at the request in the Fiddler and see if your request is aligned with the rules.

  2. Go with the tutorials about how to get the token (1 and 2).

huangapple
  • 本文由 发表于 2016年4月22日 22:51:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/36797003.html
匿名

发表评论

匿名网友

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

确定