【发布时间】:2017-03-13 13:55:20
【问题描述】:
我正在使用 Azure AD 和 Office 365 API 在我的项目中执行 OAuth。我的问题是我只能拥有 admin 帐户(如“user@project.onmicrosoft.com”)并获取数据,但 非管理员常规 帐户(如“ user@hotmail.com") 不能。
我如何实现 OAuth2
- 获取授权码:
https://login.microsoftonline.com/common/oauth2/authorize?response_type=code&client_id={my client Id}&redirect_uri={redirect uri}&resource=https%3A%2F%2Foutlook.office365.com%2F&state={guid}
- 获取访问令牌和刷新令牌:
TokenCache tokenCache = new TokenCache();
ClientCredential credential = new ClientCredential(clientId, clientSecret);
AuthenticationContext authContext = new AuthenticationContext(authorityUrl, tokenCache);
AuthenticationResult authResult = authContext.AcquireTokenByAuthorizationCode(authorizationCode, new Uri(redirectUri), credential, recourceUri);
string accessToken = authResult.AccessToken;
string refreshToken = authResult.RefreshToken;
- 获取用户数据(例如日历事件):
OutlookServicesClient outlookClient = new OutlookServicesClient(new Uri(recourceUri + "/api/v2.0"), async () => { return accessToken; });
List<Event> microsoftEvents = new List<Event>();
var events = await outlookClient.Me.Events.Take(10).ExecuteAsync();
foreach (IEvent calendarEvent in events.CurrentPage)
{
Event microsoftEvent = new Event
{
Subject = calendarEvent.Subject,
Body = calendarEvent.Body,
Location = calendarEvent.Location,
Start = calendarEvent.Start,
End = calendarEvent.End
};
microsoftEvents.Add(microsoftEvent);
}
注意:
- 我不确定是不是 Azure AD 权限设置造成的,所以暂时授予“Windows Azure Active Directory”和“Office 365 Exchange Online”所有可用权限。
- 我没有为此 Azure AD 付费。我有一个 Office 365 开发人员帐户,并且在 Office 管理中心内有一个指向 AAD 的链接。不确定这是否是原因。我是否需要为额外的 AAD 订阅付费?
2016 年 11 月 2 日更新
以前对帐户的误解。像“user@project.onmicrosoft.com”这样的帐户是 Office 365 帐户,而不是管理员帐户。 Hotmail 帐户实际上是普通的 Microsoft 帐户。
Jason 提到,Azure v1 端点不支持 microsoft 帐户授权。这里主要是指授权码的生成。
必须在新门户 (https://apps.dev.microsoft.com) 中创建 Azure AD 应用程序。否则会报Application not supported issue。
【问题讨论】:
标签: azure oauth-2.0 azure-active-directory adal office365api