I am using the latest version of the Google .NET Client API (v1.81). I am trying to connect to the Calendar Service using the following code
var calendarService = new CalendarService(new BaseClientService.Initializer
{
HttpClientInitializer = GetCredential(),
ApplicationName = "MyApp"
});
public UserCredential GetCredential()
{
UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
new ClientSecrets { ClientId = _clientId, ClientSecret = _clientSecret },
new[] { CalendarService.Scope.Calendar }.ToList(), "user", CancellationToken.None,
new FileDataStore("Drive.Auth.Store")).Result;
return credential;
}
When I do this it opens a new Browser Window and asks for me to authenticate. I have previously authenticated and the key is stored and retrieved via FileDataStore.
Why would it be asking me to authenticate again? The problem is because this code needs to run in a background service so it can't open browser tabs.
Another issue I now am getting, when I try and authorise it opens up a new browser window, I select my credentials but the login page then tells me
"The redirect URI in the request: http://localhost:51773/authorize/
did not match a registered redirect URI"
Why is it trying to use the Authorize url and not the AuthCallbackController. It also seems to randomly change the port number of the redirect uri making registering it impossible.
The ultimate question probably is, if I have a web app and I want the users to sign in using their credentials via the web page and then reuse those credentials at a later date in a background task on the server, how do I go about that? I couldn't find any relevant sample app to show how. It appears the FileDataStore does not store the credentials so they can be reused. I have also implemented my own DataStore which saves them in the database which doesn't seem to work either.
Digging further into this it appears I need offline_access. I have implemented this the same as Google Analytics OAuth with AccessType = Offline in C# but it still prompts. How to I reuse the offline_access refresh token?