使用 Go 通过 GCP pub/sub 设置 Gmail 推送通知
Setting up Gmail Push notifications through GCP pub/sub using Go
我希望对我的应用程序进行基本设置,以便在每次邮件到达 Gmail 收件箱时它都能收到通知。
我正在按照指南进行操作 here。
- 我创建了主题和订阅。
- 我的凭据有效。我可以使用凭证和 Go 脚本检索我的电子邮件,如图 here.
- 作为 Pub/Sub 发布者,我已与
gmail-api-push@system.gserviceaccount.com
启用了对我的主题 gmail
的权限。
- 我已经通过控制台手动发送消息测试了 pub/sub 中的主题。该消息显示在我进行的简单订阅中。
main.go
func main() {
ctx := context.Background()
// Sets your Google Cloud Platform project ID.
projectID := "xxx"
// Creates a client.
// client, err := pubsub.NewClient(ctx, projectID)
_, err := pubsub.NewClient(ctx, projectID)
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
b, err := ioutil.ReadFile("credentials.json")
if err != nil {
log.Fatalf("Unable to read client secret file: %v", err)
}
// If modifying these scopes, delete your previously saved token.json.
config, err := google.ConfigFromJSON(b, gmail.GmailReadonlyScope)
if err != nil {
log.Fatalf("Unable to parse client secret file to config: %v", err)
}
gmailClient := getClient(config)
svc, err := gmail.New(gmailClient)
if err != nil {
log.Fatalf("Unable to retrieve Gmail client: %v", err)
}
var watchRequest gmail.WatchRequest
watchRequest.TopicName = "projects/xxx/topics/gmail"
svc.Users.Watch("xxx@gmail.com", &watchRequest)
...
脚本运行正常,尽管没有标准输出来确认 Watch 服务 运行。
但是,通过上述设置,我从 xxx@gmail.com
发送了一封邮件给它自己,但是邮件没有显示在我的 topic/subscription.
中
我还必须做什么才能使用 Go 通过 pub/sub 启用 Gmail 推送通知?
确保您在 UsersWatchCall 上调用 Do
方法。 svc.Users.Watch
returns 只有结构,不会立即调用。
看起来像这样:
res, err := svc.Users.Watch("xxx@gmail.com", &watchRequest).Do()
if err != nil {
// don't forget to check the error
}
我希望对我的应用程序进行基本设置,以便在每次邮件到达 Gmail 收件箱时它都能收到通知。
我正在按照指南进行操作 here。
- 我创建了主题和订阅。
- 我的凭据有效。我可以使用凭证和 Go 脚本检索我的电子邮件,如图 here.
- 作为 Pub/Sub 发布者,我已与
gmail-api-push@system.gserviceaccount.com
启用了对我的主题gmail
的权限。 - 我已经通过控制台手动发送消息测试了 pub/sub 中的主题。该消息显示在我进行的简单订阅中。
main.go
func main() {
ctx := context.Background()
// Sets your Google Cloud Platform project ID.
projectID := "xxx"
// Creates a client.
// client, err := pubsub.NewClient(ctx, projectID)
_, err := pubsub.NewClient(ctx, projectID)
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
b, err := ioutil.ReadFile("credentials.json")
if err != nil {
log.Fatalf("Unable to read client secret file: %v", err)
}
// If modifying these scopes, delete your previously saved token.json.
config, err := google.ConfigFromJSON(b, gmail.GmailReadonlyScope)
if err != nil {
log.Fatalf("Unable to parse client secret file to config: %v", err)
}
gmailClient := getClient(config)
svc, err := gmail.New(gmailClient)
if err != nil {
log.Fatalf("Unable to retrieve Gmail client: %v", err)
}
var watchRequest gmail.WatchRequest
watchRequest.TopicName = "projects/xxx/topics/gmail"
svc.Users.Watch("xxx@gmail.com", &watchRequest)
...
脚本运行正常,尽管没有标准输出来确认 Watch 服务 运行。
但是,通过上述设置,我从 xxx@gmail.com
发送了一封邮件给它自己,但是邮件没有显示在我的 topic/subscription.
我还必须做什么才能使用 Go 通过 pub/sub 启用 Gmail 推送通知?
确保您在 UsersWatchCall 上调用 Do
方法。 svc.Users.Watch
returns 只有结构,不会立即调用。
看起来像这样:
res, err := svc.Users.Watch("xxx@gmail.com", &watchRequest).Do()
if err != nil {
// don't forget to check the error
}