Client is unauthorized error when authorizing service account

Viewed 216

How you can create an event in google calendar with a domain-wide authority service account. I am trying to do this but it throws an error:

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: 'Error:"unauthorized_client", Description:"Client is unauthorized to retrieve access tokens using this method, or client not authorized for any of the scopes requested.", Uri:""'

2 Answers

Client is unauthorized to retrieve access tokens using this method, or client not authorized for any of the scopes requested

There are three types of authorization that you can create on Google Developer console

  • web client
  • installed client
  • service account

Each of these methods are used for different purposes, and each has a different code needed to use them.

You have use the wrong client type for your code. I think you should double check that you have created service account credentials on google developer console and not some other type of credentials.

For using a service account with impersonation, you need to authorize all the used scopes in the Admin console

As mentioned in the documentation, go to:

  • Main menu ->Security-> API Controls-> Manage Domain Wide Delegation
  • Add the Client ID of your service account
  • Add to OAuth scopes all the scopes you are going to use in your code

Also, make sure you create the service account service corectly, here is a working example for .net.

Related