【问题标题】:Sync Token Exception google-api-client-service php people Service同步令牌异常 google-api-client-service php people 服务
【发布时间】:2017-11-22 11:44:53
【问题描述】:

我的 Google API 有问题。我需要在我的应用程序中获取用户的所有联系人,但是我不想每次都加载所有数据,因为我将数据存储在我的数据库中。

这就是我想使用同步令牌功能的原因,它让我们只获取自上次查询以来已更改的数据。

为此,我使用Google APIs PHP client library

我编写了我的代码,它引发了一个异常 (Google_Service_Exception),它解释了我的 Sync Token 不再有效,所以我决定使用 try/catch 系统在它无效时重置 syncToken。

我的问题是,每次我提出请求时,即使我的令牌有效,我也会引发捕获(google_service_exception)。

我将令牌存储在我的数据库中,因此我在 API 资源管理器中对其进行了测试:https://developers.google.com/people/api/rest/v1/people.connections/list 并且它工作正常,所以我不明白为什么我的应用程序不能正常工作。

这是我的代码:

    $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;

感谢您的帮助,抱歉我的英语不是本地人,也不是很擅长。

【问题讨论】:

    标签: php google-api google-api-php-client google-people-api


    【解决方案1】:

    抱歉,我在我的用户类中发现了问题:

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

    它总是返回 ' ',现在它适用于:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-13
      • 2021-10-14
      • 2015-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-19
      • 1970-01-01
      相关资源
      最近更新 更多