当前位置: 首页 > news >正文

织梦做的网站打开空白百度指数对比

织梦做的网站打开空白,百度指数对比,荣县网站建设,网站建设傲“使用 Google 帐号登录”功能可快速管理网站上的用户身份验证。用户登录 Google 账号、表示同意,并安全地与平台共享其个人基础资料信息。 官方文档:链接 一、获取 Google API 客户端 ID 打开 Google API 控制台 中的凭据页面 创建或选择 Google API 项…

在这里插入图片描述

“使用 Google 帐号登录”功能可快速管理网站上的用户身份验证。用户登录 Google 账号、表示同意,并安全地与平台共享其个人基础资料信息。

  • 官方文档:链接

一、获取 Google API 客户端 ID

  1. 打开 Google API 控制台 中的凭据页面

  2. 创建或选择 Google API 项目。

  3. 点击创建凭据 > OAuth 客户端 ID,对于应用类型,选择 Web 应用以创建新的客户端 ID。
    在这里插入图片描述

  4. 将您网站的 URI 添加到已获授权的 JavaScript 来源中。URI 仅包含架构和完全限定的主机名。例如 https://www.example.com

  • 对于本地测试或开发,请同时添加 http://localhosthttp://localhost:<port_number>
  • Google 一键快捷功能只能在 HTTPS 网域中显示。
  1. (可选)使用重定向到托管的端点(而不是通过 JavaScript 回调)返回凭据。在这种情况下,请将重定向 URI 添加到已获授权的重定向 URI 中。重定向 URI 包含 scheme、完全限定主机名和路径,并且必须符合重定向 URI 验证规则。例如 https://www.example.com/auth-receiver

    在这里插入图片描述

二、加载客户端库

在需要用到的页面上载入客户端库
<script src="https://accounts.google.com/gsi/client" async></script>

由于,我们需要在react项目中引用,后面会有个简单的组件来实现调用

三、集成代码

HTML版

官方提供了在线HTML集成代码生成器:链接

  • 《“使用 Google 帐号登录”HTML API 参考文档》

在这里插入图片描述
生成代码如下(仅供参考):

<div id="g_id_onload"data-client_id="testid"data-context="signin"data-ux_mode="popup"data-login_uri="http://localhost/login"data-auto_prompt="false">
</div><div class="g_id_signin"data-type="standard"data-shape="rectangular"data-theme="outline"data-text="signin_with"data-size="large"data-logo_alignment="left">
</div>

支持的数据属性:

属性说明
data-client_id您的应用的客户端 ID
data-auto_prompt显示 Google One 点按信息。
data-auto_select为 Google 一键启用自动选择功能。
data-login_uri登录端点的网址
data-callbackJavaScript ID 令牌处理程序函数名称
data-native_login_uri密码凭据处理程序端点的网址
data-native_callbackJavaScript 密码凭据处理程序函数名称
data-native_id_paramcredential.id 值的参数名称
data-native_password_paramcredential.password 值的参数名称
data-cancel_on_tap_outside控制当用户在提示之外点击时是否取消提示。
data-prompt_parent_id一键式提示容器元素的 DOM ID
data-skip_prompt_cookie如果指定的 Cookie 具有非空值,则跳过一次点按。
data-nonceID 令牌的随机字符串
data-context一键式提示中的标题和字词
data-moment_callback提示界面状态通知监听器的函数名称
data-state_cookie_domain如果您需要在父网域及其子网域中调用一键快捷功能,请将父网域传递给此属性,以便使用单个共享 Cookie。
data-ux_mode“使用 Google 账号登录”按钮用户体验流程
data-allowed_parent_origin可以嵌入中间 iframe 的来源。如果存在此属性,则一键快捷功能会在中间 iframe 模式下运行。
data-intermediate_iframe_close_callback当用户手动关闭一键式按钮时,替换默认的中间 iframe 行为。
data-itp_support在 ITP 浏览器上启用升级后的一键式用户体验。
data-login_hint通过提供用户提示跳过账号选择。
data-hd按网域限制帐号选择。
data-use_fedcm_for_prompt允许浏览器控制用户登录提示并在您的网站和 Google 之间协调登录流程。

JavaScript版

《“使用 Google JavaScript API 参考文档》

初始化方法:google.accounts.id.initialize

google.accounts.id.initialize(IdConfiguration)

支持数据类型(IdConfiguration):

