How to create a new group in Azure AD using Go SDK?

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

How to create a new group in Azure AD using Go SDK?

问题

你好!以下是如何使用Go SDK在Azure Active Directory中以编程方式创建组的示例代码:

package main

import (
	"context"
	"fmt"
	"log"
	"os"

	"github.com/Azure/azure-sdk-for-go/profiles/latest/graphrbac/graphrbac"
	"github.com/Azure/go-autorest/autorest"
	"github.com/Azure/go-autorest/autorest/azure/auth"
)

func main() {
	tenantID := "<Your Tenant ID>"
	clientID := "<Your Client ID>"
	clientSecret := "<Your Client Secret>"

	authorizer, err := auth.NewClientCredentialsConfig(clientID, clientSecret, tenantID).Authorizer()
	if err != nil {
		log.Fatalf("Failed to get OAuth config: %v", err)
	}

	groupName := "<Your Group Name>"
	groupDescription := "<Your Group Description>"

	groupClient := graphrbac.NewGroupsClient(tenantID)
	groupClient.Authorizer = authorizer

	groupParameters := graphrbac.GroupCreateParameters{
		DisplayName: &groupName,
		Description: &groupDescription,
		MailEnabled: autorest.Bool(false),
		SecurityEnabled: autorest.Bool(true),
	}

	group, err := groupClient.Create(context.TODO(), groupParameters)
	if err != nil {
		log.Fatalf("Failed to create group: %v", err)
	}

	fmt.Printf("Group created successfully. Group ID: %s\n", *group.ObjectID)
}

请确保将 <Your Tenant ID>, <Your Client ID>, <Your Client Secret>, <Your Group Name><Your Group Description> 替换为你自己的值。

这段代码使用 Azure SDK for Go 中的 graphrbac 包来创建一个组。首先,你需要提供你的租户 ID、客户端 ID 和客户端密钥来进行身份验证。然后,你可以设置组的名称、描述等参数,并调用 Create 方法来创建组。创建成功后,你将获得组的对象 ID。

希望这可以帮助到你!如果你有任何其他问题,请随时问我。

英文:

I need to create a group in Azure Active Directory programmatically using Go SDK, I somehow struggle to find the learning resources to do that. Can I get an example or explanation how to go about it?

答案1

得分: 1

要实现您的要求,您可以利用**Microsoft Graph SDK for Go**将Microsoft Graph API集成到您的Go应用程序中。

请确保安装和导入所需的库,并创建authProvider实例。

您可以参考此**Microsoft文档通过Graph API在Azure AD中创建组,其中包含了Go**的代码片段。

在运行查询之前,请确保同意所需的权限。根据您指定的请求正文,将相应地决定**组类型**。

要创建安全组,我提供了以下请求正文:

How to create a new group in Azure AD using Go SDK?

您可以通过选择代码片段获取GO代码

//THE GO SDK IS IN PREVIEW. NON-PRODUCTION USE ONLY
graphClient := msgraphsdk.NewGraphServiceClient(requestAdapter)

requestBody := msgraphsdk.NewGroup()
description := &quot;Example of Security Group&quot;
requestBody.SetDescription(&amp;description)
displayName := &quot;Sri Security&quot;
requestBody.SetDisplayName(&amp;displayName)
requestBody.SetGroupTypes( []string {
}
mailEnabled := false
requestBody.SetMailEnabled(&amp;mailEnabled)
mailNickname := &quot;test&quot;
requestBody.SetMailNickname(&amp;mailNickname)
securityEnabled := true
requestBody.SetSecurityEnabled(&amp;securityEnabled)
result, err := graphClient.Groups().Post(requestBody)

运行上述查询后,安全组成功创建,如下所示:

How to create a new group in Azure AD using Go SDK?

英文:

To achieve your requirement, you can make use of Microsoft Graph SDK for Go by integrating the Microsoft Graph API into your Go application.

Make sure to install and import required libraries and create authProvider instance.

You can refer this Microsoft Doc to create group in Azure AD via Graph API that includes code snippets for Go too.

Make sure to consent required permissions before running the query. Based on the request body you specify; group type will be decided accordingly.

To create Security group, I gave request body like below:

How to create a new group in Azure AD using Go SDK?

You can get GO code by selecting code snippets:

//THE GO SDK IS IN PREVIEW. NON-PRODUCTION USE ONLY
graphClient := msgraphsdk.NewGraphServiceClient(requestAdapter)

requestBody := msgraphsdk.NewGroup()
description := &quot;Example of Security Group&quot;
requestBody.SetDescription(&amp;description)
displayName := &quot;Sri Security&quot;
requestBody.SetDisplayName(&amp;displayName)
requestBody.SetGroupTypes( []string {
}
mailEnabled := false
requestBody.SetMailEnabled(&amp;mailEnabled)
mailNickname := &quot;test&quot;
requestBody.SetMailNickname(&amp;mailNickname)
securityEnabled := true
requestBody.SetSecurityEnabled(&amp;securityEnabled)
result, err := graphClient.Groups().Post(requestBody)

After running the above query, Security Group created successfully like below:

How to create a new group in Azure AD using Go SDK?

huangapple
  • 本文由 发表于 2022年6月23日 00:45:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/72719195.html
匿名

发表评论

匿名网友

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

确定