Created
May 2, 2023 20:26
-
-
Save maisarissi/fb718b08309e850cb3a43851a1a61453 to your computer and use it in GitHub Desktop.
microsoftgraph-go-v1-both_clients
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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