msgraphsdkgo

package module
v1.39.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 25, 2024 License: MIT Imports: 86 Imported by: 110

README

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.

Note: The Microsoft Graph Go SDK is currently in General Availability version starting from version 1.0.0. The SDK is considered stable, regular releases and updates to the SDK will however continue weekly..

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.GetError(); 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.GetError(); 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:

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetDefaultClientOptions added in v0.2.1

func GetDefaultClientOptions() core.GraphClientOptions

GetDefaultClientOptions returns the default client options used by the GraphRequestAdapterBase and the middleware.

Types

type GraphBaseServiceClient added in v0.41.0

GraphBaseServiceClient the main entry point of the SDK, exposes the configuration and the fluent API.

func NewGraphBaseServiceClient added in v0.41.1

NewGraphBaseServiceClient instantiates a new GraphBaseServiceClient and sets the default values.

func (*GraphBaseServiceClient) Admin added in v0.41.0

Admin provides operations to manage the admin singleton. returns a *AdminRequestBuilder when successful

func (*GraphBaseServiceClient) AgreementAcceptances added in v0.41.0

AgreementAcceptances provides operations to manage the collection of agreementAcceptance entities. returns a *AgreementAcceptancesRequestBuilder when successful

func (*GraphBaseServiceClient) Agreements added in v0.41.0

Agreements provides operations to manage the collection of agreement entities. returns a *AgreementsRequestBuilder when successful

func (*GraphBaseServiceClient) AppCatalogs added in v0.41.0

AppCatalogs provides operations to manage the appCatalogs singleton. returns a *AppCatalogsRequestBuilder when successful

func (*GraphBaseServiceClient) ApplicationTemplates added in v0.41.0

ApplicationTemplates provides operations to manage the collection of applicationTemplate entities. returns a *ApplicationTemplatesRequestBuilder when successful

func (*GraphBaseServiceClient) Applications added in v0.41.0

Applications provides operations to manage the collection of application entities. returns a *ApplicationsRequestBuilder when successful

func (*GraphBaseServiceClient) ApplicationsWithAppId added in v1.12.0

ApplicationsWithAppId provides operations to manage the collection of application entities. returns a *ApplicationsWithAppIdRequestBuilder when successful

func (*GraphBaseServiceClient) ApplicationsWithUniqueName added in v1.33.0

ApplicationsWithUniqueName provides operations to manage the collection of application entities. returns a *ApplicationsWithUniqueNameRequestBuilder when successful

func (*GraphBaseServiceClient) AuditLogs added in v0.41.0

AuditLogs provides operations to manage the auditLogRoot singleton. returns a *AuditLogsRequestBuilder when successful

func (*GraphBaseServiceClient) AuthenticationMethodConfigurations added in v0.41.0

AuthenticationMethodConfigurations provides operations to manage the collection of authenticationMethodConfiguration entities. returns a *AuthenticationMethodConfigurationsRequestBuilder when successful

func (*GraphBaseServiceClient) AuthenticationMethodsPolicy added in v0.41.0

AuthenticationMethodsPolicy provides operations to manage the authenticationMethodsPolicy singleton. returns a *AuthenticationMethodsPolicyRequestBuilder when successful

func (*GraphBaseServiceClient) CertificateBasedAuthConfiguration added in v0.41.0

CertificateBasedAuthConfiguration provides operations to manage the collection of certificateBasedAuthConfiguration entities. returns a *CertificateBasedAuthConfigurationRequestBuilder when successful

func (*GraphBaseServiceClient) Chats added in v0.41.0

Chats provides operations to manage the collection of chat entities. returns a *ChatsRequestBuilder when successful

func (*GraphBaseServiceClient) Communications added in v0.41.0

Communications provides operations to manage the cloudCommunications singleton. returns a *CommunicationsRequestBuilder when successful

func (*GraphBaseServiceClient) Compliance added in v0.41.0

Compliance provides operations to manage the compliance singleton. returns a *ComplianceRequestBuilder when successful

func (*GraphBaseServiceClient) Connections added in v0.41.0

Connections provides operations to manage the collection of externalConnection entities. returns a *ConnectionsRequestBuilder when successful

func (*GraphBaseServiceClient) Contacts added in v0.41.0

Contacts provides operations to manage the collection of orgContact entities. returns a *ContactsRequestBuilder when successful

func (*GraphBaseServiceClient) Contracts added in v0.41.0

Contracts provides operations to manage the collection of contract entities. returns a *ContractsRequestBuilder when successful

func (*GraphBaseServiceClient) DataPolicyOperations added in v0.41.0

DataPolicyOperations provides operations to manage the collection of dataPolicyOperation entities. returns a *DataPolicyOperationsRequestBuilder when successful

