Reactjs 如何在Chrome扩展中启用Google登录?
我已经看过这份文件了 但是,我不知道如何将扩展链接添加到授权域 这是我的清单.jsonReactjs 如何在Chrome扩展中启用Google登录?,reactjs,google-chrome,google-cloud-platform,google-chrome-extension,google-login,Reactjs,Google Chrome,Google Cloud Platform,Google Chrome Extension,Google Login,我已经看过这份文件了 但是,我不知道如何将扩展链接添加到授权域 这是我的清单.json { "manifest_version": 2, "name": "AppName", "description": "This official AppName Chrome Extension", "version": "1.0", "
{
"manifest_version": 2,
"name": "AppName",
"description": "This official AppName Chrome Extension",
"version": "1.0",
"browser_action": {
"default_popup": "index.html",
"default_title": "AppName"
},
"icons": {
"16": "icon1.png",
"48": "icon1.png",
"128": "icon1.png"
},
"content_security_policy": "script-src 'self' 'sha256-kFv4LNofhwVLOIwHReYGCRy3S9dD6iHKsyMST3uabnU='; object-src 'self'",
"permissions": [
"storage"
]
}
我已经比较喜欢了,但这对我没有多大帮助。我也遇到过类似的问题,下面是我是如何让它工作的
manifest.json
{
"manifest_version": 2,
"name": "AppName",
"description": "This official AppName Chrome Extension",
"version": "1.0",
"browser_action": {
"default_popup": "index.html",
"default_title": "AppName"
},
"icons": {
"16": "icon1.png",
"48": "icon1.png",
"128": "icon1.png"
},
"background": {
"scripts": [
"./background.js"
],
"persistent": false
},
"oauth2": {
"client_id": "YOUR_CLIENT_ID",
"scopes": [
"openid", "email", "profile"
]
},
"content_security_policy": "script-src 'self' 'sha256-kFv4LNofhwVLOIwHReYGCRy3S9dD6iHKsyMST3uabnU='; object-src 'self'",
"permissions": [
"storage",
"identity",
"*://*.google.com/*"
]
}
background.js
let user_signed_in = false;
const CLIENT_ID = "YOUR_CLIENT_ID"
const RESPONSE_TYPE = encodeURIComponent('token')
const REDIRECT_URI = chrome.identity.getRedirectURL("oauth2");
const STATE = encodeURIComponent('jsksf3')
const SCOPE = encodeURIComponent('openid')
const PROMPT = encodeURIComponent('consent')
function create_oauth() {
let auth_url = `https://accounts.google.com/o/oauth2/v2/auth?`
var auth_params = {
client_id: CLIENT_ID,
redirect_uri: REDIRECT_URI,
response_type: 'token',
scope: "https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile openid",
};
const url = new URLSearchParams(Object.entries(auth_params));
url.toString();
auth_url += url;
return auth_url;
}
function is_user_signedIn() {
return user_signed_in;
}
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.message === 'login') {
if (user_signed_in) {
return;
} else {
chrome.identity.launchWebAuthFlow({
url: create_oauth(),
interactive: true
}, function (redirect_uri) {
sendResponse('success')
})
}
}
})