json 如何使用Chrome Identity API为Chrome扩展程序设置用户身份验证

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了json 如何使用Chrome Identity API为Chrome扩展程序设置用户身份验证相关的知识,希望对你有一定的参考价值。

## How to set up user authentication for a Chrome Extension using the Chrome Identity API
1. Create a private key file, from which you can create the manifest key and Application ID, as detailed here:
https://stackoverflow.com/questions/23873623/obtaining-chrome-extension-id-for-development
2. Add the manifest key to "key" in manifest.json
3. Create a new project in Google Developer Console
https://console.developers.google.com/project
4. Go to "APIs & auth > Credentials" and create new client id for a Chrome Application using the Application ID generated in step 3.
5. Copy the Client ID to oauth2.client_id in the manifest.json

## Deprecated?
1. Create a new app on the Chrome Web Store Developer Dashboard
https://chrome.google.com/webstore/developer/dashboard
2. Click on "more info" and copy the public key to "key" in manifest.json. (or not needed because of next step...?)

## Resources
- http://stackoverflow.com/questions/25044936/chrome-identity-user-authentication-in-a-chrome-extension
chrome.identity.getAuthToken({
    interactive: true
}, function(token) {
    if (chrome.runtime.lastError) {
        alert(chrome.runtime.lastError.message);
        return;
    }
    var x = new XMLHttpRequest();
    x.open('GET', 'https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token=' + token);
    x.onload = function() {
        alert(x.response);
    };
    x.send();
});
{
    "key": "<Application ID>",
    "name": "Identity test",
    "version": "1",
    "manifest_version": 2,
    "background": {
        "scripts": ["background.js"]
    },
    "permissions": [
        "identity"
    ],
    "oauth2": {
        "client_id": "<Client ID>",
        "scopes": [
            "https://www.googleapis.com/auth/userinfo.email"
        ]
    }
}

以上是关于json 如何使用Chrome Identity API为Chrome扩展程序设置用户身份验证的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 Oauth2 和 chrome.identity 获取 Facebook 令牌

如何使用 Chrome 扩展程序获取 Laravel 护照令牌?

如何安装chrome扩展,以json-handle为例

如何避免 Chrome 在解析此 JSON 时崩溃?

从Chrome扩展程序访问Google文档

即使用户已经登录,也会从 getAuthToken 屏幕登录 Chrome