func (*GraphBaseServiceClient) DeviceAppManagement added in v0.41.0

DeviceAppManagement provides operations to manage the deviceAppManagement singleton. returns a *DeviceAppManagementRequestBuilder when successful

func (*GraphBaseServiceClient) DeviceManagement added in v0.41.0

DeviceManagement provides operations to manage the deviceManagement singleton. returns a *DeviceManagementRequestBuilder when successful

func (*GraphBaseServiceClient) Devices added in v0.41.0

Devices provides operations to manage the collection of device entities. returns a *DevicesRequestBuilder when successful

func (*GraphBaseServiceClient) DevicesWithDeviceId added in v1.12.0

DevicesWithDeviceId provides operations to manage the collection of device entities. returns a *DevicesWithDeviceIdRequestBuilder when successful

func (*GraphBaseServiceClient) Directory added in v0.41.0

Directory provides operations to manage the directory singleton. returns a *DirectoryRequestBuilder when successful

func (*GraphBaseServiceClient) DirectoryObjects added in v0.41.0

DirectoryObjects provides operations to manage the collection of directoryObject entities. returns a *DirectoryObjectsRequestBuilder when successful

func (*GraphBaseServiceClient) DirectoryRoleTemplates added in v0.41.0

DirectoryRoleTemplates provides operations to manage the collection of directoryRoleTemplate entities. returns a *DirectoryRoleTemplatesRequestBuilder when successful

func (*GraphBaseServiceClient) DirectoryRoles added in v0.41.0

DirectoryRoles provides operations to manage the collection of directoryRole entities. returns a *DirectoryRolesRequestBuilder when successful

func (*GraphBaseServiceClient) DirectoryRolesWithRoleTemplateId added in v1.12.0

DirectoryRolesWithRoleTemplateId provides operations to manage the collection of directoryRole entities. returns a *DirectoryRolesWithRoleTemplateIdRequestBuilder when successful

func (*GraphBaseServiceClient) DomainDnsRecords added in v0.41.0

DomainDnsRecords provides operations to manage the collection of domainDnsRecord entities. returns a *DomainDnsRecordsRequestBuilder when successful

func (*GraphBaseServiceClient) Domains added in v0.41.0

Domains provides operations to manage the collection of domain entities. returns a *DomainsRequestBuilder when successful

func (*GraphBaseServiceClient) Drives added in v0.41.0

Drives provides operations to manage the collection of drive entities. returns a *DrivesRequestBuilder when successful

func (*GraphBaseServiceClient) Education added in v0.41.0

Education provides operations to manage the educationRoot singleton. returns a *EducationRequestBuilder when successful

func (*GraphBaseServiceClient) EmployeeExperience added in v0.57.0

EmployeeExperience provides operations to manage the employeeExperience singleton. returns a *EmployeeExperienceRequestBuilder when successful

func (*GraphBaseServiceClient) External added in v0.41.0

External provides operations to manage the external singleton. returns a *ExternalRequestBuilder when successful

func (*GraphBaseServiceClient) FilterOperators added in v1.3.0

FilterOperators provides operations to manage the collection of filterOperatorSchema entities. returns a *FilterOperatorsRequestBuilder when successful

func (*GraphBaseServiceClient) Functions added in v1.3.0

Functions provides operations to manage the collection of attributeMappingFunctionSchema entities. returns a *FunctionsRequestBuilder when successful

func (*GraphBaseServiceClient) GetAdapter added in v0.41.1

GetAdapter returns the client current adapter, Method should only be called when the user is certain an adapter has been provided

func (*GraphBaseServiceClient) GroupLifecyclePolicies added in v0.41.0

GroupLifecyclePolicies provides operations to manage the collection of groupLifecyclePolicy entities. returns a *GroupLifecyclePoliciesRequestBuilder when successful

func (*GraphBaseServiceClient) GroupSettingTemplates added in v0.41.0

GroupSettingTemplates provides operations to manage the collection of groupSettingTemplate entities. returns a *GroupSettingTemplatesRequestBuilder when successful

func (*GraphBaseServiceClient) GroupSettings added in v0.41.0

GroupSettings provides operations to manage the collection of groupSetting entities. returns a *GroupSettingsRequestBuilder when successful

func (*GraphBaseServiceClient) Groups added in v0.41.0

Groups provides operations to manage the collection of group entities. returns a *GroupsRequestBuilder when successful

func (*GraphBaseServiceClient) GroupsWithUniqueName added in v1.33.0

GroupsWithUniqueName provides operations to manage the collection of group entities. returns a *GroupsWithUniqueNameRequestBuilder when successful

func (*GraphBaseServiceClient) Identity added in v0.41.0

Identity provides operations to manage the identityContainer singleton. returns a *IdentityRequestBuilder when successful

