PHP 如何将联系人添加到 google
PHP how to add contact to google
我试图通过 PHP 将联系人添加到 google
我向 google 开发人员添加了一个新项目,并为此电子邮件激活了联系人 API
我使用了下面的代码,但响应如下:json_response: { "error" : "invalid_request", "error_description" : "Could not determine client ID from request." }
你能帮我解决这个问题吗
parameters:
code: id from database for example: 101
gsm: cell phone
代码:
<?php
$code=doubleval($_REQUEST['code']);
echo 'code: '.$code.'<br />'.PHP_EOL;
$access = get_oauth2_token($code);
echo 'access: '.$access.'<br />'.PHP_EOL;
$client_id= 'hazem*********@gmail.com';
$client_secret= '***********';
$redirect_uri= 'http://************/synchronize.php';
$gsm=doubleval($_REQUEST['gsm']);
echo 'gsm: '.$gsm.'<br />'.PHP_EOL;
if($gsm){
addContact($access, $gsm);
}
//returns session token for calls to API using oauth 2.0
function get_oauth2_token($code) {
global $client_id;
global $client_secret;
global $redirect_uri;
$oauth2token_url = "https://accounts.google.com/o/oauth2/token";
$clienttoken_post = array(
"code" => $code,
"client_id" => $client_id,
"client_secret" => $client_secret,
"redirect_uri" => $redirect_uri,
"grant_type" => "authorization_code"
);
$curl = curl_init($oauth2token_url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $clienttoken_post);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$json_response = curl_exec($curl);
echo 'json_response: '.$json_response.'<br />'.PHP_EOL;
curl_close($curl);
$authObj = json_decode($json_response);
if (isset($authObj->refresh_token)){
//refresh token only granted on first authorization for offline access
//save to db for future use (db saving not included in example)
global $refreshToken;
$refreshToken = $authObj->refresh_token;
}
$accessToken = $authObj->access_token;
return $accessToken;
}
function addContact($access, $gsm){
$name =$gsm;
$fullName=$gsm;
$last= $gsm;
/*
<gd:givenName>'.$name.'</gd:givenName>
<gd:familyName>'.$last.'</gd:familyName>
//*/
$contactXML = '<?xml version="1.0" encoding="utf-8"?>
<atom:entry xmlns:atom="http://www.w3.org/2005/Atom" xmlns:gd="http://schemas.google.com/g/2005">
<atom:category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/contact/2008#contact"/>
<gd:name>
<gd:fullName>'.$fullName.'</gd:fullName>
</gd:name>
<gd:phoneNumber rel="http://schemas.google.com/g/2005#home" primary="true">'.$gsm.'</gd:phoneNumber>
</atom:entry>';
echo $contactXML;
$headers = array(
'Host: www.google.com',
'Gdata-version: 3.0',
'Content-length: '.strlen($contactXML),
'Content-type: application/atom+xml',
'Authorization: OAuth '.$access
);
$contactQuery = 'https://www.google.com/m8/feeds/contacts/default/full/';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $contactQuery );
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $contactXML);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_exec($ch);
}
?>
客户端 ID 是您在开发控制台中注册的客户端应用程序的 ID(与您的客户端密钥一起发布)。不是用户的电子邮件。
我试图通过 PHP 将联系人添加到 google
我向 google 开发人员添加了一个新项目,并为此电子邮件激活了联系人 API
我使用了下面的代码,但响应如下:json_response: { "error" : "invalid_request", "error_description" : "Could not determine client ID from request." }
你能帮我解决这个问题吗
parameters:
code: id from database for example: 101
gsm: cell phone
代码:
<?php
$code=doubleval($_REQUEST['code']);
echo 'code: '.$code.'<br />'.PHP_EOL;
$access = get_oauth2_token($code);
echo 'access: '.$access.'<br />'.PHP_EOL;
$client_id= 'hazem*********@gmail.com';
$client_secret= '***********';
$redirect_uri= 'http://************/synchronize.php';
$gsm=doubleval($_REQUEST['gsm']);
echo 'gsm: '.$gsm.'<br />'.PHP_EOL;
if($gsm){
addContact($access, $gsm);
}
//returns session token for calls to API using oauth 2.0
function get_oauth2_token($code) {
global $client_id;
global $client_secret;
global $redirect_uri;
$oauth2token_url = "https://accounts.google.com/o/oauth2/token";
$clienttoken_post = array(
"code" => $code,
"client_id" => $client_id,
"client_secret" => $client_secret,
"redirect_uri" => $redirect_uri,
"grant_type" => "authorization_code"
);
$curl = curl_init($oauth2token_url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $clienttoken_post);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$json_response = curl_exec($curl);
echo 'json_response: '.$json_response.'<br />'.PHP_EOL;
curl_close($curl);
$authObj = json_decode($json_response);
if (isset($authObj->refresh_token)){
//refresh token only granted on first authorization for offline access
//save to db for future use (db saving not included in example)
global $refreshToken;
$refreshToken = $authObj->refresh_token;
}
$accessToken = $authObj->access_token;
return $accessToken;
}
function addContact($access, $gsm){
$name =$gsm;
$fullName=$gsm;
$last= $gsm;
/*
<gd:givenName>'.$name.'</gd:givenName>
<gd:familyName>'.$last.'</gd:familyName>
//*/
$contactXML = '<?xml version="1.0" encoding="utf-8"?>
<atom:entry xmlns:atom="http://www.w3.org/2005/Atom" xmlns:gd="http://schemas.google.com/g/2005">
<atom:category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/contact/2008#contact"/>
<gd:name>
<gd:fullName>'.$fullName.'</gd:fullName>
</gd:name>
<gd:phoneNumber rel="http://schemas.google.com/g/2005#home" primary="true">'.$gsm.'</gd:phoneNumber>
</atom:entry>';
echo $contactXML;
$headers = array(
'Host: www.google.com',
'Gdata-version: 3.0',
'Content-length: '.strlen($contactXML),
'Content-type: application/atom+xml',
'Authorization: OAuth '.$access
);
$contactQuery = 'https://www.google.com/m8/feeds/contacts/default/full/';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $contactQuery );
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $contactXML);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_exec($ch);
}
?>
客户端 ID 是您在开发控制台中注册的客户端应用程序的 ID(与您的客户端密钥一起发布)。不是用户的电子邮件。