deval oza
deval oza

Reputation: 79

Not able to get access token for marketo rest APi

I am using Marketo REST API.Here I am writing this code

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;
}

when I return the url from postData(), it will be print like this "https://299-BYM-827.mktorest.com/rest/v1/leads.json?access_token=" you can notice that I am not getting the access token. When i print the URl from the getToken(), it will be printed correct url and when I hit this URL into the browser I will get correct output. Thanks .

Upvotes: 1

Views: 596

Answers (1)

joev
joev

Reputation: 105

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;
}

is returning perfect API URL with valid access token, please check your bodyBuilder() if it is changing something.

Upvotes: 1

Related Questions