Get started with the Microsoft Graph SDK for Go by integrating the Microsoft Graph API into your Go application!
Note: this SDK allows you to build applications using the v1.0 of Microsoft Graph. If you want to try the latest Microsoft Graph APIs under beta, use our beta SDK instead.
Note: the Microsoft Graph Go SDK is currently in Community Preview. During this period we're expecting breaking changes to happen to the SDK based on community's feedback. Checkout the known limitations.
go get github.com/microsoftgraph/msgraph-sdk-go
go get github.com/microsoft/kiota-authentication-azure-go
Register your application by following the steps at Register your app with the Microsoft Identity Platform.
An instance of the GraphRequestAdapter class handles building client. To create a new instance of this class, you need to provide an instance of AuthenticationProvider, which can authenticate requests to Microsoft Graph.
For an example of how to get an authentication provider, see choose a Microsoft Graph authentication provider.
Note: we are working to add the getting started information for Go to our public documentation, in the meantime the following sample should help you getting started.
import (
azidentity "github.com/Azure/azure-sdk-for-go/sdk/azidentity"
a "github.com/microsoft/kiota-authentication-azure-go"
"context"
)
cred, err := azidentity.NewDeviceCodeCredential(&azidentity.DeviceCodeCredentialOptions{
TenantID: "<the tenant id from your app registration>",
ClientID: "<the client id from your app registration>",
UserPrompt: func(ctx context.Context, message azidentity.DeviceCodeMessage) error {
fmt.Println(message.Message)
return nil
},
})
if err != nil {
fmt.Printf("Error creating credentials: %v\n", err)
}
You must get a GraphRequestAdapter object to make requests against the service.
import msgraphsdk "github.com/microsoftgraph/msgraph-sdk-go"
client , err := msgraphsdk.NewGraphServiceClientWithCredentials(cred, []string{"Files.Read"})
if err != nil {
fmt.Printf("Error creating client: %v\n", err)
return
}
After you have a GraphServiceClient that is authenticated, you can begin making calls against the service. The requests against the service look like our REST API.
To retrieve the user's drive:
import (
"github.com/microsoftgraph/msgraph-sdk-go/models/odataerrors"
)
result, err := client.Me().Drive().Get(context.BackGround(), nil)
if err != nil {
fmt.Printf("Error getting the drive: %v\n", err)
printOdataError(err)
}
fmt.Printf("Found Drive : %v\n", *result.GetId())
// omitted for brevity
func printOdataError(err error) {
switch err.(type) {
case *odataerrors.ODataError:
typed := err.(*odataerrors.ODataError)
fmt.Printf("error:", typed.Error())
if terr := typed.GetError(); terr != nil {
fmt.Printf("code: %s", *terr.GetCode())
fmt.Printf("msg: %s", *terr.GetMessage())
}
default:
fmt.Printf("%T > error: %#v", err, err)
}
}
Items in a collection response can span across multiple pages. To get the complete set of items in the collection, your application must make additional calls to get the subsequent pages until no more next link is provided in the response.
To retrieve the users:
import (
msgraphcore "github.com/microsoftgraph/msgraph-sdk-go-core"
"github.com/microsoftgraph/msgraph-sdk-go/users"
"github.com/microsoftgraph/msgraph-sdk-go/models"
"github.com/microsoftgraph/msgraph-sdk-go/models/odataerrors"
)
result, err := client.Users().Get(context.Background(), nil)
if err != nil {
fmt.Printf("Error getting users: %v\n", err)
printOdataError(err error)
return err
}
// Use PageIterator to iterate through all users
pageIterator, err := msgraphcore.NewPageIterator(result, client.GetAdapter(), models.CreateUserCollectionResponseFromDiscriminatorValue)
err = pageIterator.Iterate(context.Background(), func(pageItem interface{}) bool {
user := pageItem.(models.Userable)
fmt.Printf("%s\n", *user.GetDisplayName())
// Return true to continue the iteration
return true
})
// omitted for brevity
func printOdataError(err error) {
switch err.(type) {
case *odataerrors.ODataError:
typed := err.(*odataerrors.ODataError)
fmt.Printf("error:", typed.Error())
if terr := typed.GetError(); terr != nil {
fmt.Printf("code: %s", *terr.GetCode())
fmt.Printf("msg: %s", *terr.GetMessage())
}
default:
fmt.Printf("%T > error: %#v", err, err)
}
}
For more detailed documentation, see:
For known issues, see issues.
The Microsoft Graph SDK is open for contribution. To contribute to this project, see Contributing.
Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT license.