PHP를 사용하여 Google 연락처로 새 연락처 항목을 푸시하고 싶습니다.PHP를 사용하여 Google 사람들 api에 새 연락처 추가
아래는 내 index.php 코드입니다.
require_once 'vendor/autoload.php';
session_start();
$client = new Google_Client();
$client->setClientId('519334414648-
f2jiml3iuuj87mjn1ofc9kbqmlsn7iqb.apps.googleusercontent.com');
$client->setClientSecret('0x9LpW7Qr37x89YFRtmgq6oH');
$client->setRedirectUri('http://example.com/demo/contacts/redirect.php');
$client->addScope('profile');
$client->addScope('https://www.googleapis.com/auth/contacts.readonly');
if (isset($_GET['oauth'])) {
// Start auth flow by redirecting to Google's auth server
$auth_url = $client->createAuthUrl();
header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
}
else if (isset($_GET['code'])) {
// Receive auth code from Google, exchange it for an access token, and
// redirect to your base URL
$client->authenticate($_GET['code']);
$_SESSION['access_token'] = $client->getAccessToken();
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/demo/contacts';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
else if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
// You have an access token; use it to call the People API
$client->setAccessToken($_SESSION['access_token']);
$client->setAccessType('online');
// TODO: Use service object to request People data
$client->addScope(Google_Service_PeopleService::CONTACTS);
$service = new Google_Service_PeopleService($client);
$person = new Google_Service_PeopleService_Person();
$person->setPhoneNumbers('1234512345');
$name = new Google_Service_PeopleService_Name();
$name->setDisplayName('test user');
$person->setNames($name);
$exe = $service->people->createContact($person);
}
else {
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/demo/contacts?oauth';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
아래 코드를 실행하면이 오류가 발생합니다. 캐치되지 않는 예외 메시지 'Google_Service_Exception' '{ "오류": { "코드": 401, "메시지". "요청이 유효 인증 자격 증명을 가지고 예상 OAuth는이 액세스 토큰, 로그인 쿠키 또는 기타 유효한 인증 자격 증명 수 있습니다. 내 코드가 맞다면 어떤 일이 말해?하지 연락처 또는 사람들 API에 연락처를 밀어의 coorect 방법을 제안하십시오.