我已经创建了开发者的帐户,获得了我的客户 ID 和密钥。我是 C# 新手,我可以使用 C# 检索联系人。任何人都可以发布添加新联系人条目的代码吗?谷歌为此目的提供了适当的文档,但我在使用 C# 实现它时遇到了问题。请帮忙!
谢谢
我可以使用 PHP 完成(添加联系人)
这是代码
<?php
$group_id = "6"; // Used as the default 'My Contacts' group.
require_once 'google-api-php-client/src/Google_Client.php';
session_start();
$client = new Google_Client();
$client->setApplicationName("Google Apps PHP Starter Application");
$client->setScopes(array(
'https://apps-apis.google.com/a/feeds/groups/',
'https://apps-apis.google.com/a/feeds/alias/',
'https://apps-apis.google.com/a/feeds/user/',
'https://www.google.com/m8/feeds/',
'https://www.google.com/m8/feeds/user/',
));
$client->setClientId('#################################');
$client->setClientSecret('***************************');
$client->setRedirectUri('http://localhost/try/index.php');
$client->setDeveloperKey('DEVELOPER_KEY');
if (isset($_REQUEST['logout'])) {
unset($_SESSION['access_token']);
}
if (isset($_GET['code'])) {
$client->authenticate();
$_SESSION['access_token'] = $client->getAccessToken();
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}
if (isset($_SESSION['access_token'])) {
$client->setAccessToken($_SESSION['access_token']);
$token = json_decode($_SESSION['access_token']);
$auth_pass = $token->access_token;
//Get Email of User ------------------------------------
// You are now logged in
// We need the users email address for later use. We can get that here.
$req = new Google_HttpRequest("https://www.google.com/m8/feeds/contacts/default/full");
$req->setRequestHeaders(array('GData-Version'=> '3.0','content-type'=>'application/atom+xml; charset=UTF-8; type=feed'));
$val = $client->getIo()->authenticatedRequest($req);
// The contacts api only returns XML responses.
$response =$val->getResponseBody();
$xml = simplexml_load_string($response); // Convert to an ARRAY
$user_email = urlencode($xml->id); // email address
unset($xml); // clean-up
//-------------------------------------
// How to save an entry to your My Contacts List
// This is an example contact XML that Google is looking for.
$contact="
<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>abc_Name</gd:givenName>
<gd:familyName>abc_familyname</gd:familyName>
<gd:fullName>abc_fullname</gd:fullName>
</gd:name>
<atom:content type='text'>
abc_Description</atom:content>
<link rel='http://schemas.google.com/contacts/2008/rel#photo' type='image/*'
href='https://www.google.com/m8/feeds/photos/media/userEmail/contactId'
gd:etag='photoEtag'/>
<gd:email rel='http://schemas.google.com/g/2005#work'
primary='true'
address='abc@gmail.com' displayName='E. Bennet'/>
<gd:phoneNumber rel='http://schemas.google.com/g/2005#work'
primary='true'>
9999999999999
</gd:phoneNumber>
<gd:structuredPostalAddress
rel='http://schemas.google.com/g/2005#work'
primary='true'>
<gd:city>abc_city</gd:city>
<gd:street>abc_street</gd:street>
<gd:region>abc_state</gd:region>
<gd:postcode>abc_pin</gd:postcode>
<gd:country>abc_nation</gd:country>
<gd:formattedAddress>
abc_formatted address
</gd:formattedAddress>
</gd:structuredPostalAddress>
<gContact:groupMembershipInfo deleted='false' href='http://www.google.com/m8/feeds/groups/".$user_email."/base/6'/>
</atom:entry>";
$len = strlen($contact);
$add = new Google_HttpRequest("https://www.google.com/m8/feeds/contacts/".$user_email."/full/");
$add->setRequestMethod("POST");
$add->setPostBody($contact);
$add->setRequestHeaders(array('content-length' => $len, 'GData-Version'=> '3.0','content-type'=>'application/atom+xml; charset=UTF-8; type=feed'));
$submit = $client->getIo()->authenticatedRequest($add);
$sub_response = $submit->getResponseBody();
$parsed = simplexml_load_string($sub_response);
$client_id = explode("base/",$parsed->id);
// Contact Groups -------------------------------------------------
// This section will collect all the groups for this user for contact sorting.
// For now, I have set the default group to "My Contacts" of that user.
$group="http%3A%2F%2Fwww.google.com%2Fm8%2Ffeeds%2Fgroups%2F".$user_email."%2Fbase%2F6";
//Get Contacts by Group -------------------------------------------------------------------
// Now we request the users contacts based on group. For now, we will retreive 'My Contacts'
$req = new Google_HttpRequest("https://www.google.com/m8/feeds/contacts/".$user_email."/full?group=".$group);
$req->setRequestHeaders(array('GData-Version'=> '3.0','content-type'=>'application/atom+xml; charset=UTF-8; type=feed'));
$val = $client->getIo()->authenticatedRequest($req);
// The contacts api only returns XML responses.
$response =$val->getResponseBody();
$xml = simplexml_load_string($response); // Convert the response to an ARRAY
//print_r($xml);
echo "Group: ".$xml->title."<br>";
echo "Email: ".$xml->id."<br>";
echo "<hr><br>";
for($i = 0; $i <= sizeof($xml->entry); $i++)
{
echo $xml->entry[$i]->title."<br>";
}
// The access token may have been updated lazily.
$_SESSION['access_token'] = $client->getAccessToken();
} else {
$authUrl = $client->createAuthUrl();
}
if(isset($authUrl)) {
print "<a class='login' href='$authUrl'>Connect Me!</a>";
} else {
print "<a class='logout' href='?logout'>Logout</a>";
}
我可以从这里获取用于检索联系人的有效 C# 代码