2017-08-25 88 views
2

我試圖使用Google People API添加和更新我的Google聯繫人。我使用Google文檔(https://developers.google.com/people/v1/getting-started)中給出的示例代碼幾乎逐字地設置了API。我得到一個錯誤以下行的代碼,它又來了逐字從文檔:錯誤「(獲取)未知參數:'personFields'」在Google People API文檔中的示例中使用

$profile = $people_service->people->get('people/me', array('personFields' => 'names,emailAddresses')); 

錯誤如下:

Fatal error: Uncaught exception 'Google_Exception' with message '(get) unknown parameter: 'personFields'' in /home/danbak15/bakerlegalservicesmo.com/office/google-api-php-client-2.2.0/src/Google/Service/Resource.php:147 Stack trace: #0 /home/danbak15/bakerlegalservicesmo.com/office/google-api-php-client-2.2.0/vendor/google/apiclient-services/src/Google/Service/People/Resource/People.php(52): Google_Service_Resource->call('get', Array, 'Google_Service_...') #1 /home/danbak15/bakerlegalservicesmo.com/office/BLScontacts.php(36): Google_Service_People_Resource_People->get('people/me', Array) #2 {main} thrown in /home/danbak15/bakerlegalservicesmo.com/office/google-api-php-client-2.2.0/src/Google/Service/Resource.php on line 147 

我所看到的是回答了類似的問題(Can't access my profile data when accessing google-people API )建議使用Google_Service_PeopleService類而不是文檔中所要求的Google_Service_People類。然而,當我嘗試這樣做,我得到另一個錯誤:

Fatal error: Uncaught exception 'Google_Service_Exception' with message '{ "error": { "code": 401, "message": "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.", "errors": [ { "message": "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.", "domain": "global", "reason": "unauthorized" } ], "status": "UNAUTHENTICATED" } } ' in /home/danbak15/bakerlegalservicesmo.com/office/google-api-php-client-2.2.0/src/Google/Http/REST.php:118 Stack trace: #0 /home/danbak15/bakerlegalservicesmo.com/office/google-api-php-client-2.2.0/src/Google/Http/REST.php(94): Google_Http_REST::decodeHttpResponse(Object(GuzzleHttp\Psr7\Response), Object(GuzzleHttp\Psr7\Request), 'G in /home/danbak15/bakerlegalservicesmo.com/office/google-api-php-client-2.2.0/src/Google/Http/REST.php on line 118 

在這一點上,我在全盤損失。我想使用Google通訊錄來跟蹤我的聯繫人,並希望能夠通過PHP使用Google通訊錄,以便將它們添加到我的數據庫中。任何人都可以對此有所瞭解嗎?預先感謝您的任何幫助!

更新: 我試着將頁面移動到我的計算機上的本地服務器(忘記Google將頁面重定向到我的網站),以查看是否可以從不同的服務器獲得不同的結果。不知何故,當我嘗試從我的網站上訪問腳本時,它有效地工作了大約15-30分鐘左右。然後,我得到了和以前一樣的錯誤。如果我從本地主機服務器運行該頁面,我將陷入無盡的授權重定向循環(預計Google會將我重定向到在線頁面),但這個在線頁面一段時間就會工作。

在這一點上,我無法開始猜測問題出在哪裏。這對其他人有意義嗎?

+0

您確定您擁有最新版本的客戶端庫嗎? – paolo

+0

我有同樣的問題,但使用Google_Service_PeopleService修復了它;我建議你專注於解決第二個錯誤。 – Isaiah

+0

我使用的是Google API PHP Client 2.2.0版本,我認爲它是正確的版本(儘管我總是可能會誤)。在使用Google_Service_PeopleService時,我對於身份驗證憑證錯誤感到有些不知所措。我有OAuth 2憑據,在使用Google_Service_People類時工作得很好。只有當我使用Google_Service_PeopleService類時纔會引發錯誤。我應該查看或更改我的憑據中是否有某些內容? –

回答

0

我想我知道最新情況。 我的英語不是很好,所以我希望你能明白我的意思。

在我的情況,我使用Google_Service_People類(src \ Google \ Service \ People.php)並得到與你一樣的錯誤。所以我來看看有關People.php,看看這個:

$this->people = new Google_Service_People_Resource_People(
    $this, 
    $this->serviceName, 
    'people', 
    array(
     'methods' => array(
     'get' => array(
      'path' => 'v1/{+resourceName}', 
      'httpMethod' => 'GET', 
      'parameters' => array(
      'resourceName' => array(
       'location' => 'path', 
       'type' => 'string', 
       'required' => true, 
      ), 
      'requestMask.includeField' => array( // <= This is the problem. 
       'location' => 'query', 
       'type' => 'string', 
      ), 
     ), 
     ), 

我檢查的官方文件,它說:「requestMask.includeField已經過時了。」但它仍然在api代碼以某種方式。

所以我把它改成這樣:

$this->people = new Google_Service_People_Resource_People(
    $this, 
    $this->serviceName, 
    'people', 
    array(
     'methods' => array(
     'get' => array(
      'path' => 'v1/{+resourceName}', 
      'httpMethod' => 'GET', 
      'parameters' => array(
      'resourceName' => array(
       'location' => 'path', 
       'type' => 'string', 
       'required' => true, 
      ), 
      'personFields' => array( // <- Change to 'personFields' 
       'location' => 'query', 
       'type' => 'string', 
      ), 
     ), 

之後每一件事情順利。

如果您使用來自PHP的Google API客戶端庫。此客戶端庫位於測試版中。所以他們的api中有一些東西看起來並不像api文檔中的確切內容。

這就是我的情況。英語不是我的語言,所以我希望你能理解我想描述的內容。

0

這個工作對我罰款:

$scopes[] = 'https://www.googleapis.com/auth/contacts.readonly'; 
$client->setScopes($scopes); 
$service = new Google_Service_People($client); 
$optParams = ['requestMask.includeField' => 'person.names']; 
$connections = $service->people_connections->listPeopleConnections('people/me', $optParams)->getConnections(); 
foreach ($connections as $connection) { 
    if (!empty($connection->getNames()[0])) { 
    print $connection->getNames()[0]->getDisplayName() . "\n" . '<br>'; 
    } 
} 
0

約翰·羅德里格斯解決的問題。謝謝!我的圖書館有點不同,但問題是一樣的。在我的情況下,我使用Google_Service_PeopleService類而不是Google_Service_People類。我改變的文件路徑是:google-api-php-client-2.2.0/vendor/google/apiclient-services/src/Google/Service/PeopleService.php。在這種情況下,$ this-> people是一個包含一組方法的Google_Service_PeopleService_Resource_People類。我評論了折舊的代碼。這裏是相關的代碼:

$this->people = new Google_Service_PeopleService_Resource_People(
    $this, 
    $this->serviceName, 
    'people', 
    array(
     'methods' => array(
     'createContact' => array(
       ... 
      ), 
     ), 
     ),'deleteContact' => array(
      ... 
      ), 
     ), 
     ),'get' => array(
      'path' => 'v1/{+resourceName}', 
      'httpMethod' => 'GET', 
      'parameters' => array(
      'resourceName' => array(
       'location' => 'path', 
       'type' => 'string', 
       'required' => true, 
      ), 
      'personFields' => array(
       'location' => 'query', 
       'type' => 'string', 
      ), 
/*   'requestMask.includeField' => array(
       'location' => 'query', 
       'type' => 'string', 
      ), */ 
     ), 
     ),'getBatchGet' => array(
      'path' => 'v1/people:batchGet', 
      'httpMethod' => 'GET', 
      'parameters' => array(
      'personFields' => array(
       'location' => 'query', 
       'type' => 'string', 
      ), 
/*   'requestMask.includeField' => array(
       'location' => 'query', 
       'type' => 'string', 
      ), */ 
      'resourceNames' => array(
       'location' => 'query', 
       'type' => 'string', 
       'repeated' => true, 
      ), 
     ), 
     ),'updateContact' => array(
      ... 
      ), 
     ), 
     ), 

到目前爲止這已經工作,因爲它應該,但我還沒有徹底測試它呢。感謝您的幫助!

相關問題