func (*GraphBaseServiceClient) IdentityGovernance added in v0.41.0

IdentityGovernance provides operations to manage the identityGovernance singleton. returns a *IdentityGovernanceRequestBuilder when successful

func (*GraphBaseServiceClient) IdentityProtection added in v0.41.0

IdentityProtection provides operations to manage the identityProtectionRoot singleton. returns a *IdentityProtectionRequestBuilder when successful

func (*GraphBaseServiceClient) IdentityProviders added in v0.41.0

IdentityProviders provides operations to manage the collection of identityProvider entities. returns a *IdentityProvidersRequestBuilder when successful

func (*GraphBaseServiceClient) InformationProtection added in v0.41.0

InformationProtection provides operations to manage the informationProtection singleton. returns a *InformationProtectionRequestBuilder when successful

func (*GraphBaseServiceClient) Invitations added in v0.41.0

Invitations provides operations to manage the collection of invitation entities. returns a *InvitationsRequestBuilder when successful

func (*GraphBaseServiceClient) Me added in v0.41.0

Me provides operations to manage the user singleton.

func (*GraphBaseServiceClient) Oauth2PermissionGrants added in v0.41.0

Oauth2PermissionGrants provides operations to manage the collection of oAuth2PermissionGrant entities. returns a *Oauth2PermissionGrantsRequestBuilder when successful

func (*GraphBaseServiceClient) Organization added in v0.41.0

Organization provides operations to manage the collection of organization entities. returns a *OrganizationRequestBuilder when successful

func (*GraphBaseServiceClient) PermissionGrants added in v0.41.0

PermissionGrants provides operations to manage the collection of resourceSpecificPermissionGrant entities. returns a *PermissionGrantsRequestBuilder when successful

func (*GraphBaseServiceClient) Places added in v0.41.0

Places the places property returns a *PlacesRequestBuilder when successful

func (*GraphBaseServiceClient) Planner added in v0.41.0

Planner provides operations to manage the planner singleton. returns a *PlannerRequestBuilder when successful

func (*GraphBaseServiceClient) Policies added in v0.41.0

Policies provides operations to manage the policyRoot singleton. returns a *PoliciesRequestBuilder when successful

func (*GraphBaseServiceClient) Print added in v0.41.0

Print provides operations to manage the print singleton. returns a *PrintRequestBuilder when successful

func (*GraphBaseServiceClient) Privacy added in v0.41.0

Privacy provides operations to manage the privacy singleton. returns a *PrivacyRequestBuilder when successful

func (*GraphBaseServiceClient) Reports added in v0.41.0

Reports provides operations to manage the reportRoot singleton. returns a *ReportsRequestBuilder when successful

func (*GraphBaseServiceClient) RoleManagement added in v0.41.0

RoleManagement provides operations to manage the roleManagement singleton. returns a *RoleManagementRequestBuilder when successful

func (*GraphBaseServiceClient) SchemaExtensions added in v0.41.0

SchemaExtensions provides operations to manage the collection of schemaExtension entities. returns a *SchemaExtensionsRequestBuilder when successful

func (*GraphBaseServiceClient) ScopedRoleMemberships added in v0.41.0

ScopedRoleMemberships provides operations to manage the collection of scopedRoleMembership entities. returns a *ScopedRoleMembershipsRequestBuilder when successful

func (*GraphBaseServiceClient) Search added in v0.41.0

Search provides operations to manage the searchEntity singleton. returns a *SearchRequestBuilder when successful

func (*GraphBaseServiceClient) Security added in v0.41.0

Security provides operations to manage the security singleton. returns a *SecurityRequestBuilder when successful

func (*GraphBaseServiceClient) ServicePrincipals added in v0.41.0

ServicePrincipals provides operations to manage the collection of servicePrincipal entities. returns a *ServicePrincipalsRequestBuilder when successful

func (*GraphBaseServiceClient) ServicePrincipalsWithAppId added in v1.12.0

ServicePrincipalsWithAppId provides operations to manage the collection of servicePrincipal entities. returns a *ServicePrincipalsWithAppIdRequestBuilder when successful

func (*GraphBaseServiceClient) Shares added in v0.41.0

Shares provides operations to manage the collection of sharedDriveItem entities. returns a *SharesRequestBuilder when successful

func (*GraphBaseServiceClient) Sites added in v0.41.0

Sites provides operations to manage the collection of site entities. returns a *SitesRequestBuilder when successful

func (*GraphBaseServiceClient) Solutions added in v0.41.0

Solutions provides operations to manage the solutionsRoot singleton. returns a *SolutionsRequestBuilder when successful

func (*GraphBaseServiceClient) SubscribedSkus added in v0.41.0

