Marketo REST API资产令牌是否有效?

时间:2017-05-15 13:27:12

标签: php rest api token marketo

我正在关注文档的这一部分:http://developers.marketo.com/rest-api/assets/tokens/并且我总是收到以下错误:字段不能为空。

有人让它成功吗?

public function create_token($folder_id,$name,$content,$folder_type = 'Program')
{
    $folder_id = intval($folder_id);
    $endpoint = 'rest/asset/v1/folder/'.$folder_id.'/tokens';
    $body = new stdClass();
    $body->folderType = $folder_type;
    $body->name = $name;
    $body->type = 'rich text';
    $body->value = $content;
    $body_encoded = json_encode($body);

    echo $url = $this->url . $endpoint . ".json?access_token=" . self::$token;

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: x-www-form-urlencoded'));
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $body_encoded);
    $response = curl_exec($ch);
    curl_close($ch);
    return json_decode($response);
}

Content-Type标题的原因是Marketo的建议:https://www.screencast.com/t/CL5ZtPo1o

这是我一直得到的要求的答案:

object(stdClass)#1934 (4) {


["success"]=>
  bool(false)
  ["warnings"]=>
  array(0) {
  }
  ["errors"]=>
  array(4) {
    [0]=>
    object(stdClass)#1935 (2) {
      ["message"]=>
      string(20) "name cannot be null."
      ["code"]=>
      string(3) "701"
    }
    [1]=>
    object(stdClass)#1936 (2) {
      ["message"]=>
      string(20) "type cannot be null."
      ["code"]=>
      string(3) "701"
    }
    [2]=>
    object(stdClass)#1937 (2) {
      ["message"]=>
      string(101) "Token type is either null, blank or invalid. Please refer to the documentation for valid token types."
      ["code"]=>
      string(3) "701"
    }
    [3]=>
    object(stdClass)#1938 (2) {
      ["message"]=>
      string(21) "value cannot be null."
      ["code"]=>
      string(3) "701"
    }
  }
  ["requestId"]=>
  string(16) "11d1#15b49284636"
}

1 个答案:

答案 0 :(得分:0)

您不必将令牌字段发布为JSON对象:json_encode($body)
字段作为请求参数或常规表单传递

此请求对我有用:

POST https://123-FOO-456.mktorest.com/rest/asset/v1/folder/1039/tokens.json?value=TestTokenValue&folderType=Program&name=TestToken&type=text

在这种情况下,您也不必指定内容类型Content-Type: x-www-form-urlencoded

我不是PHP dev,但您可以在此处查看如何发布表单数据的示例 - PHP + curl, HTTP POST sample code?