gomsgraph
Gomsgraph is a Go client library for accessing the Microsoft Graph API v1.
Package documentation
For complete usage of the Gomsgraph client, see the full package documentation.
Installation
Installation of a specific version using go get
.
go get github.com/michaljirman/gomsgraph@v1.0.0
Authentication
The Gomsgraph library uses oauth2 library to implement the oauth2 authentication.
Simply pass http.Client
implementation which can handle authentication while creating a new MS Graph
client.
ctx := context.Background()
tc := msgraph.GetOAuth2Client(ctx, azureTenantID, azureClientID, azureClientSecret)
client := msgraph.NewClient(tc)
OAuth2 documentation is available on oauth2 docs.
Example usage
To create a new Gomsgraph client:
package main
import (
"context"
"flag"
"fmt"
"log"
"github.com/google/uuid"
"github.com/michaljirman/gomsgraph/msgraph"
"github.com/michaljirman/gomsgraph/msgraph/models"
)
var (
azureClientID string
azureClientSecret string
azureTenantID string
)
func main() {
flag.StringVar(&azureClientID, "azure-client-id", "", "")
flag.StringVar(&azureClientSecret, "azure-client-secret", "", "")
flag.StringVar(&azureTenantID, "azure-client-tenant-id", "", "")
flag.Parse()
if azureClientID == "" {
log.Fatal("azure client id is required")
}
if azureClientSecret == "" {
log.Fatal("azure client secret is required")
}
if azureTenantID == "" {
log.Fatal("azure tenant id is required")
}
ctx := context.Background()
tc := msgraph.GetOAuth2Client(ctx, azureTenantID, azureClientID, azureClientSecret)
client := msgraph.NewClient(tc)
}
To create a new User:
ctx := context.Background()
req := &models.User{
Id: msgraph.String(uuid.NewString()),
AccountEnabled: msgraph.Bool(true),
DisplayName: msgraph.String("Test User"),
MailNickname: msgraph.String("TestU"),
UserPrincipalName: msgraph.String("TestU@testworkspace.onmicrosoft.com"),
PasswordProfile: &models.PasswordProfile{
Password: msgraph.String(uuid.NewString()),
ForceChangePasswordNextSignIn: msgraph.Bool(true),
ForceChangePasswordNextSignInWithMfa: msgraph.Bool(true),
},
}
userResp, err := client.Users.CreateUser(ctx, req)
To delete a user:
err = client.Users.DeleteUser(ctx, *userResp.Id)
To list all users:
usersResp, err := client.Users.ListAll(ctx, &msgraph.UserListOptions{})
To list users with pagination:
opts := &msgraph.UserListOptions{
Top: 1,
}
var allUsers []*models.User
for {
usersResp, err := client.Users.ListAll(ctx, opts)
allUsers = append(allUsers, usersResp.Users...)
if usersResp.NextLink == nil {
break
}
opts.NextLink = *usersResp.NextLink
}
Developing
Committed code must pass following checks:
To list all development commands:
$ make help
fmt : format go files
golangci-lint : run golangci-lint
gotest : run all golang tests
integration : run all integration tests
lint : run lint tools
staticcheck : run staticcheck
test : run all tests
To use a proxy, set the environment variable HTTP_PROXY
from a shell or withing the program:
$ export HTTP_PROXY=http://proxy_name:proxy_port
os.Setenv("HTTP_PROXY", "http://proxy_name:proxy_port")
Versioning
Versions of the client are tagged on github. For each tagged version a new release is automatically creating
using a goreleaser and github actions.
Contributing
We love pull requests! Please see the contribution guidelines.