我想使用C#中的图形API更新我的用户配置文件映像和另一个用户的配置文件映像.

在Microsoft Azure中,我拥有使用代码做任何事情的所有权限.

怎么办?

推荐答案

确保授予100应用程序类型API权限:

enter image description here

To update the profile photo of a user, please use the below code:

using System;
using System.IO;
using System.Threading.Tasks;
using Azure.Identity;
using Microsoft.Graph;

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<bool> UpdateProfilePicture(string userId, string imagePath)
        {
            try
            {
                using (var stream = new FileStream(imagePath, FileMode.Open))
                {
                    await GraphClient.Users[userId].Photo.Content.PutAsync(stream);
                    Console.WriteLine("Profile picture updated successfully.");
                    return true;
                }
            }
            catch (ServiceException ex)
            {
                Console.WriteLine($"Error updating profile picture: {ex.Message}");
                return false;
            }
        }
    }

    class Program
    {
        static async Task Main(string[] args)
        {
            var handler = new GraphHandler();
            var userId = "UserID"; // Replace with the desired user's ID
            await handler.UpdateProfilePicture(userId, "C:\\Users\\rukmini\\Downloads\\ruk.jpg");
        }
    }
}

enter image description here

个人资料照片更新成功:

enter image description here

Csharp相关问答推荐

如何打印已添加到List的Linq值,而不是C#中的:System.Collections.Generic.List ' 1[System.Int32]?

在ASP.NET中为数据注释 Select 合适的语言

哪个nuget包含SecurityStampValidatorOptions

如何告诉自己创建的NuGet包在应用程序中发生了变化?

碰撞检测与盒碰撞器,其isTrigger on问题

当通过Google的Gmail Api发送邮件时,签名会产生dkim = neutral(正文散列未验证)'

将轮询与超时同步

将现有字段映射到EFCore中的复杂类型

JSON空引用异常仅在调试器中忽略try-Catch块,但在其他上下文中捕获得很好

当前的文化决定了错误的文化

在使用Audit.NET的AuditTrail实现中,如何逐月将数据摄取到AzureTableStorage?

具有可空类型的C#NOTNULL约束具有意外行为

如何在.NET AOT中为所有枚举启用JsonStringEnumConverter

在PostgreSQL上使用ExecuteSqlRawAsync的C#11原始字符串文字有区分大小写的问题

源代码生成器:CS8795分部方法';Class1.GetS2(字符串)';必须有实现部分,因为它有可访问性修饰符?

CRL已过期,但ChainStatus告诉我RevocationStatus未知

如何在发布NuGet包之前设置命名空间?

具有嵌套属性的IGGroup

将文本从剪贴板粘贴到RichTextBox时,新文本不会在RichTextBox ForeColor中着色

能否将我图表中的星号与X轴上一天中的第二位数字对齐?