All Projects → microsoftgraph → msgraph-sdk-go

microsoftgraph / msgraph-sdk-go

Licence: MIT license
Microsoft Graph SDK for Go

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to msgraph-sdk-go

odata-client
Java client generator for a service described by OData CSDL 4.0 metadata. Includes Microsoft Graph clients (v1.0 and Beta), Graph Explorer client, Analytics for DevOps, Dynamics CRM clients
Stars: ✭ 23 (-78.1%)
Mutual labels:  microsoft-graph, microsoft-graph-sdk
msgraph-sdk-java-core
Microsoft Graph SDK for Java - Core Library
Stars: ✭ 33 (-68.57%)
Mutual labels:  microsoft-graph
spfx-msgraph-peoplesearch
Show and search users from your organization with an SPFx web part, through Microsoft Graph.
Stars: ✭ 29 (-72.38%)
Mutual labels:  microsoft-graph
office-mygroups-react
This application shows the list of your Office 365 Groups retrieved using the Microsoft Graph.
Stars: ✭ 27 (-74.29%)
Mutual labels:  microsoft-graph
BlazorGraphApi
Blazor Server App with Azure AD Authentication, that calls the Microsoft Graph API on-behalf of the signed-in user.
Stars: ✭ 28 (-73.33%)
Mutual labels:  microsoft-graph
powershell
PnP PowerShell
Stars: ✭ 326 (+210.48%)
Mutual labels:  microsoft-graph
msgraph-training-nodeexpressapp
This sample demonstrates how to use the Microsoft Graph JavaScript SDK to access data in Office 365 from Node.js Express apps.
Stars: ✭ 116 (+10.48%)
Mutual labels:  microsoft-graph
dataconnect-solutions
Microsoft Graph Data Connect enables scaled access to Microsoft 365 data with fine grained consent model, integrated with compliance and governance, to enable analytics and big data applications on the largest productivity platform.
Stars: ✭ 14 (-86.67%)
Mutual labels:  microsoft-graph
msgraph-training-android
This sample demonstrates how to use the Microsoft Graph Java SDK to access data in Office 365 from native mobile Android applications.
Stars: ✭ 31 (-70.48%)
Mutual labels:  microsoft-graph
QuickTeams
Download, Clone, Archive, Un-Archive your Microsoft Teams and manage the apps you've installed to them
Stars: ✭ 31 (-70.48%)
Mutual labels:  microsoft-graph
msgraph-training-javascriptspa
This sample demonstrates how to use the Microsoft Graph JavaScript SDK to access data in Office 365 from JavaScript browser apps.
Stars: ✭ 23 (-78.1%)
Mutual labels:  microsoft-graph
Partner-Smart-Office
An open source toolkit empowering partners understand their customer’s security posture.
Stars: ✭ 37 (-64.76%)
Mutual labels:  microsoft-graph
script-samples
A sample gallery of scripts to manage all things Microsoft 365.
Stars: ✭ 56 (-46.67%)
Mutual labels:  microsoft-graph-sdk
restfuloutlook
This sample app is a .NET/Winform tool to test making REST calls for Outlook/Exchange data, using either the Microsoft Graph SDK (client libraries) or sending the calls directly to Exchange Online, similar to the Graph Explorer.
Stars: ✭ 13 (-87.62%)
Mutual labels:  microsoft-graph-sdk

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 Community Preview. During this period we're expecting breaking changes to happen to the SDK based on community's feedback. Checkout the known limitations.

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.

import (
    azidentity "github.com/Azure/azure-sdk-for-go/sdk/azidentity"
    a          "github.com/microsoft/kiota-authentication-azure-go"
    "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 error)
    return err
}

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

err = pageIterator.Iterate(context.Background(), func(pageItem interface{}) bool {
    user := pageItem.(models.Userable)
    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:", 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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].