如何解决如何在 Chrome 扩展程序中启用使用 Google 登录?
我已经看过这个文件
https://cloud.google.com/identity-platform/docs/web/chrome-extension
这是我的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"
},"content_security_policy": "script-src 'self' 'sha256-kFv4LNofhwVLOIwHReYGCRy3S9dD6iHKsyMST3uabnU='; object-src 'self'","permissions": [
"storage"
]
}
我已经更喜欢 https://stackoverflow.com/a/44987478/12318562 但这对我没有帮助。
解决方法
我遇到了类似的问题,这是我如何让它工作的,
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')
})
}
}
})
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。