Mac 環境で GA （一般提供開始）した Microsoft Graph CLI を試してみた

私の場合 Azure CLI に慣れているので、簡単な Microsoft Graph API を使う分には、Azure CLI で事足りていました。今回 Microsoft Graph CLI が GA したとの事で、実際に試してみました。

## Mac に Microsoft Graph CLI を導入

https://devblogs.microsoft.com/microsoft365dev/microsoft-graph-cli-v1-0-0-is-now-generally-available/?utm_source=pocket_saves

こちらのドキュメントに記載の `macOS xarm64: https://aka.ms/get/graphcli/latest/osx-arm64.zip` をダウンロードします。

```bash:bash
$ wget https://aka.ms/get/graphcli/latest/osx-arm64.zip

# 拡張子は zip だけど、どうも tar.gz っぽい
$ tar xvfz osx-arm64.zip

$ ./mgc --version
1.0.0
```

## mgc コマンドのヘルプを確認

```bash:bash
$ ./mgc -h
Description:
  Microsoft Graph CLI

Usage:
  mgc [command] [options]

Options:
  -?, -h, --help  Show help and usage information
  --debug         Enable debug output
  --version       Show version information

Commands:
  admin                                  Provides operations to manage the admin singleton.
  agreement-acceptances                  Provides operations to manage the collection of agreementAcceptance entities.
  agreements                             Provides operations to manage the collection of agreement entities.
  app-catalogs                           Provides operations to manage the appCatalogs singleton.
  applications                           Provides operations to manage the collection of application entities.
  applications-with-app-id               Provides operations to manage the collection of application entities.
  application-templates                  Provides operations to manage the collection of applicationTemplate entities.
  audit-logs                             Provides operations to manage the auditLogRoot singleton.
  authentication-method-configurations   Provides operations to manage the collection of authenticationMethodConfiguration entities.
  authentication-methods-policy          Provides operations to manage the authenticationMethodsPolicy singleton.
  certificate-based-auth-configuration   Provides operations to manage the collection of certificateBasedAuthConfiguration entities.
  chats                                  Provides operations to manage the collection of chat entities.
  communications                         Provides operations to manage the cloudCommunications singleton.
  compliance                             Provides operations to manage the compliance singleton.
  connections                            Provides operations to manage the collection of externalConnection entities.
  contacts                               Provides operations to manage the collection of orgContact entities.
  contracts                              Provides operations to manage the collection of contract entities.
  data-policy-operations                 Provides operations to manage the collection of dataPolicyOperation entities.
  device-app-management                  Provides operations to manage the deviceAppManagement singleton.
  device-management                      Provides operations to manage the deviceManagement singleton.
  devices                                Provides operations to manage the collection of device entities.
  devices-with-device-id                 Provides operations to manage the collection of device entities.
  directory                              Provides operations to manage the directory singleton.
  directory-objects                      Provides operations to manage the collection of directoryObject entities.
  directory-roles                        Provides operations to manage the collection of directoryRole entities.
  directory-roles-with-role-template-id  Provides operations to manage the collection of directoryRole entities.
  directory-role-templates               Provides operations to manage the collection of directoryRoleTemplate entities.
  domain-dns-records                     Provides operations to manage the collection of domainDnsRecord entities.
  domains                                Provides operations to manage the collection of domain entities.
  drives                                 Provides operations to manage the collection of drive entities.
  education                              Provides operations to manage the educationRoot singleton.
  employee-experience                    Provides operations to manage the employeeExperience singleton.
  external                               Provides operations to manage the external singleton.
  filter-operators                       Provides operations to manage the collection of filterOperatorSchema entities.
  functions                              Provides operations to manage the collection of attributeMappingFunctionSchema entities.
  group-lifecycle-policies               Provides operations to manage the collection of groupLifecyclePolicy entities.
  group-settings                         Provides operations to manage the collection of groupSetting entities.
  group-setting-templates                Provides operations to manage the collection of groupSettingTemplate entities.
  groups                                 Provides operations to manage the collection of group entities.
  identity-governance                    Provides operations to manage the identityGovernance singleton.
  identity                               Provides operations to manage the identityContainer singleton.
  identity-protection                    Provides operations to manage the identityProtectionRoot singleton.
  identity-providers                     Provides operations to manage the collection of identityProvider entities.
  information-protection                 Provides operations to manage the informationProtection singleton.
  invitations                            Provides operations to manage the collection of invitation entities.
  oauth2-permission-grants               Provides operations to manage the collection of oAuth2PermissionGrant entities.
  organization                           Provides operations to manage the collection of organization entities.
  permission-grants                      Provides operations to manage the collection of resourceSpecificPermissionGrant entities.
  places                                 The places property
  planner                                Provides operations to manage the planner singleton.
  policies                               Provides operations to manage the policyRoot singleton.
  print                                  Provides operations to manage the print singleton.
  privacy                                Provides operations to manage the privacy singleton.
  reports                                Provides operations to manage the reportRoot singleton.
  role-management                        Provides operations to manage the roleManagement singleton.
  schema-extensions                      Provides operations to manage the collection of schemaExtension entities.
  scoped-role-memberships                Provides operations to manage the collection of scopedRoleMembership entities.
  search                                 Provides operations to manage the searchEntity singleton.
  security                               Provides operations to manage the security singleton.
  service-principals                     Provides operations to manage the collection of servicePrincipal entities.
  service-principals-with-app-id         Provides operations to manage the collection of servicePrincipal entities.
  shares                                 Provides operations to manage the collection of sharedDriveItem entities.
  sites                                  Provides operations to manage the collection of site entities.
  solutions                              Provides operations to manage the solutionsRoot singleton.
  subscribed-skus                        Provides operations to manage the collection of subscribedSku entities.
  subscriptions                          Provides operations to manage the collection of subscription entities.
  teams                                  Provides operations to manage the collection of team entities.
  teams-templates                        Provides operations to manage the collection of teamsTemplate entities.
  teamwork                               Provides operations to manage the teamwork singleton.
  tenant-relationships                   Provides operations to manage the tenantRelationship singleton.
  me, users                              Provides operations to manage the collection of user entities.
  login                                  Login and store the session for use in subsequent commands
  logout                                 Logout by deleting the stored session used by commands
```

## Chat 関連のコマンドを試す

```bash:bash
# ログイン
$ ./mgc login
To sign in, use a web browser to open the page https://microsoft.com/devicelogin and enter the code HT8DAGG6R to authenticate.

# デフォルトのログインでは権限が足りない
$ ./mgc chats list
{"error":{"code":"Forbidden","message":"Missing scope permissions on the request. API requires one of 'Chat.ReadBasic, Chat.Read, Chat.ReadWrite'. Scopes on the request 'ChannelMessage.Read.All, Group.ReadWrite.All, openid, profile, Team.ReadBasic.All, User.Read, User.Read.All, email'","innerError":{"date":"2023-11-24T23:21:31","request-id":"c2fcd982-02b0-4ebf-96df-0ef41a735c67","client-request-id":"afc51879-ba78-47c5-a775-63826b425987"}}}

# Chat.Read 権限を付ける
$ ./mgc login --scopes User.Read Chat.Read

# Chat リストを表示
$ ./mgc chats list --select id,chatType,topic

# Chat のメッセージ一覧を表示
$ ./mgc chats messages list --chat-id 19:547c4868c930406c98f6075ed6276c33@thread.v2
```

## 参考

https://devblogs.microsoft.com/microsoft365dev/microsoft-graph-cli-v1-0-0-is-now-generally-available/?utm_source=pocket_saves

https://learn.microsoft.com/ja-jp/graph/cli/overview

https://learn.microsoft.com/ja-jp/graph/api/chat-list?view=graph-rest-1.0&tabs=http

## 関連

https://qiita.com/mnrst/items/0efccf45424d01bd06b4

https://pnp.github.io/cli-microsoft365/

https://qiita.com/mnrst/items/28fad8d061204c2f2e3e