GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - microsoftgraph/msgraph-sdk-go: Microsoft Graph SDK for Go

BranchesTags
Open more actions menu

Latest commit

 

History

1,060 Commits
1,060 Commits

Folders and files

NameName
Last commit message
Last commit date
.azure-pipelines
.azure-pipelines
 
 
.config
.config
 
 
.github
.github
 
 
admin
admin
 
 
agreementacceptances
agreementacceptances
 
 
agreements
agreements
 
 
appcatalogs
appcatalogs
 
 
applications
applications
 
 
applicationswithappid
applicationswithappid
 
 
applicationswithuniquename
applicationswithuniquename
 
 
applicationtemplates
applicationtemplates
 
 
auditlogs
auditlogs
 
 
authenticationmethodconfigurations
authenticationmethodconfigurations
 
 
authenticationmethodspolicy
authenticationmethodspolicy
 
 
certificatebasedauthconfiguration
certificatebasedauthconfiguration
 
 
chats
chats
 
 
communications
communications
 
 
compliance
compliance
 
 
connections
connections
 
 
contacts
contacts
 
 
contracts
contracts
 
 
datapolicyoperations
datapolicyoperations
 
 
deviceappmanagement
deviceappmanagement
 
 
devicemanagement
devicemanagement
 
 
devices
devices
 
 
deviceswithdeviceid
deviceswithdeviceid
 
 
directory
directory
 
 
directoryobjects
directoryobjects
 
 
directoryroles
directoryroles
 
 
directoryroleswithroletemplateid
directoryroleswithroletemplateid
 
 
directoryroletemplates
directoryroletemplates
 
 
domaindnsrecords
domaindnsrecords
 
 
domains
domains
 
 
drives
drives
 
 
education
education
 
 
employeeexperience
employeeexperience
 
 
external
external
 
 
filteroperators
filteroperators
 
 
functions
functions
 
 
grouplifecyclepolicies
grouplifecyclepolicies
 
 
groups
groups
 
 
groupsettings
groupsettings
 
 
groupsettingtemplates
groupsettingtemplates
 
 
groupswithuniquename
groupswithuniquename
 
 
identity
identity
 
 
identitygovernance
identitygovernance
 
 
identityprotection
identityprotection
 
 
identityproviders
identityproviders
 
 
informationprotection
informationprotection
 
 
invitations
invitations
 
 
models
models
 
 
oauth2permissiongrants
oauth2permissiongrants
 
 
organization
organization
 
 
permissiongrants
permissiongrants
 
 
places
places
 
 
planner
planner
 
 
policies
policies
 
 
print
print
 
 
privacy
privacy
 
 
reports
reports
 
 
rolemanagement
rolemanagement
 
 
schemaextensions
schemaextensions
 
 
scopedrolememberships
scopedrolememberships
 
 
search
search
 
 
security
security
 
 
serviceprincipals
serviceprincipals
 
 
serviceprincipalswithappid
serviceprincipalswithappid
 
 
shares
shares
 
 
sites
sites
 
 
solutions
solutions
 
 
storage
storage
 
 
subscribedskus
subscribedskus
 
 
subscriptions
subscriptions
 
 
teams
teams
 
 
teamstemplates
teamstemplates
 
 
teamwork
teamwork
 
 
tenantrelationships
tenantrelationships
 
 
tests
tests
 
 
users
users
 
 
userswithuserprincipalname
userswithuserprincipalname
 
 
.gitignore
.gitignore
 
 
.release-please-manifest.json
.release-please-manifest.json
 
 
CHANGELOG.md
CHANGELOG.md
 
 
CODE_OF_CONDUCT.md
CODE_OF_CONDUCT.md
 
 
CONTRIBUTING.md
CONTRIBUTING.md
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
SECURITY.md
SECURITY.md
 
 
THIRD PARTY NOTICES
THIRD PARTY NOTICES
 
 
go.mod
go.mod
 
 
go.sum
go.sum
 
 
graph_base_service_client.go
graph_base_service_client.go
 
 
graph_request_adapter.go
graph_request_adapter.go
 
 
graph_service_client.go
graph_service_client.go
 
 
kiota-dom-export.txt
kiota-dom-export.txt
 
 
kiota-lock.json
kiota-lock.json
 
 
release-please-config.json
release-please-config.json
 
 

Repository files navigation

Microsoft Graph SDK for Go

PkgGoDev

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.

1. Installation

go get github.com/microsoftgraph/msgraph-sdk-go
go get github.com/microsoft/kiota-authentication-azure-go

2. Getting started

2.1 Register your application

Register your application by following the steps at Register your app with the Microsoft Identity Platform.

2.2 Create an AuthenticationProvider object

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.

This example uses the DeviceCodeCredential class, which uses the device code flow to authenticate the user and acquire an access token. This authentication method is not enabled on app registrations by default. In order to use this example, you must enable public client flows on the app registation in the Azure portal by selecting Authentication under Manage, and setting the Allow public client flows toggle to Yes.

import (
    azidentity "github.com/Azure/azure-sdk-for-go/sdk/azidentity"
    "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)
}

2.3 Get a Graph Service Client and Adapter object

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
}

3. Make requests against the service

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.

3.1 Get the user's drive

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.GetErrorEscaped(); terr != nil {
			fmt.Printf("code: %s", *terr.GetCode())
			fmt.Printf("msg: %s", *terr.GetMessage())
		}
	default:
		fmt.Printf("%T > error: %#v", err, err)
	}
}

4. Getting results that span across multiple pages

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.

4.1 Get all the users in an environment

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)
    return err
}

// Use PageIterator to iterate through all users
pageIterator, err := msgraphcore.NewPageIterator[models.Userable](result, client.GetAdapter(), models.CreateUserCollectionResponseFromDiscriminatorValue)

err = pageIterator.Iterate(context.Background(), func(user models.Userable) bool {
    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: %s", typed.Error())
                if terr := typed.GetErrorEscaped(); terr != nil {
                        fmt.Printf("code: %s", *terr.GetCode())
                        fmt.Printf("msg: %s", *terr.GetMessage())
                }
        default:
                fmt.Printf("%T > error: %#v", err, err)
        }
}

5. Documentation

For more detailed documentation, see:

  • Overview
  • Collections
  • Making requests
  • Known issues
  • Contributions

5. Update Schedule

The Microsoft Graph Go Client Library is scheduled to be updated during the second and fourth week of each month

6. Issues

For known issues, see issues.

7. Contributions

The Microsoft Graph SDK is open for contribution. To contribute to this project, see Contributing.

8. License

Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT license.

9. Third-party notices

Third-party notices

About

Microsoft Graph SDK for Go

docs.microsoft.com/en-us/graph/sdks/sdks-overview

Topics

devxenggomicrosoft-graphmicrosoft-graph-sdksdk

Resources

Readme
MIT license

Code of conduct

Code of conduct

Contributing

Contributing

Security policy

Security policy
Activity
Custom properties

Stars

332 stars

Watchers

18 watching

Forks

44 forks
Report repository

Releases

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.