SubscribedSkus provides operations to manage the collection of subscribedSku entities. returns a *SubscribedSkusRequestBuilder when successful

func (*GraphBaseServiceClient) Subscriptions added in v0.41.0

Subscriptions provides operations to manage the collection of subscription entities. returns a *SubscriptionsRequestBuilder when successful

func (*GraphBaseServiceClient) Teams added in v0.41.0

Teams provides operations to manage the collection of team entities. returns a *TeamsRequestBuilder when successful

func (*GraphBaseServiceClient) TeamsTemplates added in v0.41.0

TeamsTemplates provides operations to manage the collection of teamsTemplate entities. returns a *TeamsTemplatesRequestBuilder when successful

func (*GraphBaseServiceClient) Teamwork added in v0.41.0

Teamwork provides operations to manage the teamwork singleton. returns a *TeamworkRequestBuilder when successful

func (*GraphBaseServiceClient) TenantRelationships added in v0.54.0

TenantRelationships provides operations to manage the tenantRelationship singleton. returns a *TenantRelationshipsRequestBuilder when successful

func (*GraphBaseServiceClient) Users added in v0.41.0

Users provides operations to manage the collection of user entities. returns a *UsersRequestBuilder when successful

type GraphRequestAdapter

type GraphRequestAdapter struct {
	core.GraphRequestAdapterBase
}

GraphRequestAdapter is the core service used by GraphBaseServiceClient to make requests to Microsoft Graph.

func NewGraphRequestAdapter

func NewGraphRequestAdapter(authenticationProvider absauth.AuthenticationProvider) (*GraphRequestAdapter, error)

NewGraphRequestAdapter creates a new GraphRequestAdapter with the given parameters Parameters: authenticationProvider: the provider used to authenticate requests Returns: a new GraphRequestAdapter

func NewGraphRequestAdapterWithParseNodeFactory

func NewGraphRequestAdapterWithParseNodeFactory(authenticationProvider absauth.AuthenticationProvider, parseNodeFactory absser.ParseNodeFactory) (*GraphRequestAdapter, error)

NewGraphRequestAdapterWithParseNodeFactory creates a new GraphRequestAdapter with the given parameters Parameters: authenticationProvider: the provider used to authenticate requests parseNodeFactory: the factory used to create parse nodes Returns: a new GraphRequestAdapter

func NewGraphRequestAdapterWithParseNodeFactoryAndSerializationWriterFactory

func NewGraphRequestAdapterWithParseNodeFactoryAndSerializationWriterFactory(authenticationProvider absauth.AuthenticationProvider, parseNodeFactory absser.ParseNodeFactory, serializationWriterFactory absser.SerializationWriterFactory) (*GraphRequestAdapter, error)

NewGraphRequestAdapterWithParseNodeFactoryAndSerializationWriterFactory creates a new GraphRequestAdapter with the given parameters Parameters: authenticationProvider: the provider used to authenticate requests parseNodeFactory: the factory used to create parse nodes serializationWriterFactory: the factory used to create serialization writers Returns: a new GraphRequestAdapter

func NewGraphRequestAdapterWithParseNodeFactoryAndSerializationWriterFactoryAndHttpClient

func NewGraphRequestAdapterWithParseNodeFactoryAndSerializationWriterFactoryAndHttpClient(authenticationProvider absauth.AuthenticationProvider, parseNodeFactory absser.ParseNodeFactory, serializationWriterFactory absser.SerializationWriterFactory, httpClient *nethttp.Client) (*GraphRequestAdapter, error)

NewGraphRequestAdapterWithParseNodeFactoryAndSerializationWriterFactoryAndHttpClient creates a new GraphRequestAdapter with the given parameters Parameters: authenticationProvider: the provider used to authenticate requests parseNodeFactory: the factory used to create parse nodes serializationWriterFactory: the factory used to create serialization writers httpClient: the client used to send requests Returns: a new GraphRequestAdapter

type GraphServiceClient

type GraphServiceClient struct {
	GraphBaseServiceClient
}

func NewGraphServiceClient

func NewGraphServiceClient(adapter abstractions.RequestAdapter) *GraphServiceClient

func NewGraphServiceClientWithCredentials added in v0.41.1

func NewGraphServiceClientWithCredentials(credential azcore.TokenCredential, scopes []string) (*GraphServiceClient, error)

NewGraphServiceClientWithCredentials instantiates a new GraphServiceClient with provided credentials and scopes

func NewGraphServiceClientWithCredentialsAndHosts added in v0.41.1

func NewGraphServiceClientWithCredentialsAndHosts(credential azcore.TokenCredential, scopes []string, validhosts []string) (*GraphServiceClient, error)

NewGraphServiceClientWithCredentialsAndHosts instantiates a new GraphServiceClient with provided credentials , scopes and validhosts

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL