Microsoft Graph SDK for Go - Core Library

Microsoft Graph Core SDK for Go

PkgGoDev Coverage Sonarcloud Status

Get started with the Microsoft Graph Core SDK for Go by integrating the Microsoft Graph API into your Go application! You can also have a look at the Go documentation

Note: Although you can use this library directly, we recommend you use the v1 or beta library which rely on this library and additionally provide a fluent style Go API and models.

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.

Samples and usage guide

1. Installation

go get -u github.com/microsoftgraph/msgraph-sdk-go-core

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 GraphRequestAdapterBase 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/go/azure"
    "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)
}

auth, err := a.NewAzureIdentityAuthenticationProviderWithScopes(cred, []string{"Mail.Read", "Mail.Send"})
if err != nil {
    fmt.Printf("Error authentication provider: %v\n", err)
    return
}

2.3 Get a Request Adapter object

You must get a GraphRequestAdapterBase object to make requests against the service.

import core "github.com/microsoftgraph/msgraph-sdk-go-core"

adapter, err := core.NewGraphRequestAdapterBase(auth)
if err != nil {
    fmt.Printf("Error creating adapter: %v\n", err)
    return
}

3. Make requests against the service

After you have a GraphRequestAdapterBase 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 details

To retrieve the user's details

import abs "github.com/microsoft/kiota/abstractions/go"

requestInf := abs.NewRequestInformation()
targetUrl, err := url.Parse("https://graph.microsoft.com/v1.0/me")
if err != nil {
    fmt.Printf("Error parsing URL: %v\n", err)
}
requestInf.SetUri(*targetUrl)

// User is your own type that implements Parsable or comes from the service library
user, err := adapter.SendAsync(*requestInf, func() { return &User }, nil)

if err != nil {
    fmt.Printf("Error getting the user: %v\n", err)
}

4. Issues

For known issues, see issues.

5. Contributions

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

6. License

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

7. Third-party notices

Third-party notices

Owner
Microsoft Graph
Unified endpoint for accessing data, relationships and insights coming from the Microsoft cloud
Microsoft Graph
Comments
  • examples needed

    examples needed

    i'm new to go but after playing with this sdk for a bit, getting basic things to work took a long time and I gave up.

    I gave up on azidentity.NewClientCertificateCredential (does it need a fs path to a cert or a []*x509.Certificate?) I couldnt figure out how to set the right options for .IdentityProvidersRequestBuilderGetOption I couldn't figure out how to parse responses:

    result: &{{map[error:map[code:0xc000099c40 innerError:0xc0001bcf20 message:0xc000099d50]] <nil>} <nil>}
    result: &{map[error:map[code:0xc000012e60 innerError:0xc0000130e0 message:0xc000012f00]] <nil> []}
    cannot range over result (type *graph.IdentityProviderBase)
    cannot range over result2 (type *"github.com/microsoftgraph/msgraph-beta-sdk-go/identity/identityproviders".IdentityProvidersResponse)
    

    the only example I could find used result.GetId()) when calling Drive()

    Can we get an example of how to authenticate with a certificate, set options correctly, and work with responses?

    Your docs currently show this for most methods:

    //THE GO SDK IS IN PREVIEW. NON-PRODUCTION USE ONLY
    graphClient := msgraphsdk.NewGraphServiceClient(requestAdapter)
    result, err := graphClient.Identity().IdentityProviders().Get(options)
    

    what are the options? how do I parse the results?

  • Support http proxies

    Support http proxies

    Customers must be able to configure an HTTP proxy for the request adapter so the applications they build can run in networks that require accessing microsoft graph through a proxy. Docs should also be updated once this is implemented

    Tasks

    • [x] Implement support for proxy authentication
    • [x] #127
  • simplify client initialization

    simplify client initialization

    here are the steps to get a client today

    auth, err := a.NewAzureIdentityAuthenticationProviderWithScopes(credentials, []string{"Files.Read"})
    // handle error
    adapter, err := msgraphsdk.NewGraphRequestAdapter(auth)
    //handle error
    client := msgraphsdk.NewGraphServiceClient(adapter)
    result, err := client.Me().Drive().Get(nil)
    

    Which makes for a lot of boiler plate code.

    By using inheritance, renaming the generated graph service client and adding an override constructor we could make it

    client := msgraphsdk.NewGraphServiceClientWithCredentials(credentials, []string{"Files.Read"})
    // panic if we get an error from the encapsulated layers.
    result, err := client.Me().Drive().Get(nil)
    
  • reduce the amount of code being generated

    reduce the amount of code being generated

    The build time of the service SDKs can take a while for the initial build/dependencies upgrade on some machines. This is caused by the breadth of our API but we might have opportunities to improve that aspect by reducing the amount of code we generate.

    The get field deserializers method might have opportunities for reduction by moving some of the code into helper methods in abstractions https://github.com/microsoftgraph/msgraph-sdk-go/blob/3a362de6cb79ffc639841e0d8fb122975173c145/models/microsoft/graph/message.go#L320

    Same for the serialize method https://github.com/microsoftgraph/msgraph-sdk-go/blob/3a362de6cb79ffc639841e0d8fb122975173c145/models/microsoft/graph/message.go#L667

    Lastly the field accessors might also provide such opportunity https://github.com/microsoftgraph/msgraph-sdk-go/blob/3a362de6cb79ffc639841e0d8fb122975173c145/models/microsoft/graph/message.go#L160

    The goal here is to look into these patterns, identify and share the examples of what could be improved, so we can feed the information back to the kiota generation process.

  • Follow Go's commenting convetions

    Follow Go's commenting convetions

    Overview

    1. Follows Go commenting convention where comments didn't follow the convention
    2. Removes parameters and return types because go doc prints the function signature together with the documentation.

    Demo

    The snippet below shows the output of a go doc command. Note that the function signature is also printed followed by the comment description.

    package msgraphgocore // import "github.com/microsoftgraph/msgraph-sdk-go-core"
    
    func GetDefaultClient(middleware ...khttp.Middleware) *nethttp.Client
        GetDefaultClient creates a new http client with a preconfigured middleware
        pipeline
    
  • Cannot add GET requests to BatchRequest -

    Cannot add GET requests to BatchRequest - "unexpected end of JSON input"

    Adding a GET request to a BatchRequest results in an error:

    error(*encoding/json.SyntaxError) *{msg: "unexpected end of JSON input", Offset: 0}
    

    Repro

    // Use the request builder to generate a regular
    // request to /me
    meRequest, err := client.Me().
        CreateGetRequestInformation(context.Background(), nil)
    if err != nil {
        return err
    }
    
    // Set the baseurl to workaround
    // https://github.com/microsoft/kiota/issues/2061
    meRequest.PathParameters["baseurl"] = client.GetAdapter().GetBaseUrl()
    
    // Create batch and add meRequest
    batch := msgraphcore.NewBatchRequest()
    meRequestItem, err := batch.AddBatchRequestStep(*meRequest)
    if err != nil {
        return err
    }
    

    More information

    This seems to be a bug here:

    https://github.com/microsoftgraph/msgraph-sdk-go-core/blob/efb4b6bb6da1a34505ef78d18ea36cb57e29fd23/batch_requests.go#L119-L123

    For GET requests, Content is always empty. Calling json.Unmarshal on an empty array generates that error.

    image

  • Bump github.com/microsoft/kiota-abstractions-go from 0.10.0 to 0.10.1

    Bump github.com/microsoft/kiota-abstractions-go from 0.10.0 to 0.10.1

    Bumps github.com/microsoft/kiota-abstractions-go from 0.10.0 to 0.10.1.

    Release notes

    Sourced from github.com/microsoft/kiota-abstractions-go's releases.

    v0.10.1

    Changed

    • Fix: Add getter and setter on ResponseHandler pointer .
    Changelog

    Sourced from github.com/microsoft/kiota-abstractions-go's changelog.

    [0.10.1] - 2022-09-14

    Changed

    • Fix: Add getter and setter on ResponseHandler pointer .
    Commits
    • d3af23c Merge pull request #32 from microsoft/fix/pass-request-option-by-ref
    • 44a28cf Adds getter and setter to response handler pointer
    • 1134b6b Update CODEOWNERS (#30)
    • See full diff in compare view

    Dependabot compatibility score

    Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.


    Dependabot commands and options

    You can trigger Dependabot actions by commenting on this PR:

    • @dependabot rebase will rebase this PR
    • @dependabot recreate will recreate this PR, overwriting any edits that have been made to it
    • @dependabot merge will merge this PR after your CI passes on it
    • @dependabot squash and merge will squash and merge this PR after your CI passes on it
    • @dependabot cancel merge will cancel a previously requested merge and block automerging
    • @dependabot reopen will reopen this PR if it is closed
    • @dependabot close will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
    • @dependabot ignore this major version will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this minor version will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this dependency will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
  • Bump github.com/microsoft/kiota-http-go from 0.6.1 to 0.6.2

    Bump github.com/microsoft/kiota-http-go from 0.6.1 to 0.6.2

    Bumps github.com/microsoft/kiota-http-go from 0.6.1 to 0.6.2.

    Release notes

    Sourced from github.com/microsoft/kiota-http-go's releases.

    v0.6.2

    Added

    • Default 100 secs timeout for all request with a default context.
    Changelog

    Sourced from github.com/microsoft/kiota-http-go's changelog.

    [0.6.2] - 2022-08-30

    Added

    • Default 100 secs timeout for all request with a default context.
    Commits
    • 7f2b427 Merge pull request #24 from microsoft/feat/add-request-timeout
    • 790adbd Update change log
    • b66644d Adds a defeault 100 secs timeout per request
    • See full diff in compare view

    Dependabot compatibility score

    Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.


    Dependabot commands and options

    You can trigger Dependabot actions by commenting on this PR:

    • @dependabot rebase will rebase this PR
    • @dependabot recreate will recreate this PR, overwriting any edits that have been made to it
    • @dependabot merge will merge this PR after your CI passes on it
    • @dependabot squash and merge will squash and merge this PR after your CI passes on it
    • @dependabot cancel merge will cancel a previously requested merge and block automerging
    • @dependabot reopen will reopen this PR if it is closed
    • @dependabot close will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
    • @dependabot ignore this major version will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this minor version will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this dependency will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
  • Filename is too long while cloning repository in windows

    Filename is too long while cloning repository in windows

    go get -u github.com/microsoftgraph/msgraph-sdk-go-core

    error: unable to create file msgraph-mail/java/utilities/src/main/java/graphjavav4/utilities/users/item/mailFolders/item/messages/item/multiValueExtendedProperties/item/MultiValueLegacyExtendedPropertyRequestBuilder.java: Filename too long error: unable to create file msgraph-mail/java/utilities/src/main/java/graphjavav4/utilities/users/item/mailFolders/item/messages/item/singleValueExtendedProperties/item/SingleValueLegacyExtendedPropertyRequestBuilder.java: Filename too long Unable to checkout 'b42e1a879cf62da7e666dcf38d4ab69357abba1d' in submodule path 'samples'****

    Please provide alternative way to do this in windows platform.

  • ability to support search

    ability to support search

    I am using this code

    	searchStr := fmt.Sprintf("appId:%s", applicationId)
    	client := msgraphsdk.NewGraphServiceClient(adapter)
    	spResponse, err := client.ServicePrincipals().Get(&serviceprincipals.ServicePrincipalsRequestBuilderGetOptions{
    		Q: &serviceprincipals.ServicePrincipalsRequestBuilderGetQueryParameters{
    			Search: &searchStr,
    		},
    	})
    

    to attempt to generate a url like /v1.0/servicePrincipals?$search="appId:applicationId" however inside microsoft.kiota.abstractions/request_information.go it appears to use reflection to determine the fieldName. The end result of the RawQuery in the request object is search=appId%3Aca1ce348-e18e-4aa0-93fd-2901f9fc6ee8 w/o the dollar.

    is there a workaround for this? am I using the API incorrectly?

    I realize this is probably more about kiota and less about this SDK but would appreciate some comments or direction on this issue. Thanks!

  • Ft page iterator #13

    Ft page iterator #13

    Overview

    Adds PageIterator

    Demo

    Create a page iterator task

    pageIterator, err := msgraphgocore.NewPageIterator(graphResponse, *reqAdapter, ParsableCons)
    pageIterator.SetHeaders(map[string]string{"Content-Type": "application/json"})
    

    Iterate

    err := pageIterator.Iterate(func(pageItem interface{}) bool {
          item := pageItem.(graph.User)
          res = append(res, *item.GetId())
          return true
    })
    
    

    Pause & Resume

    pageIterator.Iterate(func(pageItem interface{}) bool {
          item := pageItem.(graph.User)
          res = append(res, *item.GetId())
          
          return *item.GetId() != "2" // pauses when id == 2
    })
    
    // resumes iteration from user with id 3
    pageIterator.Iterate(func(pageItem interface{}) bool {
          item := pageItem.(graph.User)
          res2 = append(res2, *item.GetId())
          
          return true
    })
    
    

    Notes

    • I made the Next & HasNext methods private because we have not reached a consensus on making them part of the Iterator's API

    Testing

    • Run go test

    Closes #13

  • add support for long running operations

    add support for long running operations

    1. the golang examples under https://learn.microsoft.com/en-us/graph/api/team-post?view=graph-rest-1.0&tabs=go are missing the member odata type, e.g.
    odataType := "#microsoft.graph.aadUserConversationMember"
    member.SetOdataType(&odataType)
    
    1. if result, err := graphClient.Teams().Post(context.Background(), requestBody, nil) is called, the result is always nil, not Teamable or at least containing the new teamID as documented (but the team is successfully created).
  • Update snippets generation for request configuration and query parameters classes changes

    Update snippets generation for request configuration and query parameters classes changes

    The package structure changes has impacted classes names, the snippets generation should reflect such changes.

    Also impacts inline request bodies as well as inline response bodies

    • [x] Fix snippet generation https://github.com/microsoftgraph/microsoft-graph-devx-api/issues/1295
    • [x] Fix raptor tests https://github.com/microsoftgraph/msgraph-sdk-raptor/issues/1508
  • Change Notification models are missing

    Change Notification models are missing

    The Java SDK has models for ChangeNotification and ChangeNotificationCollection to Unmarshal into when handling webhook notifications from Subscriptions, but these and other associated models seem to be missing from the Go SDK.

    Is this possibly because of how the Go SDK is autogenerated that these are missing because they are not part of an API Request, but on the receiving end of data?

  • 📢📢📢 known limitations

    📢📢📢 known limitations

    Hi everyone and thanks for trying our preview of the Microsoft Graph Go SDK built on our next generation generator kiota.

    This issue is here to list out all the limitations we're currently aware of, we'll try to tackle them as soon as possible.

    Please go upvote the linked issues if this is something that's limiting your usage of the SDK, so we know which feature to prioritize.

    List of limitations:

    Note : a checkmark means the issue has been resolved since the initial release.

    Also please be aware that some of the libraries this SDK depends on will move at the end of the preview, so expect breaking changes to happen multiple times!

Fluent JavaScript API for SharePoint and Microsoft Graph REST APIs
Fluent JavaScript API for SharePoint and Microsoft Graph REST APIs

PnPjs is a fluent JavaScript API for consuming SharePoint and Microsoft Graph REST APIs in a type-safe way. You can use it with SharePoint Framework,

Dec 23, 2022
Microsoft Azure SDK for Go

Azure SDK for Go This repository is for active development of the Azure SDK for Go. For consumers of the SDK you can follow the links below to visit t

Jan 6, 2023
A Facebook Graph API SDK For Go.

A Facebook Graph API SDK In Golang This is a Go package that fully supports the Facebook Graph API with file upload, batch request and marketing API.

Dec 12, 2022
A go sdk for baidu netdisk open platform 百度网盘开放平台 Go SDK

Pan Go Sdk 该代码库为百度网盘开放平台Go语言的SDK

Nov 22, 2022
Nextengine-sdk-go: the NextEngine SDK for the Go programming language

NextEngine SDK for Go nextengine-sdk-go is the NextEngine SDK for the Go programming language. Getting Started Install go get github.com/takaaki-s/nex

Dec 7, 2021
Commercetools-go-sdk is fork of original commercetools-go-sdk

commercetools-go-sdk The Commercetools Go SDK is automatically generated based on the official API specifications of Commercetools. It should therefor

Dec 13, 2021
Sdk-go - Go version of the Synapse SDK

synapsesdk-go Synapse Protocol's Go SDK. Currently in super duper alpha, do not

Jan 7, 2022
Redash-go-sdk - An SDK for the programmatic management of Redash, in Go
Redash-go-sdk - An SDK for the programmatic management of Redash, in Go

Redash Go SDK An SDK for the programmatic management of Redash. The main compone

Dec 13, 2022
Nov 28, 2022
A Golang Core api of crane

Core API of Crane core api of crane. DEV GUIDE clone the project to your $GOPATH. following command will generate crd yamls and files in the project d

Dec 13, 2022
The core API for the Atlas Revision project

Atlas Revision API This is the core API for the Atlas Revision project. It provides question data for the website. All questions are from CIE. Initial

Feb 16, 2022
Graph Role-Based Access Control by Animeshon
Graph Role-Based Access Control by Animeshon

gRBAC - Graph Role-Based Access Control A cloud-native graph implementation of the Role-Based Access Control (RBAC) authorization architecture powered

Nov 9, 2022
Simple no frills AWS S3 Golang Library using REST with V4 Signing (without AWS Go SDK)

simples3 : Simple no frills AWS S3 Library using REST with V4 Signing Overview SimpleS3 is a golang library for uploading and deleting objects on S3 b

Nov 4, 2022
Go SDK library for the Solana Blockchain
Go SDK library for the Solana Blockchain

Solana SDK library for Go Go library to interface with Solana JSON RPC and WebSocket interfaces. Clients for Solana native programs, Solana Program Li

Jan 9, 2023
A Golang Client Library for building Cosmos SDK chain clients

Cosmos Client Lib in Go This is the start of ideas around how to implement the cosmos client libraries in a seperate repo How to instantiate and use t

Jan 6, 2023
AWS SDK for the Go programming language.

AWS SDK for Go aws-sdk-go is the official AWS SDK for the Go programming language. Checkout our release notes for information about the latest bug fix

Dec 31, 2022
A Golang SDK for Medium's OAuth2 API

Medium SDK for Go This repository contains the open source SDK for integrating Medium's OAuth2 API into your Go app. Install go get github.com/Medium/

Nov 28, 2022
MinIO Client SDK for Go

MinIO Go Client SDK for Amazon S3 Compatible Cloud Storage The MinIO Go Client SDK provides simple APIs to access any Amazon S3 compatible object stor

Dec 29, 2022
Twilight is an unofficial Golang SDK for Twilio APIs
Twilight is an unofficial Golang SDK for Twilio APIs

Twilight is an unofficial Golang SDK for Twilio APIs. Twilight was born as a result of my inability to spell Twilio correctly. I searched for a Twillio Golang client library and couldn’t find any, I decided to build one. Halfway through building this, I realized I had spelled Twilio as Twillio when searching for a client library on Github.

Jul 2, 2021