首页 > 解决方案 > Google 幻灯片批量更新错误 - “收到无效的 JSON 有效负载。未知名称”和“无法绑定查询参数”

问题描述

我正在尝试写入幻灯片元素

我不断收到“收到无效的 JSON 有效负载。未知名称”和“无法绑定查询参数。字段”的错误响应

我的要求是

 {
  "requests": [
    {
      "deleteText": {
        "objectId": "g33c4ea3b90_0_9",
        "textRange": {
          "type": "FROM_START_INDEX",
          "startIndex": 0
        }
      }
    },
    {
      "insertText": {
        "objectId":  "g33c4ea3b90_0_9",
        "text": "$8.23",
        "insertionIndex": 0
      }
    }
  ]
}

我发布到:https ://slides.googleapis.com/v1/presentations/PRESENTATION_ID:batchUpdate?access_token=ACCESS_TOKEN

我在 PHP 中手动卷曲请求:

 $params =  '
 {
  "requests": [
    {
      "deleteText": {
        "objectId": "'.$page_object_id.'",
        "textRange": {
          "type": "FROM_START_INDEX",
          "startIndex": 0
        }
      }
    },
    {
      "insertText": {
        "objectId":  "'.$page_object_id.'",
        "text": "$8.23",
        "insertionIndex": 0
      }
    }
  ]
}
';

$options = array(
    CURLOPT_RETURNTRANSFER => true, // return web page
    CURLOPT_HEADER => false, // don't return headers
    CURLOPT_FOLLOWLOCATION => true, // follow redirects
    CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
    CURLOPT_ENCODING => "", // handle compressed
    CURLOPT_AUTOREFERER => true, // set referrer on redirect
    CURLOPT_CONNECTTIMEOUT => 120, // time-out on connect
    CURLOPT_TIMEOUT => 120, // time-out on response
    CURLOPT_CUSTOMREQUEST => 'POST' ,   

    CURLOPT_SSL_VERIFYPEER =>  false,
    CURLOPT_SSL_VERIFYHOST =>  false ,
    CURLINFO_HEADER_OUT =>  true ,
    CURLOPT_VERBOSE => true,
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS =>  $params 

);

 $ch = curl_init($url);
curl_setopt_array($ch, $options);

$content = curl_exec($ch);

curl_close($ch);

  $obj = json_decode($content, true);
  print_r( $obj );

当我从API Explorer调用它时,它可以完美运行,但不能从我的服务器调用。

标签: google-apis-explorergoogle-slides-api

解决方案


所以问题是在请求参数json字符串中使用双引号而不是单引号。

所以以下工作:

 {
  'requests': [
    {
      'deleteText': {
        'objectId': 'g33c4ea3b90_0_9',
        'textRange': {
          'type': 'FROM_START_INDEX',
          'startIndex': 0
        }
      }
    },
    {
      'insertText': {
        'objectId': 'g33c4ea3b90_0_9',
        'text': '$44.23',
        'insertionIndex': 0
      }
    }
  ]
}

我已经为此工作了两天。


推荐阅读