使用 php 设置 firebase v3 自定义身份验证

2022-11-22php开发问题
35

本文介绍了使用 php 设置 firebase v3 自定义身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在尝试按照以下指南使用来自 google 的新 firebase sdk 设置自定义身份验证:https://firebase.google.com/docs/auth/server#use_a_jwt_library
在 samble 代码中它说:

I'm trying to set up custom auth with the new firebase sdk from google following those guidelines : https://firebase.google.com/docs/auth/server#use_a_jwt_library
In the samble code it says :

从 JSON 密钥文件中获取您的服务帐号的电子邮件地址和私钥

Get your service account's email address and private key from the JSON key file

不幸的是,我不知道从哪里获得这个 json 文件.如果我去我的 firebase 控制台 (https://console.firebase.google.com/) 我设法下载了一个 json文件,但它不包含任何电子邮件地址和私钥.

Unfortunately I have not idea where to get this json file. If I go to my firebase console (https://console.firebase.google.com/) I manage to donwload a json file but it does not contain any email adress and private key.

我设法在我的 google 云平台控制台 (http://console.conf) 中找到了一个包含电子邮件地址和私钥的 json 文件.cloud.google.com) 进入API Manager > Credentials"菜单.令人惊讶的是,我的 firebase 应用程序显示在那里.我将电子邮件和密钥复制并粘贴到示例代码中,然后出现此错误:

I managed to find a json file that contains an email adress and a private key in my google cloud platform console (http://console.cloud.google.com) by goind into the "API Manager > Credentials" menu. Surprisingly my firebase app was showed there. I copy and pasted the email and key into the sample code, then I got this error :

警告:openssl_sign():无法将提供的密钥参数强制转换为第 183 行/volume1/web/yeti/vendor/firebase/php-jwt/src/JWT.php 中的私钥致命错误:未捕获的异常DomainException"' 在/volume1/web/yeti/vendor/firebase/php-jwt/src/JWT.php:185 中带有消息 'OpenSSL 无法签署数据' 堆栈跟踪:#0/volume1/web/yeti/vendor/firebase/php-jwt/src/JWT.php(154): FirebaseJWTJWT::sign('eyJ0eXAiOiJKV1Q...', NULL, 'RS256') #1/volume1/web/yeti/jwt.php(21):FirebaseJWTJWT::encode(Array, NULL, 'RS256') #2/volume1/web/yeti/jwt.php(24): create_custom_token('1234', false) #3 {main} 被抛出到/volume1/web/yeti/vendor/firebase/php-jwt/src/JWT.php 第 185 行

Warning: openssl_sign(): supplied key param cannot be coerced into a private key in /volume1/web/yeti/vendor/firebase/php-jwt/src/JWT.php on line 183 Fatal error: Uncaught exception 'DomainException' with message 'OpenSSL unable to sign data' in /volume1/web/yeti/vendor/firebase/php-jwt/src/JWT.php:185 Stack trace: #0 /volume1/web/yeti/vendor/firebase/php-jwt/src/JWT.php(154): FirebaseJWTJWT::sign('eyJ0eXAiOiJKV1Q...', NULL, 'RS256') #1 /volume1/web/yeti/jwt.php(21): FirebaseJWTJWT::encode(Array, NULL, 'RS256') #2 /volume1/web/yeti/jwt.php(24): create_custom_token('1234', false) #3 {main} thrown in /volume1/web/yeti/vendor/firebase/php-jwt/src/JWT.php on line 185

有人知道我做错了什么吗?

Does someone has an idea of what I'm doing wrong ?

谢谢

推荐答案

您找到解决方案了吗?仍然遇到同样的问题!适用于 HS256,不适用于 RS256.这是谷歌云的限制吗?

Did you find the solution ? Still experiencing the same issue ! Works with HS256 and doesn't with RS256. Is it google cloud kind of limitation ?

非常感谢!@dbburgess

Thank you so much ! @dbburgess

问题:使用了错误的密钥和电子邮件.这些应在与 Firebase 项目对应的 GCP 凭据部分中生成.

Problem: Was using the wrong key and email. These should be generated in the Google Cloud credentials section that corresponds to the Firebase project.

解决方案:

  • 转到console.cloud.google.com".
  • 选择相关的 Firebase 项目.
  • 然后是API 管理器"->凭据".
  • 创建凭据"->服务帐户密钥"-> 选择 JSON.
  • 创建的文件将包含所需的private_key"&'client_email'.

填写值:

$service_account_email = "autogeneratedemail@developer.gserviceaccount.com";$private_key = "-----开始私钥----- SoneVeryVeryLongKey= -----结束私钥----- ";$uid = 'UserToUseInFirebaseRules';$is_premium_account = $uid;

$service_account_email = "autogeneratedemail@developer.gserviceaccount.com"; $private_key = "-----BEGIN PRIVATE KEY----- SoneVeryVeryLongKey= -----END PRIVATE KEY----- "; $uid = 'UserToUseInFirebaseRules'; $is_premium_account = $uid;

您不需要更改create_custom_token"函数中的任何内容,也许可以根据您的需要更改到期日期/时间.

You shouldn't need to change anything in the "create_custom_token" function, maybe the expiration date/time according to your needs.

然后调用函数:

create_custom_token($uid, $is_premium_account);

这篇关于使用 php 设置 firebase v3 自定义身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17