Categorygithub.com/bnixon67/msgraph4go
modulepackage
0.0.0-20210327162546-f714a2670a49
Repository: https://github.com/bnixon67/msgraph4go.git
Documentation: pkg.go.dev

# README

msgraph4go

msgraph4go provides a Go interface for the Microsoft Graph API.

This is still a work in progress, but does have some working examples for OneDrive and OneNote

In order to use this package, you must Register an application with the Microsoft identity platform

  1. Sign in to the Azure portal using either a work or school account or a personal Microsoft account.

  2. If your account gives you access to more than one tenant, select your account in the top right corner, and set your portal session to the Azure AD tenant that you want.

  3. Select the Azure Active Directory service, and then select App registrations > New registration.

  4. When the Register an application page appears, enter your application's registration information:

    • Name - enter a meaningful name
    • Supported account types - select one of the options based on your planned usage
    • Redirect URI (optional) - select Public client (mobile & desktop), and then enter https://login.microsoftonline.com/common/oauth2/nativeclient

In order to run the examples, you need to set the MSCLIENTID environmental variable to the Application (client) ID provided.

The current approach assumes the client runs on a host without a browser. The user is instructed to vist a URL to login and authorize the client. Once the login is successful, the user must copy the response URL and provide to the client program.

The token is requested for offline access, which should include a refresh token to allow access for a long period of time. The token is saved in the file provided to msgraph4go.New(...).

For example, on the first run without a token file:

~/go/src/github.com/bnixon67/msgraph4go/examples$ go run GetMyProfile.go 
Vist the following URL in a browser to authenticate this application
After authentication, copy the response URL from the browser
https://login.microsoftonline.com/common/oauth2/v2.0/authorize?access_type=offline&client_id={client_id}&redirect_uri=https%3A%2F%2Flogin.microsoftonline.com%2Fcommon%2Foauth2%2Fnativeclient&response_type=code&scope=User.Read&state={state}

Copy and paste the URL into a browser with javascript enabled to login to your Microsoft account. Once logged in, then copy the URL from the browser into the program:

Enter the response URL:
https://login.microsoftonline.com/common/oauth2/nativeclient?code={code}&state={state}
{
  "displayName": "First Last",
  "givenName": "First",
  "id": "{id}",
  "surname": "Last",
  "userPrincipalName": "{...}"
}

On subsequent runs (with a saved token file), the execution should be seamless.

You need to delete the token file is you use a different client ID or request scopes not already authorized for the current token.

A simple example, which returns a JSON result:

// Get Microsoft Application (client) ID
// The ID is not in the source code to avoid someone reusing the ID
clientID, present := os.LookupEnv("MSCLIENTID")
if !present {
	log.Fatal("Must set MSCLIENTID")
}

msGraphClient := msgraph4go.New(".token.json", clientID, []string{"User.Read"})

resp, err := msGraphClient.Get("/me", nil)
if err != nil {
	log.Fatal(err)
}

Another example that returns a custom User type:

// Get Microsoft Application (client) ID
// The ID is not in the source code to avoid someone reusing the ID
clientID, present := os.LookupEnv("MSCLIENTID")
if !present {
	log.Fatal("Must set MSCLIENTID")
}

msGraphClient := msgraph4go.New(".token.json", clientID, []string{"User.Read"})

user, err := msGraphClient.GetMyProfile(nil)
if err != nil {
	log.Fatal(err)
}

fmt.Println(msgraph4go.VarToJsonString(user)) 

# Packages

No description provided by the author

# Functions

New creates an initialized MSGraphClient using the token from tokenFileName.
VarToJsonString converts any varaible (interface{}) to an indented JSON string.
WriteContent writes the []byte content to a file.

# Structs

Attachment is the base resource for the following derived types of attachments: A file (fileAttachment resource) An item (contact, event or message, represented by an itemAttachment resource) A link to a file (referenceAttachment resource).
BaseItem is an abstract resource that contains a common set of properties shared among several other resources types.
Calendar is a container for events.
CalendarGroup is a group of user calendars.
CalendarGroupResponse is a collection of CalendarGroup types.
CalendarResponse is a collection of Calendar types.
Contact is an item in Outlook where you can organize and save information about the people and organizations you communicate with.
ContactResponse is a collection of Calendar types.
DateTimeTimeZone describes the date, time, and time zone of a point in time.
Drive is the top level object representing a user's OneDrive or a document library in SharePoint.
DriveItem represents a file, folder, or other item stored in a drive.
DriveItemResponse is a collection of DriveItem types.
DriveItemVersion represents a specific version of a DriveItem.
DriveItemVersionResponse is a collection of DriveItemVersion types.
DriveResponse is a collection of Drive types.
EmailAddress is the name and email address of a contact or message recipient.
Extension is an abstract type to support the OData v4 open type openTypeExtension.
ExternalLink is a url that opens a OneNote page or notebook.
File groups file-related data items into a single structure.
FileSystemInfo contains properties that are reported by the device's local file system for the local version of an item.
Folder groups folder-related data on an item into a single structure.
FolderView provides or sets recommendations on the user-experience of a folder.
FollowupFlag allows setting a flag in an item for the user to follow up on later.
GraphErrorResponse contains a single property named error.
The Identity resource represents an identity of an actor.
IdentitySet is a keyed collection of identity resources.
InnerError are additional error objects that may be more specific than the top level error.
InternetMessageHeader is a key-value pair that represents an Internet message header, as defined by RFC5322, that provides details of the network path taken by a message from the sender to the recipient.
ItemBody represents properties of the body of an item, such as a message, event or group post.
ItemReference provides information necessary to address a DriveItem via the API.
Message is a message in a mailFolder.
MessageCollection is a collection of Notebook types.
MSGraphClient is a client connection to the MS Graph API.
MultiValueLegacyExtendedProperty is an extended property that contains a collection of values.
Notebook represents a OneNote notebook.
NotebookCollection is a collection of Notebook types.
NotebookLinks are links for opening a OneNote notebook.
No description provided by the author
ODataError contains information about the Error.
Package indicates that a DriveItem is the top level item in a "package" or a collection of items that should be treated as a collection instead of individual items.
Page represents a page in a OneNote notebook.
PageCollection is a collection of Page types.
PageLinks contain links for opening a OneNote page.
Permission provides information about a sharing permission granted for a DriveItem resource.
PermissionsResponse is a collection of Permission types.
PhysicalAddress represents the street address of a resource such as a contact or event.
ProfilePhoto is a profile photo of a user, group or an Outlook contact accessed from Exchange Online.
PublicationFacet provides details on the published status of a driveItemVersion or driveItem resource.
Quota provides details about space constrains on a Drive resource.
Recipient represents information about a user in the sending or receiving end of an event, message or group post.
RemoteItem indicates that a driveItem references an item that exists in another drive.
Section represents a section in a OneNote notebook.
SectionResponse is a collection of Section types.
SharePointIds groups the various identifiers for an item stored in a SharePoint site or OneDrive for Business into a single structure.
SharingInvitation groups invitation-related data items into a single structure.
SharingLink groups link-related data items into a single structure.
SingleValueLegacyExtendedProperty is an extended property that contains a single value.
User represents an Azure AD user account Not all of the properties have been included from https://docs.microsoft.com/en-us/graph/api/resources/user?view=graph-rest-1.0.