0

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
Alan Alvarez
  • 646
  • 2
  • 11
  • 32
  • Please attach code... It seams that you didn't use the OAuth 2.0 protocol correctly – peleyal Sep 15 '14 at 12:35
  • Thank you Eyal(?), I edit the question now with the codes. – Alan Alvarez Sep 15 '14 at 13:20
  • Why do you set the ClientId and ClientSecret after calling the AuthorizeAsync, do that before... (That's true to adding the right scopes as well). Please fix the code, and let me know – peleyal Sep 15 '14 at 13:22
  • Ok peleyal Ill do it. Just in case I forgot to say that after showing the error the lines that have "execute" are highlighted. – Alan Alvarez Sep 15 '14 at 13:27
  • I did what you said but it shows the same error and points the "execute" line. When I try with Calendar and Drive apis works fine but with tasks and gmail dont. – Alan Alvarez Sep 15 '14 at 13:45
  • Can you update the code? – peleyal Sep 15 '14 at 14:41
  • Sorry for the delay, I updated the code. Also I tried change the ClientId and ClientSecret in several places. – Alan Alvarez Sep 15 '14 at 17:56
  • Do you set the scopes before the request? – peleyal Sep 15 '14 at 18:56
  • I tried with diferent versions of the api, doesnt work. – Alan Alvarez Sep 16 '14 at 21:27
  • As I see in your code, you are still setting the scopes after the call to the authorization method. Please update your code. Also, please add the exact error you are getting (maybe from fiddler...?). 403 is an error you get when you don't have the right scopes, or you didn't enable the API in the project explorer.... – peleyal Sep 17 '14 at 09:04
  • Updated the code. The apis are enabled and when I try in apis-explorer I get a 200 OK response. Just in case I have my account open on this computer. Here you have pics of the error and the headers I get in my datagridview: https://mega.co.nz/#!PV13ADwa!xnbZzT6grCWz7th45B3eLTgqjXgXHk39bZu-wQrAm1w – Alan Alvarez Sep 17 '14 at 13:53
  • Can you try to change the gmail scope to be "https://mail.google.com/", it should be one of declared in the scope class. In the gmail developers page "https://developers.google.com/gmail/api/auth/scopes" it says that this scope will give you full access. – peleyal Sep 18 '14 at 12:24
  • Thank you very much Peleyal, now I can get the "ID" and "ThreadID" data, but the tasks api still with the same error. I updated the code. Please can you help me with that too? – Alan Alvarez Sep 18 '14 at 17:08
  • Which flow are you trying to use? https://developers.google.com/accounts/docs/OAuth2ServiceAccount OR https://developers.google.com/accounts/docs/OAuth2InstalledApp?? – peleyal Sep 20 '14 at 14:19
  • OAuth 2.0 for Installed Applications – Alan Alvarez Sep 20 '14 at 19:50

0 Answers0