首页 > 解决方案 > POST 用户导入 v2 内部服务器错误

问题描述

我正在为我的公司开发一个全自动管道,我们在 BIM360 上使用不同的 API 自动设置项目、添加用户和上传文件。在添加用户的阶段,我收到 500 内部服务器错误:

{"code":2000,"message":"no implicit conversion of String into Integer"}

我们使用的是两条腿的身份验证方法,因此标头如下所示:

授权: Bearer <token>(拥有account:write权限)

x-user-id: ************ (我的管理员帐户的 uid)

内容类型:应用程序/json

请求内容是这样的:

@"{
  ""email"": """ + ***@********.** + @""",
  ""services"": {
            ""document_management"": {
                ""access_level"": """ + admin+ @"""
            },
            ""project_administration"": {
                ""access_level"": """ + admin+ @"""
            }
        },
  ""industry_roles"": []}";

我只是似乎无法弄清楚我做错了什么。希望可以有人帮帮我。

编辑:此请求的完整代码

public async static Task<HttpStatusCode> AddUserToProjectEmail(string projectId, string accountId, string accessToken, string userToAddEmail, string userPrivilege, string adminUserId)
    {
        using (HttpClient httpClient = new HttpClient())
        {
            using (HttpRequestMessage request = new HttpRequestMessage())
            {
                //Documentation for what to put in the Http POST: https://forge.autodesk.com/en/docs/bim360/v1/reference/http/projects-project_id-users-import-POST/
                request.Method = new HttpMethod("POST");
                request.RequestUri = new Uri("https://developer.api.autodesk.com/hq/v2/regions/eu/accounts/" + accountId + "/projects/" + projectId + "/users/import");

                //Make the request payload
                string jsonPayload = AddPayloadToUserAddEmail(userToAddEmail, userPrivilege);
                request.Content = new StringContent(jsonPayload);

                request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
                request.Headers.Add("x-user-id", adminUserId);
                request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);

                //Send request
                var response = await httpClient.SendAsync(request);

                return response.StatusCode;
            }
        }
    }

以及请求有效载荷方法:

private static string AddPayloadToUserAddEmail(string userToAddEmail, string userPrivilege)
    {

        string payload = @"{
                          ""email"": """ + userToAddEmail + @""",
                          ""services"": {
                                    ""project_administration"": {
                                        ""access_level"": """ + userPrivilege + @"""
                                    },
                                    ""document_management"": {
                                        ""access_level"": """ + userPrivilege + @"""
                                    }
                                },
                          ""industry_roles"": []
                        }";

        return payload;
    }

我已经通过 BIM360 上的 URL 检查了所有 ID,但是我认为无法检查我的帐户的 Uid。

编辑 2:我应该注意,在添加 x-user-id 标头之前,我遇到了一个不同的错误,它只是说禁止,这是有道理的。这使我认为它与 x-user-id 标头有关,但我无法弄清楚。

标签: autodesk-forge

解决方案


不要像我一样忘记将有效负载包装到一个数组中,如文档中所述。使用它作为有效载荷工作

@"[{
                      ""email"": """ + userToAddEmail + @""",
                      ""services"": {
                                ""project_administration"": {
                                    ""access_level"": """ + userPrivilege + @"""
                                },
                                ""document_management"": {
                                    ""access_level"": """ + userPrivilege + @"""
                                }
                            },
                      ""industry_roles"": []
                    }]";

推荐阅读