0

I am trying to create a contact through create a contact api. I have entered orgainzation id & oauth token. Api only generating one contact. Below are steps :

  1. Generating code by using self client in zoho api console
  2. After getting code from the above step, I make a POST request for the following URL with given params, to generate the access_token.From above post request I am getting access token and refresh token. Then I call create contact Api. But here is the problem after generating one contact.. it is showing my self client code is expired.I am also refreshing token but still getting same error. Why we need to connect it again? We have already access token and refresh token. My code is given below:
$token_url = "https://accounts.zoho.in/oauth/v2/token";
$token_payload = [
    "code" => $AUTHORIZATION_CODE,
    "client_id" => $CLIENT_ID,
    "client_secret" => $CLIENT_SECRET,
    "redirect_uri" => $REDIRECT_URI,
    "grant_type" => "authorization_code"
];

$ch = curl_init($token_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($token_payload));
$response = curl_exec($ch);
curl_close($ch);

$token_data = json_decode($response, true);

echo "<pre>";
print_r($token_data);


$access_token = $token_data["access_token"];  

$refresh_token = $token_data["refresh_token"]; 

// Step 2: Create a Contact using Zoho Books API
$create_contact_url = "https://books.zoho.in/api/v3/contacts";

$headers = [
    "Authorization: Zoho-oauthtoken $access_token",
    "Content-Type: application/json" // Set the correct content type
];

$contact_data = [
    "contact_name" => "John Doe",
    "email" => "johndoe@example.com"
    // Add more fields as needed
];

$ch = curl_init($create_contact_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($contact_data)); // Encode data as JSON
$response = curl_exec($ch);
curl_close($ch);

$contact_creation_response = json_decode($response, true);

if (isset($contact_creation_response["code"]) && $contact_creation_response["code"] === 3000) {
    echo "Contact created successfully with ID: " . $contact_creation_response["contact"]["contact_id"];
} else {
    echo "Error creating contact: " . print_r($contact_creation_response, true);
}

//But I got below error after generating one contact:
Error creating contact: Array
(
    [code] => 14
    [message] => The request could not be authenticated as the authentication value you entered is invalid. Enter a valid authentication value and try again.
)
    
swati
  • 1

0 Answers0