我正在基于 java 的服务器端应用程序中为 Office 365 帐户实现 Oauth 2 身份验证。阅读文档后,我做了以下事情:
https://login.microsoftonline.com/common/oauth2/authorize?client_id= {client_id}&response_type=code&redirect_uri={重定向网址}&response_mode=query
作为对此的回应,我按预期获得了授权代码:
http://localhost:8080?code={authorication code}&session_state=259479e4-84aa-42ea-91e9-9e919cc99587
Run Code Online (Sandbox Code Playgroud)
现在我需要获取令牌和用户名(用户登录的用户 ID),因为我需要用户名进行进一步处理。为此,我使用此处描述的方法:
https://azure.microsoft.com/en-us/documentation/articles/active-directory-protocols-oauth-code/
即使用如下 POST 请求:
POST /{tenant}/oauth2/token HTTP/1.1
Host: https://login.microsoftonline.com
Content-Type: application/x-www-form-urlencoded
grant_type=authorization_code
&client_id=2d4d11a2-f814-46a7-890a-274a72a7309e
&code=AwABAAAAvPM1KaPlrEqdFSBzjqfTGBCmLdgfSTLEMPGYuNHSUYBrqqf_ZT_p5uEAEJJ_nZ3UmphWygRNy2C3jJ239gV_DBnZ2syeg95Ki-374WHUP-i3yIhv5i-7KU2CEoPXwURQp6IVYMw-DjAOzn7C3JCu5wpngXmbZKtJdWmiBzHpcO2aICJPu1KvJrDLDP20chJBXzVYJtkfjviLNNW7l7Y3ydcHDsBRKZc3GuMQanmcghXPyoDg41g8XbwPudVh7uCmUponBQpIhbuffFP_tbV8SNzsPoFz9CLpBCZagJVXeqWoYMPe2dSsPiLO9Alf_YIe5zpi-zY4C3aLw5g9at35eZTfNd0gBRpR5ojkMIcZZ6IgAA
&redirect_uri=https%3A%2F%2Flocalhost%2Fmyapp%2F
&resource=https%3A%2F%2Fservice.contoso.com%2F
&client_secret=p@ssw0rd
Run Code Online (Sandbox Code Playgroud)
现在的问题是,当我发送这个 post 请求时,我总是会收到错误代码,有时是 400 或 402 等。我还在 chrome 中使用 POST man 来检查调用的响应。它总是返回这样的错误:
{
"error": "invalid_grant",
"error_description": "AADSTS65001: The user or administrator has not consented to use the application …Run Code Online (Sandbox Code Playgroud) azure office-addins outlook-addin office365 microsoft-graph-api
我有一个用户/组的 objectid 列表,我想解析它们以显示名称和个人资料照片。我不想对每个 objectid 发出单独的请求,而是希望对它们进行批处理并在一个响应中获取所有结果。有可用的 API 吗?从我所做的搜索来看,我找不到类似的东西。如果这样的 API 不存在,是否有任何技巧可以提高效率?
谢谢,波阿斯
我正在尝试使用 Graph API 检索 Sharepoint 文档库中的文件层次结构。由于文档库存储在“驱动器”中(将其称为 OneDrive 在技术上是否正确?),我使用端点/drives来获取文件列表,如下所示:
https://graph.microsoft.com/beta/drives/{driveid}/root/children
Run Code Online (Sandbox Code Playgroud)
我想从通过 Sharepoint 查看这些项目时存在的一些自定义列中获取信息。使用?expand=fields不起作用,因为fields仅存在于端点的 listItem 对象中/sites,而不存在于端点driveItem的对象中/drives。如果我尝试从单个driveItem获取listItem(从OneDrive遍历图表到Sharepoint),然后扩展字段,例如
https://graph.microsoft.com/beta/drives/{driveid}/items/{driveItemId}/listItem?expand=fields
Run Code Online (Sandbox Code Playgroud)
这会检索内置列(Author、DocIcon 和其他一些列),但似乎不会检索自定义列。我还尝试从端点获取文件列表/sites,并使用?expand=fields将获取自定义列,但它从每个子文件夹获取每个文件,而不是当前文件夹路径。但我觉得这值得有一个自己的问题。
是否可以从driveItems 检索自定义列信息?
截至今天,文档建议使用 Microsoft Graph 而不是 Azure AD Graph API 来访问 Azure AD/B2C 资源。
之前,使用 Azure AD Graph API,我们可以使用https://graph.windows.net/[tenant]/users/1a2a9c4d-fc59-4fd9-ad14-b72b549cdf6a?api-version=2013-11-08等查询
响应包含 Azure B2C 自定义属性(在 Azure 门户上创建)
{
"odata.metadata": "https://graph.windows.net/<tenant>/$metadata#directoryObjects/Microsoft.DirectoryServices.User",
"value": [
{
"objectId": "00000eab-603a-4de2-9d25-d3821e7d6583",
...
"extension_3a4189d71ad149c6ab5e65ac45bd6add_MyAttribute1": "something"
}
]
}
Run Code Online (Sandbox Code Playgroud)
Graph API 不会发生这种情况,仅返回一些“基本”属性 https://graph.microsoft.com/v1.0/users/00000eab-603a-4de2-9d25-d3821e7d6583
{
"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users/$entity",
"id": "00000eab-603a-4de2-9d25-d3821e7d6583",
...
}
Run Code Online (Sandbox Code Playgroud)
还尝试显式选择属性但未返回扩展值
...graph.microsoft.com/v1.0/users/00000eab-603a-4de2-9d25-d3821e7d6583/?$select=id,extension_3a4189d71ad149c6ab5e65ac45bd6add_MyAttribute1
如何使用 Graph API 读取 Azure B2C 自定义属性?
我正在尝试使用 Microsoft Graph 将文件上传到我的组织的 SharePoint,但找不到{parent-id}以下终结点:
PUT /users/{user-id}/drive/items/{parent-id}:/{filename}:/content
Run Code Online (Sandbox Code Playgroud)
应该{parent-id}是id文件将插入到的父目录吗?
React 应用程序查询 Microsoft Graph 以发现用户是否是特定 Active Directory 组的成员。现在,有这样的路线:
https://graph.microsoft.com/v1.0/me/memberOf
Run Code Online (Sandbox Code Playgroud)
上面的内容在应用程序中确实有效。它返回一个包含所有用户组的(相当大的)对象,因此我可以迭代列表,但最好直接检查用户是否是单个组的成员。
鉴于我已经可以看到所有组的列表,这似乎并不困难,但我没有找到这样做的途径。
我错过了一些明显的东西吗?
谢谢。
PS 如果我可以在不需要应用程序注册的管理员权限的情况下执行此操作,那就太好了。
我发布了这个请求:
POST https://login.microsoftonline.com:443/{my-tennant-here}/oauth2/v2.0/token HTTP/1.1
Host: login.microsoftonline.com
Content-Type: application/x-www-form-urlencoded
client_id={client id here}
&scope=https%3A%2F%2Fgraph.microsoft.com%2F.default
&client_secret={client secret here}
&grant_type=client_credentials
这将返回:
{
"token_type": "Bearer",
"expires_in": 3599,
"ext_expires_in": 0,
"access_token": "eyJ0eX......
}
我已经使用 jwt.io 解码了令牌,它绝对不是空的。它包含 14 个声明。aud, iss, tid 等...
然后我在此请求中使用访问令牌
GET https://graph.microsoft.com/v1.0/users
Athorization: Bearer eyJ0eX...
然后我得到一个 401 Unauthorized 这个机构:
{
"error": {
"code": "InvalidAuthenticationToken",
"message": "Access token is empty.",
"innerError": {
"request-id": "",
"date": "2018-08-14T15:41:44"
}
}
}
预期结果是 200 Ok,正文包含用户列表
这是否仅仅意味着我的应用程序未经授权,并且错误消息只是具有误导性(访问令牌为空)?还是我做错了什么?
更新:我注意到虽然令牌确实包含声明,但它没有范围声明,这对我来说似乎有点奇怪。我会假设它具有 User.Read.All 范围。应用程序(客户端 ID/客户端机密)应该具有此权限。我收到的令牌中的声明具有以下声明:
aud: "https://graph.microsoft.com",
iss: "https://sts.windows.net/my tennant …Run Code Online (Sandbox Code Playgroud) authentication azure azure-active-directory azure-ad-msal microsoft-graph-api
在 AAD 应用程序注册中,隐式授予流程为 true;我们已将权限委派给 User.Read 和 User.Read.All。
private static getContext(): Msal.UserAgentApplication {
if (AuthenticationService.Context) return AuthenticationService.Context;
const logger = new Msal.Logger((_logLevel, message, _piiEnabled) => {
console.log(message);
}, { level: Msal.LogLevel.Verbose, correlationId: "12345" });
AuthenticationService.Context = new Msal.UserAgentApplication(
Environment().authentication.clientId,
AuthenticationService.getAuthority(),
(errorDesc, token, error, _tokenType) => {
if (token) {
AuthenticationService.isAuthenticated = true;
AuthenticationService.accessToken = token;
} else {
const localizedError: string = LocalizationService.localize(error);
alert(localizedError !== error ? localizedError : errorDesc);
}
},
{
logger: logger,
storeAuthStateInCookie: true,
state: "12345",
cacheLocation: "localStorage" …Run Code Online (Sandbox Code Playgroud) 除了检索用户日历的日历视图(代表用户)之外,我们还努力尝试通过 Graph API 使用
https://graph.microsoft.com/beta/users/room1@ourdomain.com/calendarView. 这是一个痛苦的过程,因为我们遇到了很多问题,目前被困在以下404响应中:
https://graph.microsoft.com:443/v1.0/users/room1@ourdomain.com/calendarView?startDateTime=2018-12-04T23:00:00.000Z&endDateTime=2019-02-10T22:59:59.999Z
{
"error": {
"code": "ErrorItemNotFound",
"message": "The specified object was not found in the store.",
"innerError": {
"request-id": "358a003a-57a4-4f0e-91da-edc17c1fa2d8",
"date": "2018-12-12T07:38:33"
}
}
}
Run Code Online (Sandbox Code Playgroud)
房间的电子邮件地址已经过双重检查并且资源存在,因为我们可以使用它创建约会,并且当我们检索在该位置有约会的用户的日历时,它甚至会在响应中返回。
应用程序权限和 OAuth2 范围设置为:openid email profile offline_access https://graph.microsoft.com/Calendars.Read https://graph.microsoft.com/Calendars.Read.Shared https://graph.microsoft.com/User.Read
https://graph.microsoft.com/User.ReadBasic.All https://graph.microsoft.com/User.Read.All,因此根据文档判断,这应该不是问题。
有谁知道如何解决这个问题?
我正在尝试以我注册的应用程序登录,并授予以下权限:Azure 门户 > 应用程序注册 > 应用程序注册(预览)> 我的应用程序名称 - API 权限
根据此文档,APP ID URI在请求令牌时,我必须在范围参数中传递我的资源标识符 ( )。我确信这个范围参数是导致我出现问题的参数。
我尝试了范围的不同参数。
https://graph.microsoft.com/.default:这适用于基本功能,例如阅读日历,但我认为默认权限很少满足我的需求。既然这样有效,我相信我的其他参数是正确的,范围就是问题所在。
[APP-ID]/.default:这给了我一个成功的响应,但是,每当我尝试提出任何请求时,包括基本的读取日历请求,我都会收到InvalidAuthenticationToken. 我可以向您保证,我正在传递从令牌请求中检索到的正确令牌。
基于在线建议的多种不同 URL 组合。他们都回来了
“在租户 {id} 中找不到资源主体 {resource-url}。
我坚信问题在于我没有通过APP ID URI我的应用程序的正确性。谁能告诉我在哪里可以找到这个资源?我在网上搜索的所有内容都已使用 2 年以上,对于新的 Azure 门户似乎并不相同。