title: How to use Google Contacts API via Oauth2 authentication
body: <p>I have this below code to get calendar entries using the google Calendar API (<a href="https://developers.google.com/google-apps/calendar/" rel="nofollow">https://developers.google.com/google-apps/calendar/</a>) which uses OAuth2.
  It works well. </p>
  
  <pre><code>private IList&lt;string&gt; scopes = new List&lt;string&gt;();
  private CalendarService calendarService;
  
  private void InitializeCalendarService()
  {
          // Add the calendar specific scope to the scopes list
          scopes.Add(CalendarService.Scopes.Calendar.GetStringValue());
  
          // Display the header and initialize the sample
          CommandLine.EnableExceptionHandling();
          CommandLine.DisplayGoogleSampleHeader("Google.Api.Calendar.v3 Sample");
  
          // Create the authenticator
          //FullClientCredentials credentials = PromptingClientCredentials.EnsureFullClientCredentials();
          var provider = new NativeApplicationClient(GoogleAuthenticationServer.Description);
  
          FullClientCredentials credentials = new FullClientCredentials();
          credentials.ClientId = "XYZ.apps.googleusercontent.com";
          credentials.ClientSecret = "XYZ";
          credentials.ApiKey = "XYZ";
  
          provider.ClientIdentifier = credentials.ClientId;
          provider.ClientSecret = credentials.ClientSecret;
          OAuth2Authenticator&lt;NativeApplicationClient&gt; auth = new OAuth2Authenticator&lt;NativeApplicationClient&gt;(provider, GetAuthorization);
  
          // Create the calendar service using an initializer instance
          BaseClientService.Initializer initializer = new BaseClientService.Initializer();
          initializer.Authenticator = auth;
          calendarService = new CalendarService(initializer);
  
          CalendarList list = calendarService.CalendarList.List().Execute();
          // do something with the list .. the list is all good
  
  } 
  
  public IAuthorizationState GetAuthorization(NativeApplicationClient client)
  {
          // You should use a more secure way of storing the key here as
          // .NET applications can be disassembled using a reflection tool.
          const string STORAGE = "google.samples.dotnet.calendar";
          const string KEY = "s0mekey";
  
          // Check if there is a cached refresh token available.
          IAuthorizationState state = AuthorizationMgr.GetCachedRefreshToken(STORAGE, KEY);
          if ((state != null))
          {
              try
              {
                  client.RefreshToken(state);
                  return state;
                  // we are done
              }
              catch (DotNetOpenAuth.Messaging.ProtocolException ex)
              {
                  CommandLine.WriteError("Using an existing refresh token failed: " + ex.Message);
                  CommandLine.WriteLine();
              }
          }
  
          // Retrieve the authorization from the user
          string[] array = new string[scopes.Count];
          scopes.CopyTo(array,0);
          state = AuthorizationMgr.RequestNativeAuthorization(client, array);
          AuthorizationMgr.SetCachedRefreshToken(STORAGE, KEY, state);
          return state;
  } 
  </code></pre>
  
  <p>How can I use the similar OAuth2Authenticator to fetch Contacts?</p>
  
  <p>I am able to fetch contacts using the below code, but its not password-less, I need to get it working using Oath2. The example below uses Gdata contacts api v2. I can see that i can pass through  OAuth2Authenticator but im not exactly sure how to do it correctly (i cant see any valid examples in C# on the google site) and fetch the access code based on what the user is selecting.
  I cant see how to use OAuth2Authenticator with the contacts api v3 (<a href="https://developers.google.com/google-apps/contacts/v3/" rel="nofollow">https://developers.google.com/google-apps/contacts/v3/</a>) </p>
  
  <pre><code>RequestSettings rsLoginInfo = new RequestSettings("", email,pwd);
  rsLoginInfo.AutoPaging = true;
  ContactsRequest cRequest = new ContactsRequest(rsLoginInfo);
  
  // fetch contacts list
  Feed&lt;Contact&gt; feedContacts = cRequest.GetContacts();
  foreach (Contact gmailAddresses in feedContacts.Entries)
  {
          // Looping to read  email addresses
          foreach (EMail emailId in gmailAddresses.Emails)
          {
             lstContacts.Add(emailId.Address);
          }
  }
  </code></pre>