我有一个拥有所有权限和订阅的主题。从谷歌云控制台我可以列出我的主题。但是从c#代码或TRY IT页面获取无效的主题名称。我创建了一个项目 启用了pub / sub api 创建主题 创建订阅 获得许可 创建了api密钥 我从gmail收到未读邮件,这意味着密钥正常工作。但不能得到主题名称 这是我的代码:
using Google.Apis.Auth.OAuth2;
using Google.Apis.Gmail.v1;
using Google.Apis.Gmail.v1.Data;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Google.Cloud.PubSub.V1;
namespace ConsoleApplication4
{
class Program
{
static string[] Scopes = { GmailService.Scope.GmailReadonly };
static string ApplicationName = "Gmail API .NET Quickstart";
static void Main(string[] args)
{
UserCredential credential;
using (var stream =
new FileStream("client_secret2.json", FileMode.Open,
FileAccess.Read))
{
string credPath = System.Environment.GetFolderPath(
System.Environment.SpecialFolder.Personal);
credPath = Path.Combine(credPath, ".credentials/gmail-dotnet-
quickstart.json");
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
Console.WriteLine("Credential file saved to: " + credPath);
}
// Create Gmail API service.
var service = new GmailService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
// Define parameters of request.
//UsersResource.LabelsResource.ListRequest request =
service.Users.Labels.List("me");
List<Message> result = new List<Message>();
UsersResource.MessagesResource.ListRequest request =
service.Users.Messages.List("me");
request.Q = "is:unread";
do
{
try
{
ListMessagesResponse response = request.Execute();
result.AddRange(response.Messages);
request.PageToken = response.NextPageToken;
}
catch (Exception e)
{
Console.WriteLine("An error occurred: " + e.Message);
}
} while (!String.IsNullOrEmpty(request.PageToken));
foreach (Message m in result)
{
Message m2 = service.Users.Messages.Get("me", m.Id).Execute();
Console.WriteLine("Message: {0}", m2.Snippet);
}
WatchRequest body = new WatchRequest()
{
TopicName = "projects/test/topics/topicTest",
LabelIds = new[] { "INBOX" }
};
string userId = "me";
UsersResource.WatchRequest watchRequest = service.Users.Watch(body,
userId);
WatchResponse test = watchRequest.Execute();
Console.Read();
}
}
}