如何使用域范围的授权服务帐户在谷歌日历中创建事件。我正在尝试这样做,但它会引发错误:
Protected Sub btnCreate_Click(sender As Object, e As EventArgs) Handles btnCreate.Click
Dim pathJsonfile As String = HttpContext.Current.Server.MapPath("~/ServiceAccounts/servicecalendar-303214-dbe0f6123f.json")
Dim _service As CalendarService = Me.GetCalendarService(pathJsonfile)
CreateEvent(_service)
End Sub
Private Function GetCalendarService(ByVal keyfilepath As String) As CalendarService
Try
Dim Scopes As String() = {
CalendarService.Scope.Calendar,
CalendarService.Scope.CalendarEvents,
CalendarService.Scope.CalendarEventsReadonly
}
Dim credential As GoogleCredential
Using stream = New FileStream(keyfilepath, FileMode.Open, FileAccess.Read)
credential = GoogleCredential.FromStream(stream).CreateScoped(Scopes).CreateWithUser("mail@domain.com")
End Using
Dim service = New CalendarService(New BaseClientService.Initializer() With {
.HttpClientInitializer = credential,
.ApplicationName = "TEST APP"
})
Return service
Catch ex As Exception
Throw
End Try
End Function
Private Sub CreateEvent(ByVal _service As CalendarService)
Dim body As [Event] = New [Event]()
Dim a As EventAttendee = New EventAttendee()
a.Email = "mail@domain.com"
Dim b As EventAttendee = New EventAttendee()
b.Email = "mail3@domain.com"
Dim attendes As List(Of EventAttendee) = New List(Of EventAttendee)()
attendes.Add(a)
attendes.Add(b)
body.Attendees = attendes
Dim start As EventDateTime = New EventDateTime()
start.DateTime = Convert.ToDateTime("2021-02-03T04:00:00-0500")
Dim [end] As EventDateTime = New EventDateTime()
[end].DateTime = Convert.ToDateTime("2021-02-03T05:00:00-0500")
body.Start = start
body.[End] = [end]
body.Location = "INTERNET"
body.Summary = "SUMMARY"
Dim objRequest As EventsResource.InsertRequest = New EventsResource.InsertRequest(_service, body, "developer@cvudes.edu.co")
Dim response As [Event] = objRequest.Execute()
End Sub
<块引用>
Google.Apis.Auth.OAuth2.Responses.TokenResponseException:'错误:“unauthorized_client”,描述:“客户端无权使用此方法检索访问令牌,或客户端未获得任何请求范围的授权。”,Uri :""'
答案 0 :(得分:0)
客户端无权使用此方法检索访问令牌,或客户端未获得任何请求范围的授权
您可以在 Google Developer Console 上创建三种类型的授权
这些方法中的每一个都用于不同的目的,并且每个方法都有不同的代码来使用它们。
您的代码使用了错误的客户端类型。我认为您应该仔细检查您是否在 google 开发者控制台上创建了服务帐户凭据,而不是其他类型的凭据。
答案 1 :(得分:-1)
如documentation中所述,转到:
Client ID
OAuth scopes
另外,请确保您正确创建了服务帐户服务,here 是 .net
的一个工作示例。