phpsymfonygoogle-api-php-clientgoogle-people-api

Error 400: readMask is required. Please specify one or more valid paths


I'm traying to create a new contact in my google account using GoogleAPIClient library.

My function code's is this:

    public function createContact(Google_Client $client, $nombre, $telefonos, $correo, $biografia)
    {
        $service = new Google_Service_PeopleService($client);

        // Crear una nueva persona y establecer su nombre, número de teléfono y biografía
        $person = new Google_Service_PeopleService_Person();

        $name = new Google_Service_PeopleService_Name();
        $name->setGivenName($nombre);
        $person->setNames([$name]);

        // Crear una lista para almacenar los números de teléfono
        $phoneNumbersList = [];
        foreach ($telefonos as $telefono){
            $phoneNumber = new Google_Service_PeopleService_PhoneNumber();
            $phoneNumber->setValue($telefono);
            // Añadir el número de teléfono a la lista
            $phoneNumbersList[] = $phoneNumber;
        }
        // Establecer los números de teléfono del contacto a la lista que acabamos de crear
        $person->setPhoneNumbers($phoneNumbersList);

        $emailAddress = new Google_Service_PeopleService_EmailAddress();
        $emailAddress->setValue($correo);
        $person->setEmailAddresses([$emailAddress]);

        $biography = new Google_Service_PeopleService_Biography();
        $biography->setValue($biografia);
        $person->setBiographies([$biography]);

        // Crear el contacto
        $createdContact = $service->people->createContact($person, [
            'personFields' => 'names,phoneNumbers,emailAddresses,biographies'
        ]);

        // Obtener el ID del contacto creado
        $contactId = $createdContact->getResourceName();

        return $contactId;
    }

When I execute this function it give me this response:

{
"error": {
"code": 400,
"message": "readMask is required. Please specify one or more valid paths. Valid paths are documented at https://developers.google.com/people/api/rest/v1/people/get.",
"errors": [
{
"message": "readMask is required. Please specify one or more valid paths. Valid paths are documented at https://developers.google.com/people/api/rest/v1/people/get.",
"domain": "global",
"reason": "badRequest"
}
],
"status": "INVALID_ARGUMENT"
}
}

I don't undesrtand why I'm getting this error, I already have a readMask. I also try create contacts using this other executions:

$createdContact = $service->people->createContact($person, [
    'requestMask.includeField' => 'person.metadata,person.names,person.phoneNumbers,person.emailAddresses,person.biographies'
]);

$service->people->createContact($person, ['readMask' => 'names,emailAddresses,phoneNumbers,biographies']);


$service->people->createContact($person, ['personFields' => ['names', 'emailAddresses', 'phoneNumbers', 'biographies']]);

The response given is always the same.

¿Do I need something else? ¿There is something wrong wiht my function?


Solution

  • I had the same error somewhere in the process. The following code works for me. Maybe this brings you to the right track or you can build up on this:

    <?php
    use Google\Service\PeopleService\EmailAddress;
    use Google\Service\PeopleService\Name;
    use Google\Service\PeopleService\Person;
    use Google\Service\PeopleService\PhoneNumber;
    
    $client = new Google_Client();
    // Authorized scoped here to resolve Client is unauthorized to retrieve access tokens using this method, or client not authorized for any of the scopes requested
    // https://developers.google.com/identity/protocols/oauth2/service-account?hl=en#delegatingauthority
    // https://admin.google.com/u/3/ac/owl/domainwidedelegation
    // Available scopes https://developers.google.com/identity/protocols/oauth2/scopes?hl=de
    $client->setAuthConfig('myconfig.json');
    $client->addScope([
        'https://www.googleapis.com/auth/userinfo.profile',
        'https://www.googleapis.com/auth/contacts',
        'https://www.googleapis.com/auth/directory.readonly'
    ]);
    $client->setSubject('myemail@example.com');
    $client->setAccessType("offline");
    $client->setApprovalPrompt('force');
    
    $service = new Google\Service\PeopleService($client);
    
    $all_fields = 'addresses,ageRanges,biographies,birthdays,calendarUrls,clientData,coverPhotos,emailAddresses,events,externalIds,genders,imClients,interests,locales,locations,memberships,metadata,miscKeywords,names,nicknames,occupations,organizations,phoneNumbers,photos,relations,sipAddresses,skills,urls,userDefined';
    
    
    $person = new Person();
    
    $email = new EmailAddress();
    $email->setValue('asd@example.com');
    $person->setEmailAddresses($email);
    
    $phone_number = new PhoneNumber();
    $phone_number->setValue('+43 681 11111111111');
    //$phone_number->setCanonicalForm('+4368111111111111');
    $person->setPhoneNumbers($phone_number);
    
    $name = new Name();
    $name->setGivenName('John');
    $name->setFamilyName('Doe');
    $person->setNames($name);
    
    $p = $service->people->createContact($person, ['personFields' => $all_fields]);
    
    var_dump($p);