I am working with VB.Net, I followed the instructions to get a list of messages (gmail api v1) and tasks (tasks api v1) from Google:
https://developers.google.com/gmail/api/v1/reference/users/messages/list https://developers.google.com/google-apps/tasks/v1/reference/tasks/list
But I get the error 403 (Insufficient permission) but my datagridview is filled with the following headers with empty cells:
Gmail:
HistoryId - Id - LabelIds - Payloads - Raw - SizeEstimate - Snippet - ThreadId - Etag
Tasks:
Etag - Items - Kind - NextPageToken
I checked my google developer console and all is well, the apis are activated.
I also use my xxxxxxxxx@developer.gserviceaccount.com (just in case).
Also follow the instructions of Drive and Calendar apis and these work fine but of Tasks and Gmail give me that problem.
I use credentials (client id and client secret) to enter.
I tried on Google api explorer and everything works fine.
How can I fix this error?
Here's my Tasks and Gmail codes:
Imports Google.Apis.Auth.OAuth2
Imports Google.Apis.Services
Imports Google.Apis.Tasks.v1
Imports Google.Apis.Tasks.v1.Data.Tasks
Imports Google.Apis.Tasks.v1.Data
Imports System.Collections.Generic
Imports Google.Apis.Util.Store
Imports System.Threading
Imports System
Public Class Form1
Dim Secrets = New ClientSecrets()
Dim scope = New List(Of String)
Dim initializer = New BaseClientService.Initializer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Secrets.ClientId = "MY CLIENT ID"
Secrets.ClientSecret = "MY CLIENT SECRET"
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
scope.Add(TasksService.Scope.Tasks)
Try
Dim credential = GoogleWebAuthorizationBroker.AuthorizeAsync(Secrets, scope, "My Gmail account or developer account &.GOOGLE.COM", CancellationToken.None).Result()
initializer.HttpClientInitializer = credential
initializer.ApplicationName = "APP NAME"
Dim service = New TasksService(initializer)
Me.DataGridView1.DataSource = retrieveAllTasks(service)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Public Shared Function retrieveAllTasks(service As TasksService) As List(Of Tasks)
Dim result As New List(Of Tasks)()
Dim request As TasksResource.ListRequest = service.Tasks.List("@default")
Do
Try
Dim Gtasks As Tasks = request.Execute()
result.AddRange(Gtasks.Items)
request.PageToken = Gtasks.NextPageToken
Catch ex As Exception
MsgBox(ex.ToString)
request.PageToken = Nothing
End Try
Loop While Not [String].IsNullOrEmpty(request.PageToken)
Return result
End Function
End Class
Imports Google.Apis.Auth.OAuth2
Imports Google.Apis.Services
Imports Google.Apis.Tasks.v1
Imports Google.Apis.Tasks.v1.Data.Tasks
Imports Google.Apis.Tasks.v1.Data
Imports System.Collections.Generic
Imports Google.Apis.Util.Store
Imports System.Threading
Imports System
Imports Google.Apis.Gmail.v1
Imports Google.Apis.Gmail.v1.Data
Public Class Form1
Dim Secrets = New ClientSecrets()
Dim scope = New List(Of String)
Dim initializer = New BaseClientService.Initializer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Secrets.ClientId = "CLIENT ID"
Secrets.ClientSecret = "CLIENT SECRET"
End Sub
Public Shared Function ListMessages(service As GmailService) As List(Of Message)
Dim result As New List(Of Message)()
Dim request As UsersResource.MessagesResource.ListRequest = service.Users.Messages.List("TARGET EMAIL")
Do
Try
Dim response As ListMessagesResponse = request.Execute()
result.AddRange(response.Messages)
request.PageToken = response.NextPageToken
Catch ex As Exception
MsgBox(ex.ToString)
End Try
Loop While Not [String].IsNullOrEmpty(request.PageToken)
Return result
End Function
Private Sub Button10_Click(sender As Object, e As EventArgs) Handles Button10.Click
Try
scope.Add(GmailService.Scope.MailGoogleCom)
Dim credential = GoogleWebAuthorizationBroker.AuthorizeAsync(Secrets, scope, "XXXXXXXX@developer.gserviceaccount.google.com", CancellationToken.None).Result()
initializer.HttpClientInitializer = credential
initializer.ApplicationName = "APP NAME"
Dim service = New GmailService(initializer)
Me.DataGridView1.DataSource = ListMessages(service)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
End Class