title: Add a contact using Google Contacts API Version 3.0 & OAuth 2.0 in C# and ASP.NET
body: <p>I have created the developer's account, got my client-id and secret key. I'm new to C#, I'm able to retrieve contacts using using C#. Can anyone post the code for adding a new contact entry? Google has proper documentation for this purpose, but I'm having problem to implement it using C#. Please help!</p>
  
  <p>Thank you</p>
  
  <p>I Could Accomplish using PHP (Adding a Contact)</p>
  
  <p>Here is the code</p>
  
  <pre><code>&lt;?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-&gt;setApplicationName("Google Apps PHP Starter Application");
  $client-&gt;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-&gt;setClientId('#################################');
  $client-&gt;setClientSecret('***************************');
  $client-&gt;setRedirectUri('http://localhost/try/index.php');
  $client-&gt;setDeveloperKey('DEVELOPER_KEY');
  
  if (isset($_REQUEST['logout'])) {
  unset($_SESSION['access_token']);
  }
  
  if (isset($_GET['code'])) {
  $client-&gt;authenticate();
  $_SESSION['access_token'] = $client-&gt;getAccessToken();
  $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
  header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
  }
  
  if (isset($_SESSION['access_token'])) {
  
  $client-&gt;setAccessToken($_SESSION['access_token']);
  $token = json_decode($_SESSION['access_token']);
  
  
  $auth_pass = $token-&gt;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-&gt;setRequestHeaders(array('GData-Version'=&gt; '3.0','content-type'=&gt;'application/atom+xml; charset=UTF-8; type=feed'));
  
  $val = $client-&gt;getIo()-&gt;authenticatedRequest($req);
  
    // The contacts api only returns XML responses.
    $response =$val-&gt;getResponseBody();
  
  $xml = simplexml_load_string($response); // Convert to an ARRAY
  
  $user_email = urlencode($xml-&gt;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="
  &lt;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'&gt;
    &lt;atom:category scheme='http://schemas.google.com/g/2005#kind'
      term='http://schemas.google.com/contact/2008#contact'/&gt;
    &lt;gd:name&gt;
       &lt;gd:givenName&gt;abc_Name&lt;/gd:givenName&gt;
       &lt;gd:familyName&gt;abc_familyname&lt;/gd:familyName&gt;
       &lt;gd:fullName&gt;abc_fullname&lt;/gd:fullName&gt;
    &lt;/gd:name&gt;
    &lt;atom:content type='text'&gt;
    abc_Description&lt;/atom:content&gt;
    &lt;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'/&gt;
    &lt;gd:email rel='http://schemas.google.com/g/2005#work'
      primary='true'
      address='abc@gmail.com' displayName='E. Bennet'/&gt;
    &lt;gd:phoneNumber rel='http://schemas.google.com/g/2005#work'
      primary='true'&gt;
      9999999999999
    &lt;/gd:phoneNumber&gt;
  
    &lt;gd:structuredPostalAddress
        rel='http://schemas.google.com/g/2005#work'
        primary='true'&gt;
      &lt;gd:city&gt;abc_city&lt;/gd:city&gt;
      &lt;gd:street&gt;abc_street&lt;/gd:street&gt;
      &lt;gd:region&gt;abc_state&lt;/gd:region&gt;
      &lt;gd:postcode&gt;abc_pin&lt;/gd:postcode&gt;
      &lt;gd:country&gt;abc_nation&lt;/gd:country&gt;
      &lt;gd:formattedAddress&gt;
              abc_formatted address
      &lt;/gd:formattedAddress&gt;
     &lt;/gd:structuredPostalAddress&gt;
     &lt;gContact:groupMembershipInfo deleted='false' href='http://www.google.com/m8/feeds/groups/".$user_email."/base/6'/&gt;
    &lt;/atom:entry&gt;";
  
  
    $len = strlen($contact);
    $add = new Google_HttpRequest("https://www.google.com/m8/feeds/contacts/".$user_email."/full/");
    $add-&gt;setRequestMethod("POST");
    $add-&gt;setPostBody($contact);
    $add-&gt;setRequestHeaders(array('content-length' =&gt; $len, 'GData-Version'=&gt; '3.0','content-type'=&gt;'application/atom+xml; charset=UTF-8; type=feed'));
  
    $submit = $client-&gt;getIo()-&gt;authenticatedRequest($add);
    $sub_response = $submit-&gt;getResponseBody();
  
  
    $parsed = simplexml_load_string($sub_response);   
    $client_id = explode("base/",$parsed-&gt;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-&gt;setRequestHeaders(array('GData-Version'=&gt; '3.0','content-type'=&gt;'application/atom+xml; charset=UTF-8; type=feed'));
  $val = $client-&gt;getIo()-&gt;authenticatedRequest($req);
  
    // The contacts api only returns XML responses.
    $response =$val-&gt;getResponseBody();
  
  
      $xml = simplexml_load_string($response); // Convert the response to an ARRAY
  
  //print_r($xml);
  
          echo "Group: ".$xml-&gt;title."&lt;br&gt;";
          echo "Email: ".$xml-&gt;id."&lt;br&gt;";
          echo "&lt;hr&gt;&lt;br&gt;";
  
          for($i = 0; $i &lt;= sizeof($xml-&gt;entry); $i++)
  
              {
  
              echo $xml-&gt;entry[$i]-&gt;title."&lt;br&gt;";
  
              }
  
  
  
  // The access token may have been updated lazily.
  $_SESSION['access_token'] = $client-&gt;getAccessToken();
  } else {
  $authUrl = $client-&gt;createAuthUrl();
  }
  
  if(isset($authUrl)) {
  print "&lt;a class='login' href='$authUrl'&gt;Connect Me!&lt;/a&gt;";
  } else {
   print "&lt;a class='logout' href='?logout'&gt;Logout&lt;/a&gt;";
  }
  </code></pre>
  
  <p>I could Get a working C# code for retrieving contacts from <a href="http://www.yogihosting.com/blog/implementing-google-contacts-api-version-3-0-oauth-2-0-in-csharp-and-asp-net/" rel="nofollow">here</a></p>