как добавить контакт в группу в гугл контактах api v3 с PHP - PullRequest
0 голосов
/ 24 января 2020

Я пытаюсь добавить контакт в группу. Но он отвечает HTTP-кодом: 400 (неверный запрос). Что не так? Вот код с ошибкой 400 неверных запросов:

/** config: */
$access_token = "YOUR_ACCESS_TOKEN"; //example: ya29.ImC7B2RO5vkjhsdfdOU0tcn3jI1uP7N5wkxEEEEJF01ZbD4d1LLLLLf5gC06Lh5LJ49FnqHglDjxh5SrdX3jFhOtDQoakSi4XWm5XYHCMhBSiYbFbKqxSvP6_6_rB5_UJLg
$etag = '"YOUR_ETAG."' //example: "SHc8fTVSLLt7I2AAXBFbGEsRTgE."
$contactGroup = "YOUR_CONTACTGROUP"; //example: 1b1bccc40d9e0fff
$contactID = "YOUR_CONTACTS_ID"; //example: 5085d7c29e37dd51

$contactXML = "<entry gd:etag='". $etag ."'>
                <id>http://www.google.com/m8/feeds/contacts/default/base/". $contactID ."</id>
                <gContact:groupMembershipInfo deleted='false'
                href='http://www.google.com/m8/feeds/groups/default/base/". $contactGroup ."'/>
            </entry>";
$headers = array(
'Host: www.google.com'
,'Gdata-version: 3.0'
,'Content-length: '.strlen($contactXML)
,'If-Match: '. $etag
,'Content-type: application/atom+xml'
,'Authorization: OAuth '.$access_token
,'X-HTTP-Method-Override: PUT'
);

$contactQuery = 'https://www.google.com/m8/feeds/contacts/default/full/'. $contactID;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $contactQuery );
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
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_setopt($ch, CURLOPT_HEADER, 1);
$resultat = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo("Resultat=".print_r($resultat,1) ."<br>");
echo("HTTP-CODE: ". $httpcode);

1 Ответ

0 голосов
/ 04 февраля 2020

Здесь вы найдете решение: https://vike.io/de/204411/

$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" xmlns:gContact="http://schemas.google.com/contact/2008">
    <atom:category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/contact/2008#contact"/>



    <gd:name>
        <gd:givenName>'.utf8_encode(str_replace("&",term_user($benutzerid, " und "), $row4['vorname'])).'</gd:givenName>
        <gd:fullName>'.utf8_encode(str_replace("&",term_user($benutzerid, " und "),getNameString($row4['id'], true))).'</gd:fullName>
        <gd:familyName>'.utf8_encode(str_replace("&", term_user($benutzerid, " und "), $row4['name'])).'</gd:familyName>
    </gd:name>

    <atom:content type="text">'.utf8_encode($row4['kommentar']).' (von aBusiness CRM)</atom:content>

    <gd:email rel="http://schemas.google.com/g/2005#home" primary="true" address="'.utf8_encode($row4['email']).'"/>
    <gd:email rel="http://schemas.google.com/g/2005#home" address="'.utf8_encode($row4['email2']).'"/>
    <gd:email rel="http://schemas.google.com/g/2005#home" address="'.utf8_encode($row4['email3']).'"/>

    <gd:phoneNumber rel="http://schemas.google.com/g/2005#home" primary="true">'.utf8_encode($row4['telefon']).'</gd:phoneNumber>
    <gd:phoneNumber rel="http://schemas.google.com/g/2005#home">'.utf8_encode($row4['telefon2']).'</gd:phoneNumber>
    <gd:phoneNumber rel="http://schemas.google.com/g/2005#home">'.utf8_encode($row4['telefon3']).'</gd:phoneNumber>
    <gd:phoneNumber rel="http://schemas.google.com/g/2005#home">'.utf8_encode($row4['telefon4']).'</gd:phoneNumber>
    <gd:phoneNumber rel="http://schemas.google.com/g/2005#home">'.utf8_encode($row4['telefon5']).'</gd:phoneNumber>

    <gd:structuredPostalAddress
    rel="http://schemas.google.com/g/2005#work"
    primary="true">
        <gd:city>'.utf8_encode($row4['ort']).'</gd:city>
        <gd:street>'.utf8_encode($row4['adresse']).'</gd:street>
        <gd:postcode>'.utf8_encode($row4['plz']).'</gd:postcode>
        <gd:country>'.utf8_encode($row4['land']).'</gd:country>
    </gd:structuredPostalAddress>

    <gContact:groupMembershipInfo deleted="false" href="http://www.google.com/m8/feeds/groups/'.$row['email'].'/base/'. $groupId .'"/>
    <gContact:groupMembershipInfo deleted="false" href="http://www.google.com/m8/feeds/groups/'.$row['email'].'/base/6"/>
