reactjs - 如何在 Chrome 扩展程序中启用“使用 Google 登录”?

标签 reactjs google-chrome google-cloud-platform google-chrome-extension google-signin

我已经浏览过这份文档

https://cloud.google.com/identity-platform/docs/web/chrome-extension

但是,我不知道如何将扩展程序链接添加到授权域。

这是我的ma​​nifest.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')
      })
    }
  }
})

关于reactjs - 如何在 Chrome 扩展程序中启用“使用 Google 登录”?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66271836/

相关文章:

javascript - 为什么 setTimeout 返回数字而不是文本? ReactJS

javascript - 当URL包含#key-1时跳转到name=key-1

html - Chrome 或 FF 等同于 IE Debugger 的 Css Changes Summary?

google-cloud-platform - 如何授予 GCP 服务帐户 storage.buckets.list 只读访问权限?

javascript - 如何运行后台 Cloud Function 并安排数据库更改?

google-cloud-platform - 如何使用Google Cloud TPU训练keras模型

javascript - 使用带有 CSS 模块的 ReactJS 对 SVG 元素进行样式化

javascript - 有没有办法记住从参数传递的函数 - (useCallback) exhaustive-deps

node.js - "ER_WRONG_VALUE_COUNT_ON_ROW"

javascript - 如何从 Chrome 扩展调用 OnBlur 方法