简体   繁体   English

Chrome 应用程序,获取登录用户的电子邮件地址

[英]Chrome App, getting logged in user's email address

I have searched tutorials like https://developer.chrome.com/apps/app_identity , but I cannot see how I can retrieve a user's email address logged in chrome.我搜索了像https://developer.chrome.com/apps/app_identity这样的教程,但我看不到如何检索登录 chrome 的用户的电子邮件地址。 I can only get a token for the chrome.identity api https://developer.chrome.com/apps/identity我只能获得 chrome.identity api https://developer.chrome.com/apps/identity的令牌

so the example code for getting the token works所以获取令牌的示例代码有效

chrome.identity.getAuthToken({'interactive': true}, function(token) {
    console.log('user token: ' + token);
});

but from here how do I get the email.但从这里我如何收到电子邮件。

Note: this is for an app not an extension, thus context_script did not work in my manifest from auth example.注意:这是针对应用程序而不是扩展程序,因此 context_script 在我的 auth 示例清单中不起作用。 Also chrome.identity.getProfileUserInfo is for the beta version 37, I tried it also but enail was empty.另外 chrome.identity.getProfileUserInfo 适用于测试版 37,我也尝试过,但 enail 为空。

I am out of options, but i know there is a way to get the email.我别无选择,但我知道有一种方法可以获取电子邮件。

Haha to answer my own question.哈哈回答我自己的问题。

Short answer: you have to just add the scope of email in manifest file简短回答:您只需在清单文件中添加电子邮件的范围

Long answer: I had to use the google plus api.长答案:我不得不使用google plus api。 Google has deprecated OpenID 2.0 and now using Google Plus sign in and api according to https://developers.google.com/accounts/docs/OpenID2 . Google 已弃用 OpenID 2.0,现在根据https://developers.google.com/accounts/docs/OpenID2使用 Google Plus 登录和 api。 So I mentioned above that I was not getting the email address on the call to Google Plus using xhr method = "GET, url='ht tps://www.googleapis.com/plus/v1/people/me'. So the response was only showing basic things like name, gender, image etc, but no email. In order to get the email, you must add an email token in the manifest file as in the example所以我在上面提到,我没有使用 xhr method = "GET, url='ht tps://www.googleapis.com/plus/v1/people/me' 来获取调用 Google Plus 的电子邮件地址。所以响应仅显示姓名、性别、图像等基本信息,但没有显示电子邮件。为了获取电子邮件,您必须像示例一样在清单文件中添加电子邮件令牌

  "permissions": ["identity",
            ...,
            ],
    "oauth2": {
    // client_id below is specifc to the application key. Follow the
            // documentation to obtain one for your app.
            "client_id": xyz,
            "scopes": ["https://www.googleapis.com/auth/plus.login",
                       "https://www.googleapis.com/auth/userinfo.email"] **<-this one**
    }
    ....

Now in your response when ypou call xhr method = "GET, url='htt ps://www.googleapis.com/plus/v1/people/me', you will get basic user info plus the email现在,当 ypou call xhr method = "GET, url='htt ps://www.googleapis.com/plus/v1/people/me' 时,在您的响应中,您将获得基本用户信息和电子邮件

As an update, here it is a simpler way to get the email:作为更新,这里有一种更简单的方式来获取电子邮件:

  1. you have to add both "identity" and "identity.email" to the permissions section on the manifest file.您必须将“身份”和“身份.email”添加到清单文件的权限部分。

  2. with that, using getProfileUserInfo should do the trick:有了这个,使用getProfileUserInfo应该可以解决问题:

     chrome.identity.getAuthToken({'interactive': true}, function(token) { chrome.identity.getProfileUserInfo( function(info){console.log(info.email)} ); }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM