4

我想使用 GraphClient 向用户添加扩展。我在任何地方都找不到合适的 C# 代码。任何人都可以帮忙吗?

Dictionary<string, object> addData = new Dictionary<string, object> {
    {
    "HoloFlag",
    currentUser.UserPrincipalName
    }
};

var extPatchObject = new OpenTypeExtension();

extPatchObject.ExtensionName = "com.holobeam3.extension";
extPatchObject.AdditionalData = addData;

try {
    var extension = await _graphClient
        .Me
        .Extensions
        .Request()
        .AddAsync(extPatchObject);
    Debug.Log(extension);
} catch (Exception ex) {
    Debug.Log(ex.Message);
}

这是我到目前为止所尝试的。Me这将返回“拒绝访问”异常,但访问现有扩展或用户的其他端点没有问题。

4

2 回答 2

6

In order to add a new extension follow this code:

 _graphClient = new GraphServiceClient(new DelegateAuthenticationProvider(async requestMessage =>
            {
                requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", AccessToken);
            }));
Dictionary<string, object> addData = new Dictionary<string, object>
                {
                    {"Key","Value" }
                };
                var extObject = new OpenTypeExtension();
                extObject.ExtensionName = "TestExtension";
                extObject.AdditionalData = addData;
                try
                {
                    await _graphClient.Users["usernamegoeshere"].Extensions.Request().AddAsync(extObject);
                }

In order to fetch an extension follow this code:

var userExtensions = await _graphClient.Me.Extensions["nameofExtension"].Request().GetAsync();
于 2018-10-10T04:51:35.057 回答
1
GraphServiceClient client = AuthHelper.GetAuthClient();

var currentUser = await client.Me.Request().GetAsync();
string id = currentUser.Id;

var test = await client.Me.Extensions.Request().GetAsync();

Console.WriteLine(currentUser.DisplayName);
Console.WriteLine("Click to update extension");
Console.ReadLine();

Dictionary<string, object> addData = new Dictionary<string, object>()
{
    {"Key", "Value"}
};

var extObject = new OpenTypeExtension()
{
    ExtensionName = "testExtension",
    AdditionalData = addData
};

try
{
    await client.Me.Extensions.Request().AddAsync(extObject);
}
catch (Exception ex)
{
    //
}
于 2018-11-09T13:09:57.307 回答