错误 400:readMask 是必需的。请指定一个或多个有效的路径。

huangapple go评论61阅读模式
英文:

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

问题

我试图使用GoogleAPIClient库在我的Google帐户中创建新联系人。

我的函数代码如下:

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

    // 创建一个新的人并设置其名称、电话号码和传记
    $person = new Google_Service_PeopleService_Person();

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

    // 创建一个列表以存储电话号码
    $phoneNumbersList = [];
    foreach ($telefonos as $telefono) {
        $phoneNumber = new Google_Service_PeopleService_PhoneNumber();
        $phoneNumber->setValue($telefono);
        // 将电话号码添加到列表中
        $phoneNumbersList[] = $phoneNumber;
    }
    // 将联系人的电话号码设置为刚刚创建的列表
    $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]);

    // 创建联系人
    $createdContact = $service->people->createContact($person, [
        'personFields' => 'names,phoneNumbers,emailAddresses,biographies'
    ]);

    // 获取创建的联系人的ID
    $contactId = $createdContact->getResourceName();

    return $contactId;
}

当我执行此函数时,它给我这个响应:

{
  "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"
  }
}

我不明白为什么会出现这个错误,我已经有一个readMask。我还尝试使用以下其他方式创建联系人:

$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']]);

得到的响应始终相同。

我是否需要其他东西?我的函数有什么问题吗?

英文:

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?

答案1

得分: 0

我在处理过程中也遇到了相同的错误。以下代码对我有效。也许这能帮助你找到正确的方向,或者你可以在此基础上进行构建:

<?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();
// 在这里进行授权范围的设置以解决客户端未经授权使用此方法检索访问令牌的错误,或者客户端未经授权请求的任何范围
// 可用的范围 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);

注意:请确保替换 myconfig.jsonmyemail@example.com 为你自己的配置文件路径和电子邮件地址。

英文:

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:

&lt;?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-&gt;setAuthConfig(&#39;myconfig.json&#39;);
$client-&gt;addScope([
    &#39;https://www.googleapis.com/auth/userinfo.profile&#39;,
    &#39;https://www.googleapis.com/auth/contacts&#39;,
    &#39;https://www.googleapis.com/auth/directory.readonly&#39;
]);
$client-&gt;setSubject(&#39;myemail@example.com&#39;);
$client-&gt;setAccessType(&quot;offline&quot;);
$client-&gt;setApprovalPrompt(&#39;force&#39;);

$service = new Google\Service\PeopleService($client);

$all_fields = &#39;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&#39;;


$person = new Person();

$email = new EmailAddress();
$email-&gt;setValue(&#39;asd@example.com&#39;);
$person-&gt;setEmailAddresses($email);

$phone_number = new PhoneNumber();
$phone_number-&gt;setValue(&#39;+43 681 11111111111&#39;);
//$phone_number-&gt;setCanonicalForm(&#39;+4368111111111111&#39;);
$person-&gt;setPhoneNumbers($phone_number);

$name = new Name();
$name-&gt;setGivenName(&#39;John&#39;);
$name-&gt;setFamilyName(&#39;Doe&#39;);
$person-&gt;setNames($name);

$p = $service-&gt;people-&gt;createContact($person, [&#39;personFields&#39; =&gt; $all_fields]);

var_dump($p);

huangapple
  • 本文由 发表于 2023年6月16日 01:29:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76484159.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定