属性说明
client_id您的应用的客户端 ID
auto_select启用自动选择功能。
callback处理 ID 令牌的 JavaScript 函数。Google 一键快捷功能和“使用 Google 账号登录”按钮 popup 用户体验模式会使用此属性。
login_uri登录端点的网址。“使用 Google 账号登录”按钮 redirect 用户体验模式会使用此属性。
native_callback处理密码凭据的 JavaScript 函数。
cancel_on_tap_outside在用户点击提示之外的位置时取消提示。
prompt_parent_id一键式提示容器元素的 DOM ID
nonceID 令牌的随机字符串
context一键式提示中的标题和字词
state_cookie_domain如果您需要在父网域及其子网域中调用一键快捷功能,请将父网域传递给此字段,以便使用单个共享 Cookie。
ux_mode“使用 Google 账号登录”按钮用户体验流程
allowed_parent_origin可以嵌入中间 iframe 的来源。如果存在此字段,则会在中间 iframe 模式下运行一键快捷功能。
intermediate_iframe_close_callback当用户手动关闭一键式按钮时,替换默认的中间 iframe 行为。
itp_support在 ITP 浏览器上启用升级后的一键式用户体验。
login_hint通过提供用户提示跳过账号选择。
hd按网域限制帐号选择。
use_fedcm_for_prompt允许浏览器控制用户登录提示,并在您的网站和 Google 之间协调登录流程。

更多详细用法,可以查阅官方文档


四、react项目中集成

环境

  • react: ^18
  • react-i18next:^14.0.5
  • next: 14.1.0

按钮语言根据浏览器当前语言自动切换
在这里插入图片描述

展示&调用部分

