You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过编程方式修改Microsoft Teams工作地点?

动态设置Microsoft Teams工作地点时遇到401未授权问题

需求与已发现信息

我正在开发一款简单应用,目标是根据当前网络IP地址动态设置Microsoft Teams的工作地点。目前没有对应的Graph API可用,但通过分析浏览器端Teams的网络活动,找到了可用的API端点:

  • 请求方式:PUT
  • 端点地址:https://presence.teams.microsoft.com/v1/me/workLocation/
  • 请求体示例:
{
  "location": 2,
  "expirationTime": "2024-07-15T21:59:59.999Z"
}

其中location的取值定义:

  • 0:无
  • 1:工作
  • 2:居家

该端点受Bearer令牌保护,Teams网页端可正常传递有效令牌,但我无法获取能调用该端点的有效令牌。我了解到有通过Power Automate实现的方案可正常运行,但认证流程完全由Power Automate管理,无法复用其认证逻辑。

尝试过的方案与问题

我通过MSAL认证流程(已在Azure中注册应用)获取令牌,尝试了不同的作用域并为应用注册分配了多种权限,但调用端点始终返回401未授权错误。

.NET控制台测试代码

using System;
using Microsoft.Identity.Client;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json; 

namespace TeamsTokenApp
{
    class Program
    {
        static async Task Main(string[] args)
        {
            var clientId = "MY_CLIENT_ID";
            var tenantId = "MY_TENANT_ID";
            var authority= $"https://login.microsoftonline.com/{tenantId}";

            var app = PublicClientApplicationBuilder.Create(clientId)
                .WithAuthority(authority)
                .WithDefaultRedirectUri()
                .Build();

            string[] scopes = { "https://graph.microsoft.com/.default" };

            try
            {
                var accounts = await app.GetAccountsAsync();
                var result = await app.AcquireTokenInteractive(scopes)
                    .ExecuteAsync();

                Console.WriteLine($"Token: {result.AccessToken}");

                var token = result.AccessToken;
                var expirationTime = DateTime.UtcNow.AddHours(5).ToString("o"); // 当前时间+5小时,ISO 8601格式

                var requestBody = new
                {
                    location = 2,
                    expirationTime = expirationTime
                };

                var jsonRequestBody =JsonConvert.SerializeObject(requestBody);
                var content = new StringContent(jsonRequestBody, Encoding.UTF8, "application/json");

                using (var httpClient = new HttpClient())
                {               
                    httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);

                    var response = await httpClient.PutAsync("https://presence.teams.microsoft.com/v1/me/workLocation/", content);

                    if (response.IsSuccessStatusCode)
                    {
                        Console.WriteLine("请求成功。");
                    }
                    else
                    {
                        Console.WriteLine($"请求失败。状态码: {response.StatusCode}, 原因: {response.ReasonPhrase}");
                    }
                }
            }
            catch (MsalUiRequiredException)
            {
                Console.WriteLine("需要交互式登录");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"错误: {ex.Message}");
            }
        }
    }
}

PowerShell测试脚本

$clientId = "<MY_CLIENT_ID>"
$tenantId = "<MY_TENANT_ID>"
#$scope = "https://graph.microsoft.com/.default"
#$scope = "https://presence.teams.microsoft.com/.default"
$scope = "https://<MY_TENANT_ID>.sharepoint.com/.default"

$authResult = Get-MsalToken -ClientId $clientId -TenantId $tenantId -Scopes $scope -Interactive

if ($authResult.AccessToken) {
    Write-Output "Bearer $($authResult.AccessToken)"
} else {
    Write-Output "获取令牌失败"
}

我尝试了多种作用域组合,但都无法获取到能成功调用该端点的令牌,恳请大家提供解决思路或方案。


内容的提问来源于stack exchange,提问作者Dan Grow

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 19:13:16