How to authenticate with the Microsoft Graph SDK for Go
mainAuthentication is handled by providing an AuthenticationProvider to a GraphRequestAdapter.
A common method is using the DeviceCodeCredential from the azidentity package, which implements the device code flow.
Important: To use device code flow, you must enable Allow public client flows in your app registration under the Authentication section in the Azure portal.
import (
azidentity "github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"context"
"fmt"
)
// Example using DeviceCodeCredential
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
},
})