office365 - 由于字符无效,无法反序列化来自 Graph API 的错误响应

标签 office365 office365api microsoft-graph-api

我从 Graph API 得到的错误响应如下。

{ “错误”: { “代码”:“ErrorItemNotFound”, "message": "在商店中找不到指定的对象。", “内部错误”:{ “请求ID”:“c2b32b83-5ccf-4385-bee1-33afcc31deb0”, “日期”:“2016-07-25T00:25:16” } } }

当我尝试将响应反序列化为我自己的错误对象时,我收到错误

Unexpected character encountered while parsing value: . Path '', line 0, position 0.

复制上面的 JSON 并尝试使用此工具进行验证 https://jsonformatter.curiousconcept.com/

还有人遇到同样的问题吗?


更新

抱歉各位回复晚了。下面是我正在使用的确切代码。我刚刚再次测试过,但遇到了同样的错误。

Enpoint URL:https://graph.microsoft.com/v1.0/users/MY_USER@EMAIL/events/

// New rest client with the destination URL.
        var client = new RestClient(url);

        // Type of request
        var request = new RestRequest(requestType);

        // Headers
        request.AddHeader("Authorization", "Bearer " + accessToken);
        request.AddHeader("Content-Type", "application/json");


        // Content serialized in a json format  
        if (requestType == Method.POST || requestType == Method.PATCH || requestType == Method.PUT)
        {
            var jsonBody = JsonConvert.SerializeObject(requestContent);
            request.AddParameter("application/json", jsonBody, ParameterType.RequestBody);
        }


        var response = await client.ExecuteTaskAsync(request);

请求正文

{"Id":null,"Subject":"Maths","UserEmailAddress":"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7300121e031f163300121e031f165d1c1d1e1a10011c001c15075d101c1e" rel="noreferrer noopener nofollow">[email protected]</a>","Start":{"DateTime":"2016-09-1T10:11:56","TimeZone":"Pacific/Auckland"},"End":{"DateTime":"2016-09-1T11:11:56","TimeZone":"Pacific/Auckland"},"Location":null,"Body":null,"ReminderMinutesBeforeStart":60,"IsReminderOn":true}

最佳答案

您发布的 JSON 文本实际上包含 3 字节 UTF-8 BOM (0xEF 0xBB 0xBF)。这将导致大多数 JSON 解析器失败。

解决您的问题的一个简单方法是检测 BOM 并在解析之前将其剥离。

假设使用 JavaScript,类似下面的内容将解决您眼前的问题。

<!-- language: lang-js -->
// Reading the string into JavaScript will convert the BOM from the 3-byte
// version to the 2-byte version (0xFEFF).
json = json.replace(/^\uFEFF/, '');

-

更多

首先弄清楚为什么这会被附加到所产生的错误中会更有值(value)。您是如何发出导致此错误的请求的?如果这是来自网页,那么页面编码是什么?您是否设置了可能与所需编码相关的任何其他 HTTP 请求 header ?

关于office365 - 由于字符无效,无法反序列化来自 Graph API 的错误响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38558740/

相关文章:

python - 如何使用基于 Web 应用程序的身份验证在 Python Django 中使用 Microsoft graph api (python-o365) 发送和读取邮件?

outlook - 无法使用Office 365 REST API登录电子邮件

javascript - 如何从邮箱获取 Office 365 Outlook 文件夹名称

azure - 如何限制 Azure 应用程序注册的 Microsoft Graph 权限?

c# - MailKit C# ImapClient.Connect() 到 Office 365 生成异常 : "The remote certificate is invalid according to the validation procedure"

excel - Office 365 中 Excel 中 Power Pivot 的计算列中正确的 DAX GROUPBY 语法是什么

c# - 无法使用 GoDaddy 配置的 Office 365 帐户中的 C# 代码发送电子邮件

ms-access - 与Office 365一起安装Access Runtime 2016

azure - 使用标准用户帐户根据订阅下载邮件

python - 我可以在 OneDrive/Sharepoint 中创建不过期 token 吗?