Skip to content

Instantly share code, notes, and snippets.

@maisarissi
Created May 2, 2023 20:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maisarissi/fb718b08309e850cb3a43851a1a61453 to your computer and use it in GitHub Desktop.
Save maisarissi/fb718b08309e850cb3a43851a1a61453 to your computer and use it in GitHub Desktop.
microsoftgraph-go-v1-both_clients
import (
    "context"
    "fmt"
    "log"
    "github.com/Azure/azure-sdk-for-go/sdk/azidentity"
    auth "github.com/microsoft/kiota-authentication-azure-go"
    msgraphbeta "github.com/microsoftgraph/msgraph-beta-sdk-go"
    msgraphsdk "github.com/microsoftgraph/msgraph-sdk-go"
)
type GraphHelper struct {
    deviceCodeCredential *azidentity.DeviceCodeCredential
    prodClient           *msgraphsdk.GraphServiceClient
    betaClient           *msgraphbeta.GraphServiceClient
    graphUserScopes      []string
}
func (g *GraphHelper) InitializeV1AndBetaGraphClients() error {
    // Create the device code credential
    credential, err := azidentity.NewDeviceCodeCredential(&azidentity.DeviceCodeCredentialOptions{
        ClientID: "CLIENT_ID",
        TenantID: "TENANT_ID",
        UserPrompt: func(ctx context.Context, message azidentity.DeviceCodeMessage) error {
            fmt.Println(message.Message)
            return nil
        },
    })
    if err != nil {
        return err
    }
    g.deviceCodeCredential = credential
    // Create an auth provider using the credential
    authProvider, err := auth.NewAzureIdentityAuthenticationProviderWithScopes(credential, []string{"Files.Read"})
    if err != nil {
        return err
    }
    adapterV1, err := msgraphsdk.NewGraphRequestAdapter(authProvider)
    if err != nil {
        return err
    }
    v1Client := msgraphsdk.NewGraphServiceClient(adapterV1)
    g.prodClient = v1Client
    adapterBeta, err := msgraphbeta.NewGraphRequestAdapter(authProvider)
    if err != nil {
        return err
    }
    testClient := msgraphbeta.NewGraphServiceClient(adapterBeta)
    g.betaClient = testClient
    return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment