NB: at the time of writing, you can only use two-way authentication using Google Apps for Business / Eduction, this will not work on personal accounts, since there is no way to get the key / secret pair OAuth 1.0, you will need to use at least one online authentication times (but you can use the option outside the browser, so you do not need to create a dedicated page).
Your code is correct except that you do not need the first three lines related to NativeApplicationClient . This most likely does not work, because you misconfigured the OAuth keys, this causes 401.
Another thing that calls 401s uses " matt@example.com " instead of "matt" as the username, the username does not include your domain.
To configure OAuth, follow the instructions in this article from Google.
The most important parts that should be noted are "Allow access to all APIs", should not be checked , and you need to individually provide access to all APIs. If this has not been done, you will get 401 Invalid Credentials error. Then you also need to enable these services in the api console. If the api console step has not been completed, you will get another 403 Daily Limit Exceeded error.
This will cause problems if you previously relied on “Allow access to all APIs” to use various services, you will have to provide them individually, as I understand it, to use the v3 APIs. This seems to have been confirmed by Google (4th answer by Nicholas Garnier) and supposedly a mistake, but this is an old article, so it looks as if it is here to stay.
For reference, once this is done, this code will work, which essentially matches yours:
var auth = new OAuth2LeggedAuthenticator(domainName, consumerSecret, usernameWithoutDomain, domainName); //domainName is presently used as the OAuth ConsumerKey for Google 2legged OAuth var service = new CalendarService(auth); service.Key = serviceKey; var results = service.CalendarList.List().Fetch(); Console.WriteLine(results.Items.Count);
So in short:
In Google Apps, Manage This Domain> Advanced Tools
Using the OAuth Domain Key Management key, create a secret, uncheck the box "Allow access to all APIs".
Using the “Third-Party OAuth Client Access Control” allows the APIs that you want to access your domain, like the “Client Name” and the APIs that you want to access, for example. "http://www.google.com/calendar/feeds/" for the calendar.
Then finally create the project in the API console , use the APIKey as the serviceKey in the above example, and enable the APIs needed for access.
I answer this as I continued to run into this question when I was trying to find out why my code was constantly returning 401. I hope this helps someone as Google instructions are horrible and scattered all over the place for now.