所以我有一个场景,其中应用程序应该在某些条件下将用户添加到组中。此外,当应用程序开始运行时,不应要求用户登录其Microsoft ID/pwd。
因此,我访问了使用图形服务客户端对象创建的令牌,如下所示:
GraphServiceClient graphClient = new GraphServiceClient(
"https://graph.microsoft.com/v1.0",
new DelegateAuthenticationProvider(
async (requestMessage) =>
{
string clientId = "My APP ID";
string authorityFormat = "https://login.microsoftonline.com/{0}/v2.0";
string tenantId = "tenant GUID";
string[] _scopes = new string[] {
"https://graph.microsoft.com/User.ReadBasic.All"
};
// Custom Redirect URI asigned in the Application Registration
// Portal in the native Application Platform
string redirectUri = "https://localhost:4803/";
string clientSecret = "App Secret";
ConfidentialClientApplication daemonClient = new ConfidentialClientApplication(
clientId,
String.Format(authorityFormat, tenantId),
redirectUri,
new ClientCredential(clientSecret),
null, new TokenCache()
);
AuthenticationResult authResult = await daemonClient.AcquireTokenForClientAsync(_scopes);
string token = authResult.AccessToken;
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", token);
}
)
);
所以我尝试执行var user = await graphClient.Me.Request().GetAsync();
我收到此错误:
AADSTS70011:为输入参数"scope"提供的值无效。范围 user.read 无效。
我也尝试仅使用User.ReadBasic
作为范围,但得到同样的错误。
我在这里做错了什么?
您在此处使用客户端凭据流,这意味着您无法动态请求范围。必须在 apps.dev.microsoft.com 中配置应用注册所需的权限范围,然后将代码中的范围值设置为https://graph.microsoft.com/.default
。
有关更多详细信息,请参阅 https://developer.microsoft.com/en-us/graph/docs/concepts/auth_v2_service。
我建议使用 .defuault 范围
默认范围用于在请求中泛指资源服务 (API(,而不标识特定权限...
客户端不能在单个请求中同时使用静态(.default(同意和动态同意。因此,scope=https://graph.microsoft.com/.default Mail.Read 会导致错误,因为它结合了作用域类型。
用于在 C# 中设置作用域变量
string[] scope = new string[] {".default"};
用于设置您的范围变量 Java
private final List<String> scope = Arrays.asList(".default");
我从另一个授权中复制了代码,该授权适用于用户而不是组织。 所以我收到了错误,因为我grant_type错误。
确保你的是"client_credentials">