[英]Create Contact with Google People API + PHP (Server to Server)
我正在嘗試使用下面的代碼通過 google people api + PHP(服務器到服務器)創建一個新聯系人,代碼顯然運行沒有錯誤,但沒有在以下位置創建聯系人: https : //www.google.com /聯系人/
按照此處所述查詢聯系人 ( https://developers.google.com/people/quickstart/php#step_3_set_up_the_sample ) 我看到聯系人已創建。
我使用 Google 應用套件。
我的代碼有什么問題嗎?
<?php
require $_SERVER['DOCUMENT_ROOT'].'/../vendor/autoload.php';
$client = new Google_Client();
$client->setAuthConfig('my configs...');
$client->addScope(Google_Service_PeopleService::CONTACTS);
$service = new Google_Service_PeopleService($client);
$person = new Google_Service_PeopleService_Person();
$email = new Google_Service_PeopleService_EmailAddress();
$email->setValue('test@test.com');
$person->setEmailAddresses($email);
$name = new Google_Service_PeopleService_Name();
$name->setDisplayName('User de Test');
$person->setNames($name);
$exe = $service->people->createContact($person);
print_r($exe);
我想你應該試試這個 php 庫,它為我做了同樣的任務https://github.com/rapidwebltd/php-google-contacts-v3-api
安裝:-
- 下載 repo git clone https://github.com/rapidwebltd/php-google-contacts-v3-api
- 然后運行composer require rapidwebltd/php-google-contacts-v3-api
-在此處在 google api 上創建 api 憑據https://console.developers.google.com/
- 然后將.config_blank.json
重命名為.config.json
並將clientID
、 clientSecret
更新為您從 api 控制台獲得的憑據
-Update的redirectUri
在.config.json文件的完整路徑redirect-handler.php
文件
- 然后繼續在瀏覽器中打開authorise-application.php
並在新選項卡中打開提供的鏈接以授權您的應用程序然后此頁面會將您重定向到文件redirect-handler.php
並要求您添加提供的哈希到屬性refreshToken
的.config.json
文件
- 完成后,您可以嘗試使用此代碼在您的帳戶中創建新聯系人
require_once __DIR__ . '/vendor/autoload.php';
$contact->name = 'Test';
$contact->phoneNumber = '07812363789';
$contact->email = 'test@example.com';
$contact->content = 'Note for example';
$contactAfterUpdate =rapidweb\googlecontacts\factories\ContactFactory::submitUpdates($contact);
var_dump($contactAfterUpdate);
更新:
正如DivineOmega的評論,有一個新版本的 API在這里你可以試試。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.