简体   繁体   English

同步令牌异常google-api-client-service php people Service

[英]Sync Token Exception google-api-client-service php people Service

I have a problem with the Google API. 我的Google API有问题。 I need to get all the contacts of a user in my application, however I don't want to load all the data each time, because I store the data in my database. 我需要在应用程序中获取用户的所有联系人,但是我不想每次都加载所有数据,因为我将数据存储在数据库中。

It's why I want to use the Sync Token functionality which let's us get only data who has been changed since our last query. 这就是为什么我要使用“同步令牌”功能,该功能使我们仅获取自上次查询以来已更改的数据。

For this i Use the Google APIs PHP client library . 为此,我使用Google APIs PHP客户端库

I wrote my code and it throws an exception (Google_Service_Exception) which explains that my Sync Token is not valid anymore, so I decided to use a try/catch system to reset syncToken if it is not valid. 我编写了代码,并引发了异常(Google_Service_Exception),这说明我的同步令牌不再有效,因此,我决定使用try / catch系统重置syncToken(如果无效)。

My problem is each time i made the request I go in the catch (the google_service_exception) is raised even if my token is valid. 我的问题是,即使我的令牌有效,每次提出请求时,我都会抓住问题(google_service_exception)。

I store the token in my database so I tested it in the API explorer here: https://developers.google.com/people/api/rest/v1/people.connections/list and it work fine, so I don't understand why my application doesn't work properly. 我将令牌存储在数据库中,因此我在这里的API资源管理器中对其进行了测试: https : //developers.google.com/people/api/rest/v1/people.connections/list ,它可以正常工作,所以我不能了解为什么我的应用程序无法正常运行。

Here's my code: 这是我的代码:

    $personFields = 'metadata,names,addresses,biographies,birthdays,emailAddresses,genders,memberships,organizations,phoneNumbers';
    $user = $this->get('security.token_storage')->getToken()->getUser();
    $syncToken = $user->getContactSyncToken();
    $nextPagetoken = '';
    $contactData = array();

    $people_service = new \Google_Service_PeopleService($client);

do {

        try {
            $data = $people_service->people_connections->listPeopleConnections(
                'people/me',
                array(
                    'personFields' => $personFields,
                    'requestSyncToken' => true,
                    'syncToken' => $syncToken,
                    'pageToken' => $nextPagetoken,

                )
            );
        }catch (\Google_Service_Exception $e){
            $data = $people_service->people_connections->listPeopleConnections(
                'people/me',
                array(
                    'personFields' => $personFields,
                    'requestSyncToken' => true,
                    'syncToken' => '',
                    'pageToken' => $nextPagetoken,

                )
            );
        }finally{
            $syncToken = $data->getNextSyncToken();
            $nextPagetoken = $data->getNextPageToken();
            if (count($data->getConnections()) != 0) {
                $contactData[] = $data->getConnections();
            }
        }




    }while ($nextPagetoken !== null);

$user->setContactSyncToken($syncToken);
$this->getDoctrine()->getManager()->flush();
return $contactData;

Thank you for your help and sorry for my english i'm not a native and not very good at it. 感谢您的帮助,对不起我的英语,我不是母语人士,也不擅长英语。

Sorry i found the problem, in my User class i had: 抱歉,我在User类中遇到了问题:

public function getContactSyncToken()
{

    if($token = $this->contactSyncToken  !== null){
        return $token;
    }else{
        return '';
    }
    }

And it always return ' ', now it works with: 它总是返回'',现在可以使用:

public function getContactSyncToken()
{
    $token = $this->contactSyncToken;
    if($token  !== null){
        return $token;
    }else{
        return '';
    }


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM