Asked by:
Google Calendar V3 in ASP.Net/VB.Net

Question
-
User-1147454113 posted
Here is a working sample of a Google Calendar V3 for VB.Net/Asp.Net Web sample flow.
Imports Google.Apis.Auth.OAuth2
Imports Google.Apis.Auth.OAuth2.Flows
Imports Google.Apis.Auth.OAuth2.Web
Imports Google.Apis.Services
Imports Google.Apis.Calendar.v3
Imports Google.Apis.Calendar.v3.Data
Imports Google.Apis.Util.Store
Imports System.Reflection
Imports System.Threading
Partial Class GoogleCalendarRegistrationV3
Inherits System.Web.UI.Page
Private Shared myClientID As String = System.Configuration.ConfigurationManager.AppSettings("GoogleCalendarApplicationClientID").ToString()
Private Shared ClientSecret As String = System.Configuration.ConfigurationManager.AppSettings("GoogleCalendarApplicationClientSecret").ToString()
Public Shared myCalendarservice As CalendarService
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
If Session("group_cd") Is Nothing Then
Response.Redirect("Logout.aspx")
End If
new_authentication()
End Sub
Protected Sub new_authentication()
Dim datafolder As String = Server.MapPath("App_Data/CalendarService.api.auth.store")
Dim scopes As IList(Of String) = New List(Of String)()
Dim UserId As String = "GoogleID_" & Session("username").ToString.Trim
scopes.Add(CalendarService.Scope.Calendar)
Dim myclientsecret As New ClientSecrets() With { _
.ClientId = myClientID, _
.ClientSecret = ClientSecret _
}
Dim flow As GoogleAuthorizationCodeFlow
flow = New GoogleAuthorizationCodeFlow(New GoogleAuthorizationCodeFlow.Initializer() With { _
.DataStore = New FileDataStore(datafolder), _
.ClientSecrets = myclientsecret, _
.Scopes = scopes _
})
Dim uri As String = Request.Url.ToString()
Dim code = Request("code")
If code IsNot Nothing Then
Dim token = flow.ExchangeCodeForTokenAsync(UserId, code, uri.Substring(0, uri.IndexOf("?")), CancellationToken.None).Result
' Extract the right state.
Dim oauthState = AuthWebUtility.ExtracRedirectFromState(flow.DataStore, UserId, Request("state")).Result
Response.Redirect(oauthState)
Else
Dim result = New AuthorizationCodeWebApp(flow, uri, uri).AuthorizeAsync(UserId, CancellationToken.None).Result
If result.RedirectUri IsNot Nothing Then
' Redirect the user to the authorization server.
Response.Redirect(result.RedirectUri)
Else
' The data store contains the user credential, so the user has been already authenticated.
AppFlowMetadata.myCalendarservice = New CalendarService(New BaseClientService.Initializer() With { _
.ApplicationName = "My Calendar", _
.HttpClientInitializer = result.Credential _
})
createcalendar()
Response.Redirect("Default.aspx")
End If
End If
End Sub
Protected Sub createcalendar()
Dim sfind As Boolean = False
' Fetch the list of calendar list
Dim list As IList(Of CalendarListEntry) = GoogleCalendarRegistrationV3.myCalendarservice.CalendarList.List().Execute().Items()
For Each item As CalendarListEntry In list
If item.Summary.Trim = "Calendar Sample" Then
sfind = True
Exit For
End If
Next
If sfind = False Then
Dim calendar As New Calendar()
calendar.Summary = "Calendar Sample"
calendar.TimeZone = "Asia/Manila"
calendar.Location = "Philippines"
calendar.Description = "calendar sample description"
Dim createdCalendar As Calendar = GoogleCalendarRegistrationV3.myCalendarservice.Calendars.Insert(calendar).Execute()
Dim rule As New AclRule()
Dim scope As New AclRule.ScopeData
scope.Type = "default"
scope.Value = "default"
rule.Scope = scope
rule.Role = "reader"
Dim createdRule As AclRule = GoogleCalendarRegistrationV3.myCalendarservice.Acl.Insert(rule, createdCalendar.Id).Execute
End If
End Sub
End Class
Tuesday, October 28, 2014 3:48 AM
All replies
-
User1741084486 posted
This is the example I have been searching for - thanks. One question - I want to be able to use the gmail ID of the person running the application, not just one userid. While I see where to enter the clientID, I don't see where to enter their password. I think that ClientSecret is the auth code I get from googleAPI and not the client user password. Am I missing where to code the gmail password?
Thanks
Tuesday, November 18, 2014 2:58 PM -
User1989386120 posted
Thank you so much!!! I've been searching and searching...... This is the first example I've found that worked for ASP.Net.
Wednesday, December 3, 2014 3:44 PM -
User961753308 posted
while running am getting the following exception: in
flow = New GoogleAuthorizationCodeFlow(New GoogleAuthorizationCodeFlow.Initializer() With { _ .DataStore = New FileDataStore(datafolder), _ .ClientSecrets = myclientsecret, _ .Scopes = scopes _ })
how can i clear it? what was the file i need to store in this location?
System.TypeInitializationException: The type initializer for 'Google.Apis.Http.ConfigurableMessageHandler' threw an exception. ---> System.IO.FileLoadException: Could not load file or assembly 'System, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e, Retargetable=Yes' or one of its dependencies. The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047)
at Google.Apis.Util.Utilities.GetLibraryVersion()
at Google.Apis.Http.ConfigurableMessageHandler..cctor() in c:\code\google.com\google-api-dotnet-client\default\Tools\Google.Apis.Release\bin\Debug\test\default\Src\GoogleApis.Core\Apis\Http\ConfigurableMessageHandler.cs:line 48
--- End of inner exception stack trace ---
at Google.Apis.Http.ConfigurableMessageHandler..ctor(HttpMessageHandler httpMessageHandler)
at Google.Apis.Http.HttpClientFactory.CreateHttpClient(CreateHttpClientArgs args) in c:\code\google.com\google-api-dotnet-client\default\Tools\Google.Apis.Release\bin\Debug\test\default\Src\GoogleApis.Core\Apis\Http\HttpClientFactory.cs:line 37
at Google.Apis.Auth.OAuth2.Flows.AuthorizationCodeFlow..ctor(Initializer initializer) in c:\code\google.com\google-api-dotnet-client\default\Tools\Google.Apis.Release\bin\Debug\test\default\Src\GoogleApis.Auth\OAuth2\Flows\AuthorizationCodeFlow.cs:line 195
at Google.Apis.Auth.OAuth2.Flows.GoogleAuthorizationCodeFlow..ctor(Initializer initializer) in c:\code\google.com\google-api-dotnet-client\default\Tools\Google.Apis.Release\bin\Debug\test\default\Src\GoogleApis.Auth\OAuth2\Flows\GoogleAuthorizationCodeFlow.cs:line 39
at testingApp.sample.new_authentication() in C:\Users\m.prabu\Desktop\testingApp\testingApp\sample.aspx.vb:line 37Friday, December 19, 2014 11:39 PM -
User-1147454113 posted
1. Add a App_Data folder
2. add CalendarService.api.auth.store folder inside App_data
3. Point you FileDataStore to this path.
Dim datafolder As String = Server.MapPath("App_Data/CalendarService.api.auth.store")
Wednesday, February 11, 2015 8:43 PM -
User-1507932088 posted
Hi Jon26rite,
I tried to test your code snippet. It works fine as it takes me to the google screen to select the account and redirects back to the URI mentioned in the google developer's console settings.
I've been trying really hard to implement the following scenario.
I've got a web application for a hotel which takes online booking. I want to implement the google calander (using service account) with this application so that whenever a booking is made by the client, the booking details are posted automatically in a shared calander, which is shared with certain member's of the Hotel management. So on every event, a reminder is popped up in the user's handsets and he can get the reminder of arrival and checkout of the guests at the reminder threshold specified.
Can you please help how to do this?
Wednesday, March 4, 2015 12:11 AM -
User-148676290 posted
At last, i found this sample.....
I got an error
Compiler Error Message: BC30451: 'AppFlowMetadata' is not declared. It may be inaccessible due to its protection level.
Sorry for my newbie question because i just started to develop webapp using Google API.
Is there any step that i missed?
Tuesday, July 7, 2015 7:06 AM -
User1924419024 posted
while running i get an exception, help please:
Línea 44: }) Línea 45: Línea 46: Dim uri As String = Request.Url.ToString() Línea 47: Línea 48: Dim code = Request("code")
Tuesday, September 1, 2015 10:23 AM -
User-796304858 posted
Thanks. Works very well. I had to replace the AppFlowMetadata.myCalendarservice reference with GoogleCalendarRegistrationV3.myCalendarservice in order to it to execute properly.
Sunday, February 12, 2017 10:56 PM