You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET HTTP API对接Actions On Google时令牌返回JSON解析错误求助

问题:Actions On Google账号链接时JSON解析失败

背景

使用ASP.NET HTTP API作为Actions On Google的fulfillment,在发送令牌时收到错误提示:Can't parse the response. The response needs to be JSON format,已确认expires_in为整数,但问题仍未解决。

发送的JSON内容

{"token_type":"Bearer","access_token":"edeaa27e-12b7-43a1-bc7c-e6bbf9af71c3","refresh_token":"4344383b-cf07-4d12-a5c2-44b6481f5f48","expires_in":86400}

错误日志信息

{
    "insertId": "1o7lj2bmu",
    "jsonPayload": {
        "@type": "type.googleapis.com/google.identity.accountlinking.type.AccountLinkingError",
        "errorReason": "Can't parse the response. The response needs to be JSON format.",
        "response": {
            "body": "\"{\"token_type\":\"Bearer\",\"access_token\":\"edeaa27e-12b7-43a1-bc7c-e6bbf9af71c3\",\"refresh_token\":\"4344383b-cf07-4d12-a5c2-44b6481f5f48\",\"expires_in\":86400}\"",
            "status": 200
        },
        "step": "AUTH_CODE_EXCHANGE"
    },
    "logName": "projects/smartlightproject-f47f4/logs/accountlinking-pa.googleapis.com%2Ferror",
    "receiveTimestamp": "2022-07-12T12:32:40.739532642Z",
    "resource": {},
    "severity": "ERROR",
    "timestamp": "2022-07-12T12:32:40.539Z"
}

当前改进后的代码

var result_acceess_token = "edeaa27e-12b7-43a1-bc7c-e6bbf9af71c3";
var result_refresh_token = "4344383b-cf07-4d12-a5c2-44b6481f5f48";
var expires_expires_in = 86400;

var resultObj = new
{
     token_type = "Bearer",
     access_token = result_acceess_token,
     refresh_token = result_refresh_token,
     expires_in = expires_expires_in
};

return Ok(resultObj);

此前曾尝试使用JsonConvert序列化JSON,并返回JsonResult或原始字符串值。


问题分析及解决方案

从错误日志的response.body字段可以看到,返回的JSON被额外包裹了一层双引号,变成了带转义的字符串("{"token_type":...}"),这导致Actions On Google无法将其识别为合法的JSON对象。

出现该问题的常见原因:

  • 手动序列化JSON为字符串后直接返回,ASP.NET自动对字符串进行二次序列化,导致外层多了引号
  • 返回字符串时未正确设置响应的Content-Type为application/json

你当前改进后的代码是正确的处理方式:

  1. 直接返回匿名对象,ASP.NET会自动将其序列化为标准JSON
  2. 框架会自动设置响应的Content-Type为application/json
  3. 避免了二次序列化导致的格式错误

如果之前的错误是因手动序列化后返回字符串(如下示例),需彻底放弃这种写法:

var json = JsonConvert.SerializeObject(resultObj);
return Ok(json);

同时需确认API端点无额外全局过滤器或中间件修改响应内容,避免二次处理破坏JSON格式。


内容的提问来源于stack exchange,提问作者Dawid Krzysiak

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.26 08:45:39