简体   繁体   中英

PHP how to add contact to google

I tried to add contact to google through PHP I add a new project to google developer, and activate contact API for this email and I used the below code, but the response as the following: json_response: { "error" : "invalid_request", "error_description" : "Could not determine client ID from request." } json_response: { "error" : "invalid_request", "error_description" : "Could not determine client ID from request." }

could you help me to fix this issue

parameters:
code: id from database for example: 101
gsm: cell phone 

Code:

<?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);


}



?>

Client ID is the ID of your client application that you registered in the dev console (issued with your client secret). Not the email of the user.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM