Go Conference Tokyo 2019 Autumn
https://gocon.jp/sessions/microsoft_graph_api_library_for_go/
msgraph.go demo - SharePoint Online + Microsoft Flow + GitLab CI
https://www.youtube.com/watch?v=DwKk405XyF4
msgraph.go
https://github.com/yaegashi/msgraph.go
9. msgraph.go 使用法:Graph Client の作成
import "github.com/yaegashi/msgraph.go/auth"
import msgraph "github.com/yaegashi/msgraph.go/v1.0"
// Create HTTP client with Azure AD OAuth2 device authorization grant
m := auth.NewTokenManager()
t, err := m.DeviceAuthorizationGrant(tenantID, clientID, scope, nil)
if err != nil { /*...*/ }
httpClient := t.Client(context.Background())
// Create MS Graph client
graphClient := msgraph.NewClient(httpClient)
● 最初に Graph Client を作成する
● Azure Active Directory に対して OAuth2 認証を行う http.Client が必要
https://github.com/yaegashi/msgraph.go/tree/master/auth
10. msgraph.go 使用法:REST API 発行
// Get current user’s information
// "GET https://graph.microsoft.com/v1.0/me" -> *msgraph.User
user, err := graphClient.Me().Request().Get()
if err != nil { /*...*/ }
// Get current user's OneDrive root folder items
// "GET https://graph.microsoft.com/v1.0/me/drive/root/children" -> []msgraph.DriveItem
items, err := graphClient.Me().Drive().Root().Children().Request().Get()
if err != nil { /*...*/ }
// Create new group
// "POST https://graph.microsoft.com/v1.0/groups" -> *msgraph.Group
newGroup := &msgraph.Group{ /*...*/ }
createdGroup, err := graphClient.Groups().Request().Add(newGroup)
if err != nil { /*...*/ }
● メソッド呼び出しの連結で REST API の HTTP リクエストが出せる
レスポンスの JSON に対応するモデルの struct が返される
11. msgraph.go 使用法:REST API 対応コード
REST API msgraph.go 説明
GET /users users, err := cli.Users().Request().Get() 全ユーザー取得
POST /users
u := &msgraph.User{/*...*/}
user, err := cli.Users().Request().Add(u)
ユーザー作成
GET /users/XXX user, err := cli.Users().ID("XXX").Request().Get() ユーザー取得
PATCH /users/XXX
u := &msgraph.User{/*...*/}
err := cli.Users().ID("XXX").Request().Update(u)
ユーザー更新
DELETE /users/XXX err := cli.Users().ID("XXX").Request().Delete() ユーザー削除
● 各リソースコレクションの操作メソッドとモデル struct 定義が利用可能
12. msgraph.go 使用法:IDE による補完の活用
● REST API 仕様が Go 言語化されており IDE による補完が活用できる
● ただし Visual Studio Code は知恵熱を出して沈黙することがあり
たまに Restart Language Server する必要がある