HubSpot / hubspot-api-php

HubSpot API PHP Client Libraries for V3 version of the API
Apache License 2.0
192 stars 80 forks source link

Creating Leads: Sample code from documentation does not work #371

Closed harpreetsb closed 3 weeks ago

harpreetsb commented 1 month ago

Hi, We are using PHP to create contacts and leads in hubspot. Though the contact was created with sample code easily I am trying to create a lead from a contact. I used the sample code from page the code used is:

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://api.hubapi.com/crm/v3/objects/leads",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "{\"associations\":[{\"types\":[{\"associationCategory\":\"HUBSPOT_DEFINED\",\"associationTypeId\":0}],\"to\":{\"id\":\"string\"}}],\"properties\":{\"additionalProp1\":\"string\",\"additionalProp2\":\"string\",\"additionalProp3\":\"string\"}}",
  CURLOPT_HTTPHEADER => array(
    "accept: application/json",
    "authorization: Bearer YOUR_ACCESS_TOKEN",
    "content-type: application/json"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

The response I get is:

{ "status": "error", "message": "This app hasn't been granted all required scopes to make this call. Read more about required scopes here: https://developers.hubspot.com/scopes.", "correlationId": "1b86b036-0843-4ec5-bbb7-ad51eeb4bfea", "errors": [ { "message": "One or more of the following scopes are required.", "context": { "requiredGranularScopes": [ "crm.objects.leads.write" ] } } ], "links": { "scopes": "https://developers.hubspot.com/scopes" }, "category": "MISSING_SCOPES" }

Attached is the screenshot from the scopes in created app image

Please help

harpreetsb commented 1 month ago

UPDATE: PHP 8.2

justingivens commented 1 month ago

@harpreetsb it could be a couple of things.

Randomly noticed that you didn't have the correct AssocationTypeID in your data.

This also isn't related to the PHP SDK.

If you need additional assistance with the API in general, I'd recommend connecting on the (HubSpot Developer Slack Community](https://developers.hubspot.com/slack)

ksvirkou-hubspot commented 3 weeks ago

Hi @harpreetsb You must have Sales Hub Professional/Enterprise subscription, otherwise, you won't be able to use it. Do you have Sales Hub Professional/Enterprise subscription?