我的扩展程序可以从Google Chrome获取用户的电子邮件地址吗?

Sha*_*mer 1 google-chrome-extension

我有一个扩展,一些用户贡献金钱或努力.我希望能够通过他们的电子邮件地址识别这些用户,因此我可以显示"谢谢!" 下次他们加载扩展.

有没有办法从谷歌浏览器获取用户的电子邮件地址?

kya*_*all 9

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

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

但并非所有这些都在那里得到解释.所以:

首先,您需要API客户端ID和客户端密钥,以便用户可以授权您使用其详细信息.以下步骤将以一种适用于chrome扩展的方式设置:

  1. Google API控制台
  2. 创建一个API
  3. 转到API Access
  4. 创建客户端ID
  5. 为其命名和徽标,当要求用户授权您的应用程序检索其详细信息时,将显示此名称和徽标.下一个
  6. 选择Web应用程序
  7. 单击"更多"以获取URI和授权的javascript源
  8. 将重定向URI设置为http://www.google.com/robots.txt
  9. 删除javascript originins字段,以便没有
  10. 单击"确定"或其他任何内容,将客户端ID和客户端密钥复制出来以供日后使用.

其次你需要oauth2 javascript库,可以在这里找到:https: //github.com/borismus/oauth2-extensions/tree/master/lib

您需要将它放在名为oauth2的文件夹中,与html文件位于同一目录中.

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

<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>
Run Code Online (Sandbox Code Playgroud)

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

"权限":["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"   }
Run Code Online (Sandbox Code Playgroud)

这应该照顾它.