Google API Calendar v3 Token authentication only once

I've searched for days, but I can't get an answer, so I'm creating this post.

I have developed a web application so that a user can create an event in their Google Calendar. It works. But I can't figure out why this app only prompts the user once.

For example:

  • User John connected to the .aspx page, then redirected him to the Google login page, since he first came to the page.
  • Once logged in, John can create an event in his Google Calendar.

It works up to this step. The problem came when John was logged out of his google account.

  1. If Dave accesses this page from another computer, he is not redirected to the Google login page and suddenly creates an event in JOHN's Calendar.

Can anyone help me why this problem is happening?

this is my code:

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_co"

    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.
            myCalendarservice = New CalendarService(New BaseClientService.Initializer() With { _
              .ApplicationName = "My Calendar", _
              .HttpClientInitializer = result.Credential _
            })

            createcalendar()


        End If
    End If

End Sub

      

This is my createcalendar sub

Protected Sub createcalendar()
   Dim newEvent As New [Event]() With { _
       .Summary = "Google I/O 2015", _
       .Location = "800 Howard St., San Francisco, CA 94103", _
       .Description = "A chance to hear more about Google developer products.", _
       .Start = New EventDateTime() With { _
           .DateTime = DateTime.Parse("2015-07-13T09:00:00-07:00"), _
           .TimeZone = "America/Los_Angeles" _
       }, _
       .[End] = New EventDateTime() With { _
           .DateTime = DateTime.Parse("2015-07-14T17:00:00-07:00"), _
           .TimeZone = "America/Los_Angeles" _
       }, _
       .Recurrence = New [String]() {"RRULE:FREQ=DAILY;COUNT=2"}, _
       .Attendees = New EventAttendee() {New EventAttendee() With { _
           .Email = "lpage@example.com" _
       }, New EventAttendee() With { _
           .Email = "sbrin@example.com" _
       }}, _
       .Reminders = New [Event].RemindersData() With { _
           .UseDefault = False, _
           .[Overrides] = New EventReminder() {New EventReminder() With { _
               .Method = "email", _
               .Minutes = 24 * 60 _
           }, New EventReminder() With { _
               .Method = "sms", _
               .Minutes = 10 _
           }} _
       } _
   }

    Dim calendarId As [String] = "primary"
    Dim request As EventsResource.InsertRequest = myCalendarservice.Events.Insert(newEvent, calendarId)
    Dim createdEvent As [Event] = request.Execute()
End Sub

      

+3


source to share


1 answer


I solved my problem. I found that the name of the marker is always the same, which is why this problem occurred. So, just replace this code:

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_co"

      



in

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_co" & {unique Identifier such as userid,username,etc}

      

0


source







All Articles