0

Im trying to get info of a gmail message in a datagridview but only returns the datagridview empty (even when I erase the "return nothing" line) but when I try in apis-explorer I get the 200 ok response. What am I doing wrong?

The code Im using is from the google developers documentation:

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 GetMessage(service As GmailService, userId As [String], messageId As [String]) As Message
        Try
            Return service.Users.Messages.Get(userId, messageId).Execute()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
        'Return Nothing
    End Function
    Private Sub Button14_Click(sender As Object, e As EventArgs) Handles Button14.Click
        Dim userId As String = "GMAIL ACCOUNT"
        Dim messageId As String = "MESSAGE ID"
        Try
            scope.Add(GmailService.Scope.MailGoogleCom)
            Dim credential = GoogleWebAuthorizationBroker.AuthorizeAsync(Secrets, scope, "XXXXXXXXXXXX@developer.gserviceaccount.google.com", CancellationToken.None).Result()
            initializer.HttpClientInitializer = credential
            initializer.ApplicationName = "APP NAME"
            Dim service = New GmailService(initializer)
            Me.DataGridView1.DataSource = GetMessage(service, userId, messageId)
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
End Class
Eric D
  • 6,901
  • 1
  • 15
  • 26
Alan Alvarez
  • 646
  • 2
  • 11
  • 32
  • Are you able to get any calls to work? Say something trivial like labels.list()? I presume it's not throwing an Exception? If that's the case, can you please print out and include the entire response received (HTTP headers and body)? – Eric D Sep 25 '14 at 17:05

1 Answers1

-1

Thanks Eric but I already solved making some changes. I hope this helps other people because the google developers documentation its not too clear

Imports Google.Apis.Auth.OAuth2
Imports Google.Apis.Services
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 GetMessage(service As GmailService, userId As [String], messageId As [String]) As Message
        Try
            Return service.Users.Messages.Get(userId, messageId).Execute
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try

        Return Nothing
    End Function

    Private Sub Button14_Click(sender As Object, e As EventArgs) Handles Button14.Click
        Try
            scope.Add(GmailService.Scope.MailGoogleCom)
            Dim credential = GoogleWebAuthorizationBroker.AuthorizeAsync(Secrets, scope, "XXXXXXXXXXXXX@developer.gserviceaccount.google.com", CancellationToken.None).Result()
            initializer.HttpClientInitializer = credential
            initializer.ApplicationName = "APP NAME"
            Dim service = New GmailService(initializer)
            Dim userId As String = "EMAIL"
            Dim messageId As String = "MESSAGE ID"

            Me.DataGridView1.DataSource = GetMessage(service, userId, messageId).Payload.Headers
            TextBox1.Text = (GetMessage(service, userId, messageId).Snippet)        
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
End Class 
Alan Alvarez
  • 646
  • 2
  • 11
  • 32
  • Rather than posting an entire new code segment can you just summarize what was the bug originally? – Eric D Sep 25 '14 at 22:52
  • Sorry, Im new with apis. I dont know what was the bug originally, I just put the "dim userId" and "dim messageId" in another place and then the code work perfectly. – Alan Alvarez Sep 28 '14 at 14:01
  • I'm not particularly familiar with .NET but moving the variable declaration "dim ..." from outside the try block to inside it seems very unlikely to have changed anything here--i think that was a red herring and your issue is elsewhere. – Eric D Sep 29 '14 at 15:52