ale*_*dar 5 javascript google-api
有许多资源和堆栈溢出问题与我要问的相似但不完全相同.我将在这里重新讨论一些解决方案并解释它们.
我有一位已登录Google的用户.登录后我的意思是手动登录并且存在cookie.我的应用程序未登录.
我只需要获取电子邮件地址.
有三种方法可以做到这一点我见过,但对我来说都不适用.
方式#1:
auth2 = gapi.auth2.getAuthInstance();
if (auth2.isSignedIn.get()) {
var profile = auth2.currentUser.get().getBasicProfile();
console.log('ID: ' + profile.getId());
console.log('Full Name: ' + profile.getName());
console.log('Given Name: ' + profile.getGivenName());
console.log('Family Name: ' + profile.getFamilyName());
console.log('Image URL: ' + profile.getImageUrl());
console.log('Email: ' + profile.getEmail());
}
Run Code Online (Sandbox Code Playgroud)
方式#2:
gapi.client.setApiKey(API_KEY);
gapi.client.load("plus", "v1", function() {
gapi.client.plus.people.get({ userId: "me" }).execute(function(resp) {
// Shows profile information
console.log(resp);
});
});
Run Code Online (Sandbox Code Playgroud)
方式#3:
gapi.client.load('oauth2', 'v2', function () {
gapi.client.oauth2.userinfo.get().execute(function (resp) {
// Shows user email
console.log(resp.email);
})
});
Run Code Online (Sandbox Code Playgroud)
对于方式#2和方式#3堆栈溢出说你必须使用令牌而不是api密钥.但是用户已经登录,我没有,也无法获取令牌.
如何获得ALREADY登录用户的EMAIL?
谢谢
小智 6
虽然是一个老问题..这可能会有所帮助..以防万一..
var auth2 = gapi.auth2.getAuthInstance();
var profile = auth2.currentUser.get().getBasicProfile();
console.log(profile.getName());
console.log(profile.getEmail());
Run Code Online (Sandbox Code Playgroud)
该实例可以由 gapi.auth2.getAuthInstance() 或 gapi.auth2.init() 启动。根据用于实例化的内容,您可以使用两者之一来获取配置文件详细信息。
| 归档时间: |
|
| 查看次数: |
1957 次 |
| 最近记录: |