【发布时间】:2015-09-30 02:38:42
【问题描述】:
我已经搜索了几天,但我无法得到答案,所以我创建了这个帖子。
我开发了一个网络应用程序,因此用户可以在他们的 Google 日历中创建活动。这是工作。但是,我想不通,为什么这个应用程序只询问用户凭据一次。
例如:
- 用户 John 访问 .aspx 页面,然后他重定向到 Google 授权页面,因为这是 John 第一次访问该页面。
- 授权后,John 可以在他的 Google 日历中创建活动。
在这一步之前它一直有效。当约翰从他的谷歌帐户注销时出现问题。
- 如果 Dave 从另一台计算机访问此页面,他不会被重定向到 Google 授权页面,而是突然直接在 JOHN 的日历。
谁能帮帮我,为什么会出现这个问题?
这是我的代码:
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
【问题讨论】:
-
您将什么值传递给 Dim code = Request("code") 以在此处编码?你在这里存放约翰的令牌吗?此外,要了解 oauth 流程,请查看此 oauth play ground developers.google.com/oauthplayground,您可以在其中了解授权和身份验证是如何完成的。
-
如果您从 Google 页面授权重定向回来,则代码是参数。
标签: asp.net vb.net google-api google-calendar-api