英文:
Google people API not able to fetch emailAddresses
问题
我正在编写一个桌面应用程序,需要对Google用户进行身份验证并获取其电子邮件地址。到目前为止,使用快速入门示例,我能够进行身份验证,但是在获取用户电子邮件地址方面遇到了困难。
config, err := google.ConfigFromJSON(b, people.UserEmailsReadScope)
if err != nil {
log.Fatalf("无法解析客户端密钥文件为配置:%v", err)
}
client := getClient(config)
srv, err := people.NewService(ctx, option.WithHTTPClient(client))
if err != nil {
log.Fatalf("无法创建people客户端:%v", err)
}
me, err := srv.People.Get("people/me").PersonFields("emailAddresses").Do()
if err != nil {
log.Fatalf("无法检索用户:%v", err)
}
我还尝试了UserinfoEmailScope,但没有成功。每次都收到403错误。
googleapi: 错误 403: 调用者没有请求“people/me”的权限。请求需要以下范围之一:[profile]。, forbidden
我已经将所有这些范围添加到项目中了。我做错了什么?
英文:
I am writing a desktop app that needs to authenticate a google user and fetch his email id. So far with the quickstart example, I am able to authenticate but I am having a hard time fetching user email address.
config, err := google.ConfigFromJSON(b, people.UserEmailsReadScope)
if err != nil {
log.Fatalf("Unable to parse client secret file to config: %v", err)
}
client := getClient(config)
srv, err := people.NewService(ctx, option.WithHTTPClient(client))
if err != nil {
log.Fatalf("Unable to create people Client %v", err)
}
me, err := srv.People.Get("people/me").PersonFields("emailAddresses").Do()
if err != nil {
log.Fatalf("Unable to retrieve user. %v", err)
}
I also tried UserinfoEmailScope but no luck. Getting 403 each time.
googleapi: Error 403: The caller does not have permission to request "people/me". Request requires one of the following scopes: [profile]., forbidden
I have added all of these scopes to the project also. What am I doing wrong?
答案1
得分: 1
最后找到了解决方法。这里需要两个范围。
config, err := google.ConfigFromJSON(b, people.UserinfoProfileScope, people.UserinfoEmailScope)
英文:
Finally figured it out. Two scopes are required for this.
config, err := google.ConfigFromJSON(b, people.UserinfoProfileScope, people.UserinfoEmailScope)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论