使用客户端凭据进行服务呼叫服务

时间:2016-05-25 05:04:37

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

我尝试使用下面的代码在office 365中为组创建别名,但它显示了一些错误以解决此问题。我试图使用service来服务调用方法。我得到了令牌。如何检查其有效性?是否可以使用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中为组创建别名

1 个答案:

答案 0 :(得分:1)

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

作为一种解决方法,我们可以使用您想要的特定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提交反馈。