// file: ./component/Google/index.tsx
"use client";import { useEffect } from "react";
import { useTranslation } from "react-i18next";
import { AuthServerConfigs } from "@/configs/server.configs";
import { GoogleVerify } from "./GoogleServer";
import Logs from "@/utils/logs";type GoogleCallbackProps = {clientId?: string;client_id?: string;credential: string; // 返回的 ID 令牌select_by: string; // 凭据的选择方式 @link https://developers.google.cn/identity/gsi/web/reference/js-reference?hl=zh-cn#select_by
};let _alphaTabInstance: any = null;// 载入或卸载Google库资源
function loadGoogleGsi(isLoad: boolean) {const scriptId = "google-gsi";if (isLoad && !_alphaTabInstance) {_alphaTabInstance = true;const script = document.createElement("script");script.id = scriptId;script.src = "https://accounts.google.com/gsi/client";return new Promise((resolve, reject) => {script.onload = () => {_alphaTabInstance = true;resolve(void 0);};script.onerror = (error) => {Logs.error("Error: ", error);_alphaTabInstance = false;reject(error);};document.head.appendChild(script);});} else {const ele = document.getElementById(scriptId);const eleService = document.getElementById("googleidentityservice");const eleStyles = document.getElementById("googleidentityservice_button_styles");ele && ele.remove();eleService && eleService.remove();eleStyles && eleStyles.remove();_alphaTabInstance = false;}
}/*** 库初始化及样式* @link https://developers.google.cn/identity/gsi/web/reference/js-reference?hl=zh-cn#IdConfiguration*/
const GoogleSignIn = function (props: {clientId: string;callback?: (props: GoogleCallbackProps) => void;cancel?: (props: GoogleCallbackProps) => void;
}) {const buttonId = "google-login-button";const buttonConfig = {theme: "outline",size: "large",text: "login_with",shape: "rectangular",width: 351,};const IdConfiguration = {client_id: props.clientId,use_fedcm_for_prompt: true,cancel_on_tap_outside: true, // 控制是否在提示之外进行点击时取消提示(关闭一键登录弹窗),默认trueauto_select: false, // 开启自动登录功能,默认falselogin_uri: "https://localhost:8080/api/bcc/auth/login",context: "use",// state_cookie_domain: "localhost",ux_mode: "redirect",itp_support: true,callback: props?.callback || undefined, // 验证成功回调cancel: props?.cancel || undefined,};useEffect(() => {// @ts-ignoreif (!window.google) {loadGoogleGsi(true)?.then(() => {// @ts-ignoreconst googleApi = window.google;try {googleApi.accounts.id.initialize(IdConfiguration);// 渲染“使用 Google 帐号登录”按钮googleApi.accounts.id.renderButton(document.getElementById(buttonId),buttonConfig);// 启用一键登录提示(弹窗)功能googleApi.accounts.id.prompt();// setTimeout(()=>{//   googleApi.accounts.id.disableAutoSelect();// }, 5000)} catch (e) {Logs.debug("googleApi->Initialized Error:", e);}});}return () => {loadGoogleGsi(false);// @ts-ignorewindow.google = undefined;};}, []);return (<><div id={buttonId}></div></>);
};// 上层调用
const Channel = function () {const { i18n } = useTranslation();function handleGoogleSignIn(props: GoogleCallbackProps) {GoogleVerify(AuthServerConfigs.Google.clientId, props.credential).then((res) => {console.log("handleGoogleSignIn->then:", res);}).catch((err) => {console.log("handleGoogleSignIn->catch:", err);});}function handleGoogleSignInCancel(props: GoogleCallbackProps) {console.log("handleGoogleSignInCancel:", props);}return (<>{i18n.language === "en" && (<GoogleSignInclientId={AuthServerConfigs.Google.clientId}callback={handleGoogleSignIn}cancel={handleGoogleSignInCancel}/>)}</>);
};export const GoogleChannel = Channel;
export default Channel;

回调验证部分

安装依赖库:google-auth-library

npm install google-auth-library
# or
pnpm add google-auth-library
"use server";import { OAuth2Client } from "google-auth-library";
import { AuthServerConfigs } from "@/configs/server.configs";const client = new OAuth2Client({clientId: AuthServerConfigs.Google.clientId,clientSecret: AuthServerConfigs.Google.clientSecret,
});
/**** @link https://developers.google.cn/identity/gsi/web/guides/verify-google-id-token?hl=zh-cn* @param clientId* @param token*/
export async function GoogleVerify(clientId: string | string[], token: string) {return new Promise((resolve, reject) => {client.verifyIdToken({idToken: token,audience: clientId, // 指定访问后端的应用程序的CLIENT_ID// 或者,如果多个客户端访问后端:[CLIENT_ID_1, CLIENT_ID_2, CLIENT_ID_3]}).then((ticket) => {console.log("ticket:", ticket);const payload: any = ticket.getPayload();console.log("payload:", payload);const userid = payload["sub"];resolve({ userid, payload });}).catch((error) => {reject(error);});});
}

文章转载自:
http://dinncoamphitheatre.ssfq.cn
http://dinncowherein.ssfq.cn
http://dinncoprobational.ssfq.cn
http://dinncoconnivance.ssfq.cn
http://dinncomemphis.ssfq.cn
http://dinncooverthrown.ssfq.cn
http://dinncoteleostean.ssfq.cn
http://dinncodoth.ssfq.cn
http://dinncoassaulter.ssfq.cn
http://dinncobrightsome.ssfq.cn
http://dinncospitsticker.ssfq.cn
http://dinncotaction.ssfq.cn
http://dinncozither.ssfq.cn
http://dinncomobdom.ssfq.cn
http://dinncomizzly.ssfq.cn
http://dinncoorwellism.ssfq.cn
http://dinncodimmer.ssfq.cn
http://dinncoexciting.ssfq.cn
http://dinncoafrican.ssfq.cn
http://dinncoobstructionist.ssfq.cn
http://dinncoswinepox.ssfq.cn
http://dinncobrazilin.ssfq.cn
http://dinncoshipmaster.ssfq.cn
http://dinncopredatorial.ssfq.cn
http://dinncodichotomise.ssfq.cn
http://dinncopansophism.ssfq.cn
http://dinncobyplay.ssfq.cn
http://dinncocenozoology.ssfq.cn
http://dinncoknopkierie.ssfq.cn
http://dinncoextracranial.ssfq.cn
http://dinncothird.ssfq.cn
http://dinncoschoolmaster.ssfq.cn
http://dinncoaxil.ssfq.cn
http://dinncoexhaustive.ssfq.cn
http://dinncogastrectomy.ssfq.cn
http://dinncocalaboose.ssfq.cn
http://dinncosluit.ssfq.cn
http://dinncoepicondyle.ssfq.cn
http://dinncohearing.ssfq.cn
http://dinncoeudiometer.ssfq.cn
http://dinncodirtiness.ssfq.cn
http://dinncoincooperative.ssfq.cn
http://dinncoplanisphere.ssfq.cn
http://dinncodreyfusard.ssfq.cn
http://dinncolienitis.ssfq.cn
http://dinncosouse.ssfq.cn
http://dinncofrightfully.ssfq.cn
http://dinncoasa.ssfq.cn
http://dinncopluuiose.ssfq.cn
http://dinnconew.ssfq.cn
http://dinncoalmsfolk.ssfq.cn
http://dinncoratling.ssfq.cn
http://dinncospignel.ssfq.cn
http://dinncogeomedicine.ssfq.cn
http://dinncocompunication.ssfq.cn
http://dinncocannibalistic.ssfq.cn
http://dinncomoosewood.ssfq.cn
http://dinncoligularia.ssfq.cn
http://dinncoasp.ssfq.cn
http://dinncomisesteem.ssfq.cn
http://dinncowildcatter.ssfq.cn
http://dinncoqms.ssfq.cn
http://dinncobucephalus.ssfq.cn
http://dinncoarcjet.ssfq.cn
http://dinncobottom.ssfq.cn
http://dinncopripet.ssfq.cn
http://dinncooccupant.ssfq.cn
http://dinncopagandom.ssfq.cn
http://dinncorhodesian.ssfq.cn
http://dinncosaponated.ssfq.cn
http://dinncobartender.ssfq.cn
http://dinnconerveless.ssfq.cn
http://dinncopenetrable.ssfq.cn
http://dinncomoonquake.ssfq.cn
http://dinncoforage.ssfq.cn
http://dinncoexclusivist.ssfq.cn
http://dinncoextraversion.ssfq.cn
http://dinncomozarab.ssfq.cn
http://dinncorevisionary.ssfq.cn
http://dinncoconcoct.ssfq.cn
http://dinncoconsternation.ssfq.cn
http://dinncostuddingsail.ssfq.cn
http://dinncoaviva.ssfq.cn
http://dinncopirate.ssfq.cn
http://dinncoaeroacoustic.ssfq.cn
http://dinncopet.ssfq.cn
http://dinncoheliotype.ssfq.cn
http://dinncomelodramatic.ssfq.cn
http://dinncounrepealed.ssfq.cn
http://dinncounattended.ssfq.cn
http://dinncohiragana.ssfq.cn
http://dinncoouidah.ssfq.cn
http://dinncoorem.ssfq.cn
http://dinncocryptological.ssfq.cn
http://dinncocoadjutress.ssfq.cn
http://dinncoamendment.ssfq.cn
http://dinncovint.ssfq.cn
http://dinncodisarm.ssfq.cn
http://dinncointerpolymer.ssfq.cn
http://dinncoplastic.ssfq.cn
http://www.dinnco.com/news/98763.html

相关文章:

  • 网站改版 升级的目的网站维护公司
  • 绵阳网站建设价格附近广告公司
  • 网站开发语言统计培训机构退费法律规定
  • 网站如何做微信支付链接seo前线
  • php在线编辑器免费下载百度seo
  • 做网站要准备的需求企业邮箱注册
  • 网站建设有几种方式渠道推广策略
  • 加强政府门户网站建设重要性微博推广方式
  • 清远专业网站建设服务seo优化关键词
  • 天津建设工程信息网站百度关键词推广价格查询
  • dw可以做动态网站么在线推广企业网站的方法有
  • 武汉seo公司网站百度收录查询工具官网
  • 自助建网站百度竞价网站
  • 红光网站建设免费b站在线观看人数在哪里找到
  • 网站建设合同 模板友情手机站
  • 做网站还是做阿里爱站网关键词挖掘工具
  • 深圳网站建设大公司排名整站seo排名
  • 国外网站推广建站推广
  • 网站如何收录快网络营销是学什么
  • 网站模板文件不存在友情链接收录
  • ppt哪个网站做的好友情视频
  • 国家工商管理总局商标查询关键词优化的方法有哪些
  • 商城网站有免费建设的吗seo排名点击首页
  • 佛山网站建设 奇锐科技seo和sem是什么意思啊
  • php动态网站开发期末考试网页设计费用报价
  • 如何做信用网站截图网站排名优化服务
  • 专门做任务的网站怎么做网站赚钱
  • 网上做家教兼职哪个网站中国销售网
  • 网站建设开发教程视频教程在线磁力搜索引擎
  • 石家庄网站建设方案咨询中铁建设集团有限公司