【问题标题】:Service to Service Calls Using Client Credentials使用客户端凭据的服务到服务调用
【发布时间】:2025-11-26 19:00:01
【问题描述】:

我尝试使用以下代码在 Office 365 中为组创建别名,但它显示了一些错误。如何解决此问题。我尝试使用服务到服务调用方法。我得到了生成的令牌。如何检查其有效与否?是否可以使用 api 为没有 powershell 选项的组创建别名?如果没有好心的建议我其他选择..

string clientId = "************";
string clientsecret = "******";
string tenantId = "********";
//string resourceUri = "http://office.microsoft.com/outlook/";
string redirectUri = "https://login.live.com/oauth20_desktop.srf";
var authUri = "https://login.windows.net/" + tenantId + "/oauth2/authorize/";
var RESOURCE_URL = "https://graph.windows.net";

HttpClient client = new HttpClient();
var authContext = new AuthenticationContext(authUri);
var credential = new ClientCredential(clientId: clientId, clientSecret: clientsecret);
var result = authContext.AcquireTokenAsync(RESOURCE_URL, credential).Result;
client.DefaultRequestHeaders.Add("Authorization", "bearer " + result.AccessToken);
string content = @"{
              'displayName': 'mailgrouptest',
              'groupTypes': ['Unified'],
              'mailEnabled': true,
              'mailNickname': 'mailalias1',
              'securityEnabled': false
      }";

 var httpContent = new StringContent(content, Encoding.GetEncoding("utf-8"), "application/json");
 var response = client.PostAsync("https://graph.microsoft.com/v1.0/groups", httpContent).Result;
 Console.WriteLine(response.Content.ReadAsStringAsync().Result);

当我在控制台中运行此代码时,它会显示这样的错误......是令牌的问题吗?还是租户 ID?

{
  "error": {
"code": "InvalidAuthenticationToken",
"message": "Access token validation failure.",
"innerError": {``
  "request-id": "*****-***-",
  "date": "2016-05-25T04:53:08"
    }
  }
}

请建议我在 api 中为组创建别名

【问题讨论】:

    标签: powershell office365api azure-ad-graph-api


    【解决方案1】:

    目前无法使用 Microsoft Graph 更新群组的 mailNickName。

    作为一种解决方法,我们可以使用您想要的特定 mailNickName 创建一个新组并使用新组。以下是使用 mailNicekName 创建组的代码供您参考:

            string clientId = "";
            string clientsecret = "";
            string tenant = "yourdomain.onmicrosoft.com";
    
            var authUri = "https://login.microsoftonline.com/"+tenant+"/oauth2/token";
            var RESOURCE_URL = "https://graph.microsoft.com";
    
            HttpClient client = new HttpClient();
            var authContext = new AuthenticationContext(authUri);
            var credential = new ClientCredential(clientId: clientId, clientSecret: clientsecret);
            var result = authContext.AcquireTokenAsync(RESOURCE_URL, credential).Result;
            client.DefaultRequestHeaders.Add("Authorization", "bearer " + result.AccessToken);
    
            string content = @"{
     'description': 'description-value',
      'displayName': 'displayName-value',
      'groupTypes': [
        'Unified'
      ],
      'mailEnabled': true,
      'mailNickname': 'mailNickname-value',
      'securityEnabled': false
    }";
            var httpContent = new StringContent(content, Encoding.GetEncoding("utf-8"), "application/json");
            //var response = client.GetAsync("https://graph.microsoft.com/v1.0/groups").Result;
            var response = client.PostAsync("https://graph.microsoft.com/v1.0/groups",httpContent).Result;
            Console.WriteLine(response.Content.ReadAsStringAsync().Result);
    

    更多关于 Goupr REST API 的详细信息,请参考here

    对于错误“InvalidAuthenticationToken”,您请求访问令牌的资源不正确。要使用 Microsoft Graph API,我们需要使用“https://graph.microsoft.com”而不是“https://graph.windows.net”来指定资源。

    另外,如果您希望群组的mailNickName 是可更新的,您可以尝试提交here 的反馈。

    【讨论】:

    • 谢谢。现在它没有错误了。我怀疑是否可以使用图形 API 以相同的方式为现有电子邮件创建电子邮件别名?
    • 是否可以使用 api 为用户电子邮件添加多个别名 [mailNickName]? [不是在创建时而是在我们想添加更多别名时]
    • 没有。用户只有一个 mailNickName。可以参考document,这个参数的类型是string而不是collection。
    • 我可以随时更新 otherMails 选项吗?
    • 您想更新哪个选项?在使用 REST 创建用户后,[mailNickName] 也无法更新。