I’m working on a web application that needs to add events to users’ Google Calendars. I’ve already completed the OAuth flow and have the access token and refresh token stored in my database.
The issue I’m facing is how to create the proper credentials object for the Google Calendar API using these stored tokens. Most examples I find use GoogleWebAuthorizationBroker.AuthorizeAsync() which is designed for desktop apps where the user authenticates interactively.
Here’s what I’m trying to achieve:
// I have these stored values from previous OAuth flow
string storedAccessToken = "ya29.abc123...";
string storedRefreshToken = "1//def456...";
string myClientId = "client123.googleusercontent.com";
string myClientSecret = "secret789";
// Need to create credentials without user interaction
// This doesn't work for server-side scenarios:
var authBroker = await GoogleWebAuthorizationBroker.AuthorizeAsync(
new ClientSecrets { ClientId = myClientId, ClientSecret = myClientSecret },
new[] { CalendarService.Scope.Calendar },
"[email protected]",
CancellationToken.None);
var calendarService = new CalendarService(new BaseClientService.Initializer()
{
HttpClientInitializer = authBroker
});
How can I construct the credentials object directly from my stored tokens instead of going through the authorization broker again?