Skip to content

Instantly share code, notes, and snippets.

@baywet
Last active November 23, 2021 13:22
Show Gist options
  • Save baywet/d323470cb62f58a96bf36228863e8544 to your computer and use it in GitHub Desktop.
Save baywet/d323470cb62f58a96bf36228863e8544 to your computer and use it in GitHub Desktop.
Sample for the go sdk preview announcement
import (
azidentity "github.com/Azure/azure-sdk-for-go/sdk/azidentity"
a "github.com/microsoft/kiota/authentication/go/azure"
"context"
msgraphsdk "github.com/microsoftgraph/msgraph-sdk-go"
)
// the following block creates a new device code credential using the Azure Identity library which will be used by the authentication provider to obtain an access token for requests
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)
}
// the following block creates a new authentication provider using the previously created credential, it will be used by the request adapter to authenticate the requests
auth, err := a.NewAzureIdentityAuthenticationProviderWithScopes(cred, []string{"Files.Read"})
if err != nil {
fmt.Printf("Error authentication provider: %v\n", err)
return
}
// the following block creates a new request adapter using the previously created authentication provider, it will be used by the client to execute the requests built using the fluent style API
adapter, err := msgraphsdk.NewGraphRequestAdapter(auth)
if err != nil {
fmt.Printf("Error creating adapter: %v\n", err)
return
}
// the following block create a new Graph Service Client using the previously created request adapter.
// the client is the starting point of the fluent style API to access anything in Microsoft Graph
client := msgraphsdk.NewGraphServiceClient(adapter)
// the following block uses the fluent style API provided by the client to build a request to get the current user’s OneDrive information
result, err := client
.Me()
.Drive()
.Get(nil)
if err != nil {
fmt.Printf("Error getting the drive: %v\n", err)
}
fmt.Printf("Found Drive : %v\n", result.GetId())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment