🔑
Microsoft Graph でログイン ユーザーが組織アカウントか Microsoft アカウントかどうかを識別する
/me で判断する
とりあえず思いつくのは /me ですが、あまり違いがわかりません。
組織アカウントの場合
{
"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users/$entity",
"id": "00000000-0000-0000-0000-000000000000",
"businessPhones": [],
"displayName": "Example User",
"givenName": "",
"jobTitle": null,
"mail": "example@example.onmicrosoft.com",
"mobilePhone": null,
"officeLocation": null,
"preferredLanguage": "ja-JP",
"surname": "",
"userPrincipalName": "user@example.onmicrosoft.com"
}
Microsoft アカウントの場合
{
"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users/$entity",
"displayName": "Example User",
"surname": "",
"givenName": "",
"id": "0000000000000000",
"userPrincipalName": "example@outlook.com",
"businessPhones": [],
"jobTitle": null,
"mail": null,
"mobilePhone": null,
"officeLocation": null,
"preferredLanguage": null
}
userPrincipalName のドメイン名で判別するか、id が CID (おそらく ULong 型) か GUID かで判断できます。ただし、どちらもあまり推奨できる方法ではありません。
/organization で判断する
/organization は Microsoft アカウントの場合、空の配列を返します。この方法のほうが適切です。
組織アカウントの場合
{
"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#organization",
"value": [
{
"id": "00000000-0000-0000-0000-000000000000",
"deletedDateTime": null,
"businessPhones": [],
"city": null,
"country": null,
"countryLetterCode": null,
"displayName": "example",
"marketingNotificationEmails": [],
"onPremisesLastSyncDateTime": null,
"onPremisesSyncEnabled": null,
"postalCode": null,
"preferredLanguage": null,
"privacyProfile": null,
"securityComplianceNotificationMails": [],
"securityComplianceNotificationPhones": [],
"state": null,
"street": null,
"technicalNotificationMails": [],
"assignedPlans": [],
"provisionedPlans": [],
"verifiedDomains": [
{
"capabilities": "Email, OfficeCommunicationsOnline",
"isDefault": false,
"isInitial": true,
"name": "example.onmicrosoft.com",
"type": "Managed"
}
]
}
]
}
Microsoft アカウントの場合
{
"@odata.context": "https://graph.microsoft.com/beta/$metadata#organization",
"value": []
}
そもそも判断する必要はありません
アカウントでサポートされていない API を呼び出した場合、エラーが返されます。たとえば、Microsoft アカウントで /me/manager を実行すると 404 エラーが発生します。
{
"error": {
"code": "",
"message": "No HTTP resource was found that matches the request URI 'https://outlook.office365.com:444/profile/v1.0/users('CID:0000000000000000')/profile/manager?api-version=AGSV1-internal'.",
"innerError": {
"request-id": "00000000-0000-0000-0000-000000000000",
"date": "2018-07-10T00:00:00"
}
}
}
まずは API を呼び出してみて、エラーが返された場合はその機能が利用できないと判断してください。
Discussion