无法获得marketo rest APi的访问令牌

时间:2015-09-01 08:33:43

标签: php rest curl marketo

我正在使用Marketo REST API。我正在编写此代码

class UpsertLeads{
private $host = "";//CHANGE ME
private $clientId = "";//CHANGE ME
private $clientSecret = "";//CHANGE ME
public $input; //an array of lead records as objects
public $lookupField; //field used for deduplication
public $action; //operation type, createOnly, updateOnly, createOrUpdate, createDuplicate

public function postData(){
    $url = $this->host . "/rest/v1/leads.json?access_token=" . $this->getToken();
    $ch = curl_init($url);
    $requestBody = $this->bodyBuilder();
    print_r($requestBody);
    curl_setopt($ch,  CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('accept: application/json','Content-Type: application/json'));
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $requestBody);
    curl_getinfo($ch);
    $response = curl_exec($ch);
    return $response;
}

private function getToken(){
    $ch = curl_init($this->host . "/identity/oauth/token?grant_type=client_credentials&client_id=" . $this->clientId . "&client_secret=" . $this->clientSecret);
    curl_setopt($ch,  CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('accept: application/json',));
    $response = json_decode(curl_exec($ch));
    curl_close($ch);
    $token = $response->access_token;
    return $token;
}

当我从postData()返回url时,它会像这样打印 “https://299-BYM-827.mktorest.com/rest/v1/leads.json?access_token=” 你可以注意到我没有获得访问令牌。 当我从getToken()打印URl时,它将被打印出正确的URL,当我在浏览器中点击此URL时,我将获得正确的输出。 谢谢。

1 个答案:

答案 0 :(得分:1)

public function postData(){
    $url = $this->host . "/rest/v1/leads.json?access_token=" . $this->getToken();
    $ch = curl_init($url);
    //$requestBody = $this->bodyBuilder();
    //print_r($requestBody);
    curl_setopt($ch,  CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('accept: application/json','Content-Type: application/json'));
    curl_setopt($ch, CURLOPT_POST, 1);
    //curl_setopt($ch, CURLOPT_POSTFIELDS, $requestBody);
    curl_getinfo($ch);
    $response = curl_exec($ch);
    //echo"<pre>";print_r($response);exit();
    return $url;
}

返回带有效访问令牌的完美API网址,如果更改内容,请检查您的bodyBuilder()。