gpt4 book ai didi

asp.net - Google API Calendar v3 token 身份验证仅一次

转载 作者:行者123 更新时间:2023-12-02 04:47:57 26 4
gpt4 key购买 nike

我已经搜索了几天,但找不到答案,所以我创建了这篇文章。

我开发了一个网络应用程序,因此用户可以在他们的 Google 日历中创建事件。这是工作。但是,我不明白为什么这个应用程序只询问用户凭据一次。

例如:

  1. User John access the .aspx page then he redirected to Google Authorized page because it's the first time John access the page.
  2. After Authorized, John can create an event in his Google Calendar.

一直到这一步都有效。当约翰从他的谷歌帐户注销时出现问题。

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

谁能帮帮我,为什么会出现这个问题?

这是我的代码:

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

这是我的创建日历子

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's 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

最佳答案

我解决了我的问题。我发现 token 的名称总是相同的,这就是发生此问题的原因。所以,只需替换这段代码:

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"

进入

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}

关于asp.net - Google API Calendar v3 token 身份验证仅一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31356140/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com