错误102通常发生在使用100并调用101端点.
- 100是非交互流,101端点仅与用户交互流一起工作.
- 因此,要么切换到用户交互流,要么使用100并呼叫101端点.
授予应用程序API权限以使用100:
然后通过修改100来修改代码,如下所示:
using Azure.Identity;
using Microsoft.Graph;
using Microsoft.Graph.Models;
using System;
using System.Threading.Tasks;
namespace UserProperties
{
public class GraphHandler
{
public GraphServiceClient GraphClient { get; set; }
public GraphHandler()
{
var tenantId = "TenantID";
var clientId = "ClientID";
var clientSecret = "ClientSecret";
GraphClient = CreateGraphClient(tenantId, clientId, clientSecret);
}
public GraphServiceClient CreateGraphClient(string tenantId, string clientId, string clientSecret)
{
var options = new TokenCredentialOptions
{
AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
};
var clientSecretCredential = new ClientSecretCredential(tenantId, clientId, clientSecret, options);
var scopes = new[] { "https://graph.microsoft.com/.default" };
return new GraphServiceClient(clientSecretCredential, scopes);
}
public async Task userDetail()
{
try
{
// Example usage to get user details directly
var userId = "UserID"; // Replace with the desired user's ID
var user = await GraphClient.Users[userId].GetAsync();
// Print user ID and DisplayName
Console.WriteLine($"User ID: {user.Id}");
Console.WriteLine($"Display Name: {user.DisplayName}");
}
catch (ServiceException ex)
{
Console.WriteLine($"Error getting user details: {ex.Message}");
}
}
}
class Program
{
static async Task Main(string[] args)
{
GraphHandler handler = new GraphHandler();
// Example usage to get my details
await handler.userDetail();
}
}
}
The user details displayed successfully like below:
如果您想调用101端点,则授予User.Read委托API权限,并参考MsDoc (except Client credentials provider choose any of the flow)并 Select 交互流进行认证.
100
To get the User Profile Photo, use the below code:
using System;
using System.IO;
using System.Threading.Tasks;
using Azure.Identity;
using Microsoft.Graph;
using Microsoft.Graph.Models;
namespace UserProperties
{
public class GraphHandler
{
public GraphServiceClient GraphClient { get; set; }
public GraphHandler()
{
var tenantId = "TenantID";
var clientId = "ClientID";
var clientSecret = "ClientSecret";
GraphClient = CreateGraphClient(tenantId, clientId, clientSecret);
}
public GraphServiceClient CreateGraphClient(string tenantId, string clientId, string clientSecret)
{
var options = new TokenCredentialOptions
{
AuthorityHost = Azure.Identity.AzureAuthorityHosts.AzurePublicCloud
};
var clientSecretCredential = new Azure.Identity.ClientSecretCredential(tenantId, clientId, clientSecret, options);
var scopes = new[] { "https://graph.microsoft.com/.default" };
return new GraphServiceClient(clientSecretCredential, scopes);
}
public async Task<User> GetUser(string userId)
{
try
{
var user = await GraphClient.Users[userId].GetAsync();
return user;
}
catch (ServiceException ex)
{
Console.WriteLine($"Error getting user details: {ex.Message}");
return null;
}
}
public async Task PrintProfilePicture(string userId)
{
var user = await GetUser(userId);
if (user != null)
{
try
{
using (var photoStream = await GraphClient.Users[userId].Photo.Content.GetAsync())
{
var fileName = $"{userId}_profile_pic.jpg";
using (var fileStream = File.Create(fileName))
{
await photoStream.CopyToAsync(fileStream);
Console.WriteLine($"Profile picture saved as: {fileName}");
}
}
}
catch (ServiceException ex)
{
Console.WriteLine($"Error downloading profile picture: {ex.Message}");
}
}
}
}
class Program
{
static async Task Main(string[] args)
{
var handler = new GraphHandler();
var userId = "UserID"; // Replace with the desired user's ID
await handler.PrintProfilePicture(userId);
}
}
}
Reference:
Get a user - Microsoft Graph v1.0 | Microsoft