gpt4 book ai didi

azure - 非管理员的 Microsoft Graph API 权限?

转载 作者:行者123 更新时间:2023-12-03 03:10:03 24 4
gpt4 key购买 nike

我正在尝试创建包含 Office365 租户中所有用户的下拉列表。我在 Azure AD 中创建了一个应用程序并为其授予了所有必要的权限。我实际上给了它 Microsoft Graph 的所有权限、应用程序权限和委派权限。所有这些。

然后我编写了脚本来使用 https://graph.microsoft.com/v1.0/users 查询所有用户。

我让租户管理员进入并接受权限,然后在 UI 中输出用户列表。对于管理员来说工作正常

我不是管理员,但当我访问该页面时,出现以下错误:

This application requires application permissions to another application. Consent for application permissions can only be performed by an administrator. Sign out and sign in as an administrator or contact one of your organization's administrators.

我需要知道这是否适用于权限更低的用户。据我了解,API 请求和应用程序正在 Azure 中授予应用程序的权限下运行。因此,即使用户为只读,请求也不会在该用户下运行,而是在我设置的应用程序下运行。那么为什么我会收到有关权限的错误?

这是我正在使用的代码:

(function () {
"use strict";
// Some samples will use the tenant name here like "tenant.onmicrosoft.com"
// I prefer to user the subscription Id
var subscriptionId = "metenant.onmicrosoft.com";
// Copy the client ID of your AAD app here once you have registered one, configured the required permissions, and
// allowed implicit flow https://msdn.microsoft.com/en-us/office/office365/howto/get-started-with-office-365-unified-api
var clientId = "cccb1f2f-xxx-x-xxxxx-x-x-x-x-x-";

window.config = {
// subscriptionId: subscriptionId,
clientId: clientId,
postLogoutRedirectUri: window.location.origin,
endpoints: {
graphApiUri: 'https://graph.microsoft.com'
},
cacheLocation: 'localStorage' // enable this for IE, as sessionStorage does not work for localhost.
};

var authContext = new AuthenticationContext(config);

// Check For & Handle Redirect From AAD After Login
var isCallback = authContext.isCallback(window.location.hash);
authContext.handleWindowCallback();

if (isCallback && !authContext.getLoginError()) {
window.location = authContext._getItem(authContext.CONSTANTS.STORAGE.LOGIN_REQUEST);
}

// If not logged in force login
var user = authContext.getCachedUser();
// NOTE: you may want to render the page for anonymous users and render
// a login button which runs the login function upon click.
if (!user) authContext.login();

// Acquire token for Files resource.
authContext.acquireToken(config.endpoints.graphApiUri, function (error, token) {
// Handle ADAL Errors.
if (error || !token) {
console.log('ADAL error occurred: ' + error);
return;
}
// Execute GET request to Files API.
var filesUri = config.endpoints.graphApiUri + "/v1.0/users";
$.ajax({
type: "GET",
url: filesUri,
headers: {
'Authorization': 'Bearer ' + token,
}
}).done(function (response) {
console.log('Successfully fetched from Graph.');
console.log(response);

var container = $(".container")

container.empty();

$.each(response.value, function(index, item) {
container.append($('<li>').text(item.displayName + " " + item.mail + " " + item.mobilePhone))
})
}).fail(function (response) {
var err = JSON.parse(response.responseText)
console.log('Failed:', err.error.message);
});
});
})();

最佳答案

Microsoft Graph 有两种权限/范围。一是需要管理员同意。另一个则不需要。

您为此应用程序配置的权限是什么?要列出未经管理员同意的用户,我们可以使用范围 User.ReadBasic.All 如下图所示: enter image description here

您可以从here获取有关权限/范围的更多详细信息。 .

修改:

目前,adal.js 未提供管理员同意。如果您想使用此功能,可以修改代码添加如下参数:

AuthenticationContext.prototype.login = function (prompt) {
// Token is not present and user needs to login
var expectedState = this._guid();
this.config.state = expectedState;
this._idTokenNonce = this._guid();
this._logstatus('Expected state: ' + expectedState + ' startPage:' + window.location);
this._saveItem(this.CONSTANTS.STORAGE.LOGIN_REQUEST, window.location);
this._saveItem(this.CONSTANTS.STORAGE.LOGIN_ERROR, '');
this._saveItem(this.CONSTANTS.STORAGE.STATE_LOGIN, expectedState);
this._saveItem(this.CONSTANTS.STORAGE.NONCE_IDTOKEN, this._idTokenNonce);
this._saveItem(this.CONSTANTS.STORAGE.FAILED_RENEW, '');
this._saveItem(this.CONSTANTS.STORAGE.ERROR, '');
this._saveItem(this.CONSTANTS.STORAGE.ERROR_DESCRIPTION, '');


var urlNavigate = this._getNavigateUrl('id_token', null) + '&nonce=' + encodeURIComponent(this._idTokenNonce);

if (prompt && prompt === "admin_consent") {
urlNavigate = urlNavigate + "&prompt=admin_consent"
}


this.frameCallInProgress = false;
this._loginInProgress = true;
if (this.config.displayCall) {
// User defined way of handling the navigation
this.config.displayCall(urlNavigate);
} else {
this.promptUser(urlNavigate);
}
// callback from redirected page will receive fragment. It needs to call oauth2Callback
};

如果您使用的是 Angular,我们还需要修改 adal-angular.js:

this.$get = ['$rootScope', '$window', '$q', '$location', '$timeout', function ($rootScope, $window, $q, $location, $timeout) {
...
return {
// public methods will be here that are accessible from Controller
config: _adal.config,
login: function (prompt) {
_adal.login(prompt);
},
...
}

然后我们可以提供两个按钮供用户登录。一个按钮是用户自己登录。另一个是管理员给予组织的同意。以下是在 Angular 的控制下重定向到管理员同意登录页面的代码:

$scope.login = function () {
adalService.login("admin_consent");
};

关于azure - 非管理员的 Microsoft Graph API 权限?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39731303/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com