C#的图形API:尝试在Office 365上创建团队时出现BadGateway结果

时间:2020-04-18 13:16:28

标签: sharepoint azure-active-directory microsoft-graph-api microsoft-graph-teams

几天以来,我一直在研究一种解决方案,该解决方案在SharePoint中创建具有网站集的团队和小组。尝试将新团队创建到组中时,出现了与BadGateway相关的错误消息。

这是引发异常的代码:

 var team = new Team
                {
                    MemberSettings = new TeamMemberSettings
                    {
                        AllowCreateUpdateChannels = true
                    },
                    MessagingSettings = new TeamMessagingSettings
                    {
                        AllowUserEditMessages = true,
                        AllowUserDeleteMessages = true
                    },
                    FunSettings = new TeamFunSettings
                    {
                        AllowGiphy = true,
                        GiphyContentRating = GiphyRatingType.Strict
                    }
                };

                await graphClient.Groups[groupid].Team
                    .Request()
                    .PutAsync(team);

抛出的异常是:

Code: BadGateway
Message: Failed to execute backend request.
Inner error:
    AdditionalData:
    request-id: a3ecc097-6969-4263-84dd-e6c3fd60bd03
    date: 2020-04-18T13:02:43
ClientRequestId: a3ecc097-6969-4263-84dd-e6c3fd60bd03

更多评论:

  1. 执行的用户是网站集管理员(初始化此对象时)
var authManager = new OfficeDevPnP.Core.AuthenticationManager();
            ClientContext context = authManager.GetSharePointOnlineAuthenticatedContextTenant(AdminSiteUrl, username, pwd); 
  1. Azure中的客户端密钥ID和应用程序注册具有以下权限: App registrated permissions in Azure Active Directory

  2. 我用来声明Team对象的特定版本是Assembly Microsoft.Graph,使用Nuget的Version = 3.3.0.0。使用3.2.0.0

  3. 时遇到相同的问题

我知道可以直接使用REST API执行相同的方法,但是我不确定这个新版本(几天前发布的版本)是否存在错误。因此,我想知道如何解决此错误,或者我是否应该直接使用REST API进行移动(如果您有示例代码,那就太好了!)。感谢你!

2 个答案:

答案 0 :(得分:0)

这是完整的代码:

string ClassSiteUrl = "https://***********.sharepoint.com/sites/*******";
string AdminSiteUrl = "https://************-admin.sharepoint.com";

string clientId = "*********************"; //e.g. 01e54f9a-81bc-4dee-b15d-e661ae13f382
string clientSecret = @"*********************";
string tenantID = "*********************";

var pwd = "*********************";
var username = "*********************";

var authManager = new OfficeDevPnP.Core.AuthenticationManager();
ClientContext context = authManager.GetSharePointOnlineAuthenticatedContextTenant(AdminSiteUrl, username, pwd);
Tenant tenant = new Tenant(context);

GroupCreationParams optionalParams = new GroupCreationParams(tenant.Context);
optionalParams.Description = "description";
optionalParams.CreationOptions = new string[] { "SharePointKeepOldHomepage" };

tenant.CreateGroupForSite(ClassSiteUrl, "*********************", "*********************", true, optionalParams);
context.ExecuteQuery();

//get group id
ClientContext classicalsitectx = authManager.GetSharePointOnlineAuthenticatedContextTenant(ClassSiteUrl, username, pwd);
classicalsitectx.Load(classicalsitectx.Site);
classicalsitectx.ExecuteQuery();
var groupid = classicalsitectx.Site.GroupId.ToString(); 

// Link group to a team
IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
    .Create(clientId)
    .WithTenantId(tenantID)
    .WithClientSecret(clientSecret)
    .Build();

ClientCredentialProvider authProvider = new ClientCredentialProvider(confidentialClientApplication);
GraphServiceClient graphClient = new GraphServiceClient(authProvider);

try
{

    var team = new Team
    {
        MemberSettings = new TeamMemberSettings
        {
            AllowCreateUpdateChannels = true
        },
        MessagingSettings = new TeamMessagingSettings
        {
            AllowUserEditMessages = true,
            AllowUserDeleteMessages = true
        },
        FunSettings = new TeamFunSettings
        {
            AllowGiphy = true,
            GiphyContentRating = GiphyRatingType.Strict
        },
        ODataType = null
    };

    await graphClient.Groups[groupid].Team
        .Request()
        .PutAsync(team);
}
catch (ServiceException e)
{
    Console.WriteLine("This program is expected to throw WebException on successful run." +
                        "\n\nException Message :" + e.Message);
}
catch (Exception ex)
{

    Console.WriteLine("This program is expected to throw WebException on successful run." +
                       "\n\nException Message :" + ex.Message);
}

答案 1 :(得分:0)

我正在使用Graph API SDK 3.1.0。尝试在团队对象中设置ODataType = null

            var team = new GraphApi.Team
            {      
                MemberSettings = new GraphApi.TeamMemberSettings
                {
                    AllowCreateUpdateChannels = true,
                    ODataType = null
                },
                MessagingSettings = new GraphApi.TeamMessagingSettings
                {
                    AllowUserEditMessages = true,
                    AllowUserDeleteMessages = true,
                    ODataType = null
                },
                FunSettings = new GraphApi.TeamFunSettings
                {
                    AllowGiphy = true,
                    GiphyContentRating = GraphApi.GiphyRatingType.Strict,
                    ODataType = null
                },
                ODataType = null
            };
相关问题