使用gmail api v1(Net)发出“获取”消息

时间:2014-09-21 22:32:33

标签: vb.net datagridview google-api-dotnet-client gmail-api

我试图在datagridview中获取gmail消息的信息,但只返回datagridview为空(即使我删除了“return nothing”行)但是当我在apis-explorer中尝试时,我得到200 ok响应。我做错了什么?

我使用的代码来自Google开发者文档:

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

1 个答案:

答案 0 :(得分:-1)

感谢Eric,但我已经解决了一些变化。我希望这有助于其他人,因为谷歌开发者的文档不太清楚

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