我的分机可以从Google Chrome获取用户的电子邮件地址吗? [英] Can my extension get a user's e-mail address from Google Chrome?

查看:251
本文介绍了我的分机可以从Google Chrome获取用户的电子邮件地址吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些扩展功能,有些用户会为此付出金钱或努力。我希望能够通过他们的电子邮件地址识别这些用户,所以我可以显示谢谢你!下一次他们加载扩展名。



有没有办法从Google Chrome获取用户的电子邮件地址?

解决方案

好的,让她解决了。这个网站基本上描述它:



http://smus.com/ oauth2-chrome-extensions /



但并非所有解释都在这里解释。因此:

首先,您需要一个API客户端ID和客户端密码,以便用户可以授权您使用他们的详细信息。以下步骤将以可扩展的Chrome浏览器的方式进行设置:


  1. Google API控制台

  2. 创建API

  3. 转到API访问权限

  4. 创建客户端ID
  5. 名称和徽标,当用户被要求获得应用程序许可以检索其详细信息时,将显示此名称和徽标。下一步
  6. 选择Web应用程序

  7. 单击更多获取URI和授权的javascript来源

  8. 设置重定向URI http://www.google.com/robots.txt

  9. 删除javascript origins字段,以便不存在

  10. 单击确定或其他任何内容,将客户端ID和客户端密钥复制以供稍后使用。

其次,您需要oauth2 javascript库,可在此处找到:
https://github.com/borismus/oauth2-extensions/tree/master/lib



您需要将它放在与html文件相同的目录中,名为oauth2的文件夹中。



在你的html文件中添加以下内容:

 < script type =text / javascriptsrc =oauth2 / oauth2.js>< / script> 
< script type =text / javascript>
var googleAuth = new OAuth2('google',{
client_id:'$(您的客户ID)',
client_secret:'$(您的客户机密)',
api_scope:'https://www.googleapis.com/auth/userinfo.email'
});

googleAuth.authorize(function(){
//我们现在应该有googleAuth.getAccessToken()为我们返回一个有效的标记值
//创建一个XMLHttpRequest以获取(xhr.readyState == 4){
if(xhr.status = = 200){
var parseResult = JSON.parse(xhr.responseText);
//电子邮件地址位于naw:
var email = parseResult [email];
}
}
}
//将它打开为GET,POST对我来说不适用于用户信息
xhr.open(GET,https:/ /www.googleapis.com/oauth2/v1/userinfo\",true);
//设置内容和身份验证
xhr.setRequestHeader('Content-Type','application / json');
xhr.setRequestHeader('Authorization',OAuth+ googleAuth.getAccessToken());
xhr.send(null);
//调试东西以便我们可以看到xhr中的所有内容。不要在产品代码中留下
console.log(xhr);
});< / script>

在您的manifest.json中,您需要:

permissions:[https://accounts.google.com/o/oauth2/token,https://www.googleapis.com/oauth2/v1/userinfo]



在清单文件中,您还需要:

 content_scripts:[{
matches:[http://www.google.com/robots.txt*],
js:[oauth2 / oauth2_inject.js],
run_at: document_start}

这应该很好处理它。

I've got an extension for which some users contribute money or effort. I'd like to be able to identify those users by their e-mail addresses, so I can display a "Thank You!" next time they load the extension.

Is there any way to get from Google Chrome the user's e-mail address?

解决方案

Allright, Got her solved. This website basically describes it:

http://smus.com/oauth2-chrome-extensions/

But not all of it is explained there. So:

First of all you need an API Client ID and Client Secret so that you can be authorized by the user to use their details. The following steps will set that up in a fashion that will work with a chrome extension:

  1. Google API Console
  2. Create an API
  3. Go to API Access
  4. Create A Client ID
  5. Give it a name and a Logo, This name and logo will be displayed when the user is asked for permission for your application to retrieve their details. Next
  6. Select Web Application
  7. Click More to get URI and authorized javascript origins
  8. Set the Redirect URI to http://www.google.com/robots.txt
  9. Delete the javascript origins field so that there is none
  10. Click OK or whatever it is, copy the client ID and client secret out for later.

Second you'll need the oauth2 javascript library, which is available here: https://github.com/borismus/oauth2-extensions/tree/master/lib

You'll need to put that in a folder called oauth2 in the same directory as your html file.

In you html file add the following stuff:

<script type="text/javascript" src="oauth2/oauth2.js"></script>
<script type="text/javascript">
var googleAuth = new OAuth2('google', {
  client_id: ' $(YOUR CLIENT ID HERE) ',
  client_secret: ' $(YOUR CLIENT SECRET HERE) ',
  api_scope: 'https://www.googleapis.com/auth/userinfo.email'
});

googleAuth.authorize(function() {
  // We should now have googleAuth.getAccessToken() returning a valid token value for us 
  // Create an XMLHttpRequest to get the email address 
  var xhr = new XMLHttpRequest();
  xhr.onreadystatechange = function() { 
    if( xhr.readyState == 4 ) {
      if( xhr.status == 200 ) { 
        var parseResult = JSON.parse(xhr.responseText);
        // The email address is located naw: 
        var email = parseResult["email"];
      }
    }
  }
  // Open it up as GET, POST didn't work for me for the userinfo 
  xhr.open("GET","https://www.googleapis.com/oauth2/v1/userinfo",true);
  // Set the content & autherization 
  xhr.setRequestHeader('Content-Type', 'application/json');
  xhr.setRequestHeader('Authorization', "OAuth " + googleAuth.getAccessToken() );
  xhr.send(null);
  // Debugging stuff so we can see everything in the xhr.  Do not leave this in production code 
  console.log(xhr);
});</script>

In your manifest.json you need:

"permissions": ["https://accounts.google.com/o/oauth2/token", "https://www.googleapis.com/oauth2/v1/userinfo"]

Also in the manifest file you will need:

"content_scripts": [   {
    "matches": ["http://www.google.com/robots.txt*"],
    "js": ["oauth2/oauth2_inject.js"],
    "run_at": "document_start"   }

That should pretty much take care of it.

这篇关于我的分机可以从Google Chrome获取用户的电子邮件地址吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