我正在使用VB.Net,我按照说明从Google获取消息列表(gmail api v1)和任务(任务api v1):
https://developers.google.com/gmail/api/v1/reference/users/messages/list https://developers.google.com/google-apps/tasks/v1/reference/tasks/list
但我收到错误403(权限不足),但我的datagridview填充了以下带有空单元格的标题:
的Gmail:
HistoryId - Id - LabelIds - Payloads - Raw - SizeEstimate - Snippet - ThreadId - Etag
任务:
Etag - Items - Kind - NextPageToken
我查看了我的google开发者控制台,一切顺利,api已激活。
我也使用我的xxxxxxxxx@developer.gserviceaccount.com(以防万一)。
另请按照云端硬盘和日历api的说明操作,这些工作正常,但任务和Gmail给我这个问题。
我使用凭证(客户端ID和客户端密码)进入。
我试过Google api资源管理器,一切正常。
如何解决此错误?
以下是我的任务和Gmail代码:
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