</atom:entry>';

$headers = array(
'Host: www.google.com'
,'Gdata-version: 3.0'
,'Content-length: '.strlen($contactXML)
,'Content-type: application/atom+xml'
,'Authorization: OAuth '.$access_token
);

$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_setopt($ch, CURLOPT_HEADER, 1);
$resultat = curl_exec($ch);

По умолчанию работать не будет. Вы можете не использовать это:

<gContact:groupMembershipInfo deleted="false" href="http://www.google.com/m8/feeds/groups/default/base/'. $groupId .'"/>

Вы должны использовать E-Mail от учетной записи oAuth. Вы можете получить электронное письмо следующим образом и сохранить его в базе данных:

/** Die Account-ID holen */
$contactQuery = "https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token=". urlencode($access_token);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $contactQuery );
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
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);
$result = curl_exec($ch);
$result = json_decode($result);
$accountid = $result->id;
if(!$accountid) die("Error");
$sql = "update abuoauth set
            accountid = ". dbsecurestring($accountid) ."
            ,`email` = ". dbsecurestring($result->email) ."
            where id = ". dbsecurenumber($abuoauthid) ."
        ";
mysql_query($sql);

Чтобы создать группу контактов:

/** Kontaktgruppe erstellen */
$contactXML = '<entry xmlns="http://www.w3.org/2005/Atom"
                       xmlns:gd="http://schemas.google.com/g/2005">
                    <category scheme="http://schemas.google.com/g/2005#kind"
                              term="http://schemas.google.com/g/2008#group"/>
                    <title>aBusiness</title>
                    <gd:extendedProperty name="more info">
                        <description>aBusiness Kontakte</description>
                    </gd:extendedProperty>
                </entry>';

$headers = array(
'Host: www.google.com'
,'Gdata-version: 3.0'
,'Content-length: '.strlen($contactXML)
,'Content-type: application/atom+xml'
,'Authorization: OAuth '.$access_token
);

$contactQuery = 'https://www.google.com/m8/feeds/groups/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_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $contactXML);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
$resultat = curl_exec($ch);

Чтобы получить группу контактов:

/** Kontaktgruppe "aBusiness schnappen" */
$groupId = "";
$headers = array(
'Host: www.google.com'
,'Gdata-version: 3.0'
,'Content-type: application/atom+xml'
,'Authorization: OAuth '.$access_token
);

$contactQuery = 'https://www.google.com/m8/feeds/groups/default/full';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $contactQuery );
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
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);
$resultat = curl_exec($ch);

$posTitle = strpos($resultat, "<description>aBusiness Kontakte</description>");
if($posTitle !== false) {
    /** Kontatkgruppe parsern */
    $iPosLast = 0;
    $iPos = strpos($resultat, "/base/");
    while($iPos !== false) {
        $iPosLast = $iPos;
        $iPos = strpos($resultat, "/base/", $iPos+1);
        if($iPos > $posTitle) {
            /** Eine Position zu weit. */
            $iPos = $iPosLast;
            break;
        }
    }
    /** Anfang zuschneiden */
    $strCut = substr($resultat, $iPosLast+6);
    $groupId = getPartOfString($strCut, "", "</id>");
}

Чтобы удалить контакт:

$headers = array(
    'Host: www.google.com'
    ,'Gdata-version: 3.0'
    ,'Content-type: application/atom+xml'
    ,'Authorization: OAuth '.$access_token
    ,'If-Match: '. $row3['etag']
    ,'X-HTTP-Method-Override: DELETE'
);
if($row3['googleid']) {
    $contactQuery = 'https://www.google.com/m8/feeds/contacts/default/full/'. $row3['googleid'];
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $contactQuery );
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
    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_setopt($ch, CURLOPT_HEADER, 1);
    $resultat = curl_exec($ch);

    if(strpos($resultat, "200 OK") !== false) {
        mysql_query("delete from googlecontacts where id = ". dbsecurenumber($row3['id']));
        alert("Google Kontakt mit Kunde-ID ". $row3['kundeid'] ." gelöscht.");
    }
}
...