繁体   English   中英

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." } 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(随客户端机密一起发布)。 不是用户的电子邮件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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