How to create ContactService with Google Contact API v3 with OAuth v2 UserCredentials

My app uses Google API Calendar V3 with OAuth and this works great. First, he will ask the user for his consent. It's easy to use the calendar service to create, edit, and delete calendar events. So far, so good!

Now I want to give the user permission to add and change contact information. By adding the following line: https://www.google.com/m8/feeds/ , the user is also prompted for permission to access the contact. It seems to work. However, I cannot find a way to create a ContactService based on the UserCredential obtained from the above process. How do I use UserCredential with ContactService?

I have read all the questions tagged: google-contact and google-api-dotnet-client. I checked the API V3 documentation and I found creating a service for Calendar, Drive and tons of other APIs, but not Contact? What am I missing?

This is the piece of code I am using to request permission and start the calendar service.

using Google.Contacts;
using Google.GData.Contacts;
using Google.GData.Client;
using Google.GData.Extensions;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Calendar;
using Google.Apis.Calendar.v3;
using Google.Apis.Calendar.v3.Data;
using System.Threading;
using Google.Apis.Services;

namespace MY
{
class GoogleContact
{
    static public void start_service()
    {

        UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
            new ClientSecrets
            {
                ClientId = "clientID",
                ClientSecret = "clientsecret",
            },
            new[] { CalendarService.Scope.Calendar, "https://www.google.com/m8/feeds/" }, // This will ask the client for concent on calendar and contatcs
            "user",
            CancellationToken.None).Result;


        // Create the calendar service.
        CalendarService cal_service = new CalendarService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = "Calendar API Sample",
        });

        // How do I use the found credential to create a ContactsService????
        ContactsService service = new ContactsService("APP_NAME");


    }

      

I appreciate any feedback on the next steps I should be taking?

UPDATE, after receiving feedback:

I have added the following piece of code to use the Contact data.

        // Get the tokens from the FileDataStore
        var token = new FileDataStore("Google.Apis.Auth")
            .GetAsync<TokenResponse>("user"); 

        OAuth2Parameters parameters = new OAuth2Parameters
        {
            ClientId = mSecrets.ClientId,
            ClientSecret = mSecrets.ClientSecret,
            // Note: AccessToken is valid only for 60 minutes
            AccessToken = token.Result.AccessToken, 
            RefreshToken = token.Result.RefreshToken
        };

        RequestSettings settings = new RequestSettings(
            "Contact API Sample", parameters);
        ContactsRequest cr = new ContactsRequest(settings);
        Feed<Contact> f = cr.GetContacts();
        // The AccessCode is automatically updated after expiration!
        foreach (Contact c in f.Entries)
        {
            Console.WriteLine(c.Name.FullName);
        }

      

I first read the access token and refresh token from the FileDataStore.
Then I set the OAuth2Parameters using the token I just read.
And now I can create a new ContactService and ContactRequest.

To my surprise, the access token automatically renews after expiration also for the ContactService.

+3


source to share


1 answer


You just can't.
You are trying to combine two different libraries (GData and Google API).

The GData documentation is available at: https://code.google.com/p/google-gdata/ and the Contacts API NuGet package is available at: https://www.nuget.org/packages/Google.GData.Contacts/ .

The Google API Client Library for .NET is a new library. BUT unfortunately the Contacts API doesn't support it. A list of all supported Google APIs is available at: https://developers.google.com/api-client-library/dotnet/apis/ , you can find Getting Started .

UPDATE: I'm not familiar with the GData API, but ...

1) You can add more scopes besides calendar in AuhorizeAsync method to include contact scopes (if any)

2) You can use the access token + refresh token from the datastore (by default you use FileDataStore and initiate a GData request (again I'm not familiar with the API) to use the access token.



This might work for you, but you need to research more .. I haven't tried this because I'm not familiar with GData.

UPDATE 2: adding the correct call to FileDataStore:

var token = new FileDataStore("Google.Apis.Auth")
    .GetAsync<TokenResponse>("user");
var accessToken = token.AccessToken; // valid for 60 minutes ONLY.
var refreshToken = token.RefreshToken;

      

Should receive a token response containing an access and update token.

** GoogleWebAuthorizationBroker is responsible for creating the default datastore using the above folder, unless the user provided one (your case).

** The Auth library is responsible for storing correct data. See authorization code flow for details .

+2


source







All Articles