如何使用OAuth在Chrome扩展中获取唯一的用户ID? [英] How to get the unique user ID in a chrome extension using OAuth?
问题描述
我在清单文件中使用标识
权限,在我的JavaScript代码中使用 chrome.identity.getAuthToken({'interactive':真正的},函数(令牌)
来获得使用识别标记。
/ identity#method-getProfileUserInforel =nofollow>
chrome.identity.getProfileUserInfo()
以获得电子邮件地址和唯一ID。需要添加 identity.email
权限(除了 identity
),它不需要OAuth2标记(或即使是网络连接)。 这是一个最小的扩展,它将这些信息输出到后台页面的控制台:
manifest.json
{
manifest_version:2,
名称 : 身份测试,
版本:0.1,
背景:{
脚本:[background.js]
},
permissions:[
identity,
identity.email
]
}
background.js
chrome.identity。 getProfileUserInfo(function(userInfo){
console.log(JSON.stringify(userInfo));
});
您可以通过调用Google来获取用户信息API与OAuth2令牌。我相信你需要设置个人资料
和电子邮件
范围。如果您使用 Google API客户端库,则可以致电 gapi.client.oauth2.userinfo.get()
。这更严格地满足您的问题的字母,它指定使用OAuth并且不喜欢添加权限,但我猜测
I am using the identity
permission in my manifest file and in my JavaScript code I use chrome.identity.getAuthToken({ 'interactive': true }, function( token )
to get the use identification token.
How do I get the unique user ID or e-mail address? Preferrably without adding new permissions.
You can use chrome.identity.getProfileUserInfo()
to get both the email address and a unique id. You will need to add the identity.email
permission (in addition to identity
). It does not require an OAuth2 token (or even a network connection).
Here is a minimal extension that prints this information to the console of a background page:
manifest.json
{
"manifest_version" : 2,
"name" : "Identity Test",
"version" : "0.1",
"background": {
"scripts": ["background.js"]
},
"permissions": [
"identity",
"identity.email"
]
}
background.js
chrome.identity.getProfileUserInfo(function(userInfo) {
console.log(JSON.stringify(userInfo));
});
You could instead get user info by calling the Google API with the OAuth2 token. I believe you will need to set both the profile
and email
scopes. If you use the Google API Client library, you can call gapi.client.oauth2.userinfo.get()
. This more strictly satisfies the letter of your question, which specifies using OAuth and prefers not adding permissions, but I would guess that chrome.identity.getProfileUserInfo()
is what you're really looking for.
这篇关于如何使用OAuth在Chrome扩展中获取唯一的用户ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!