php - 将 Google OAuth 授权令牌存储在数据库中
问题描述
我正在构建一个门户,多个用户可以在其中登录到他们的多个 Gmail 帐户。我已成功检索令牌值,但是,我想将其存储在我的数据库中,但我无法存储它。
下面是我正在使用的代码:
function mInititalize(){
$client = new Google_Client();
$client->addScope('https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/userinfo.email https://mail.google.com/');
$client->setClientId(Config('gmail.client_id'));
$client->setClientSecret(Config('gmail.client_secret'));
$client->setRedirectUri('http://localhost:81'.Config('gmail.redirect_url'));
$loginURL = $client->createAuthUrl();
return redirect($loginURL);
}
重定向或用户登录后
function mGetToken(){
$token = $client->fetchAccessTokenWithAuthCode( 'code'); // here i get the 'code' from login URL
I pass this code to get token I successfully get token
$oAuth = new Google_Service_Oauth2( $client);
$userData = $oAuth->userinfo_v2_me->get(); // get current user detail
}
我想将$token
值存储在数据库中,但收到错误消息
>Serialization of 'Closure' is not allowed
请任何人帮我解决这个问题。谢谢。
解决方案
我建议存储 Google API 的 OAuth 凭据信息,而不是在您的数据库中,而是通过 API 本身。如果您打算以任何身份验证方式使用它,则会遇到问题,如文档所述:
访问令牌会定期过期并成为相关 API 请求的无效凭证。 Google Identity Platform:将 OAuth 2.0 用于 Web 服务器应用程序
但是,相同的文档还显示了一种可以在 API 中本地设置或检索令牌的方法。由于它是与 google 的身份验证过程相关的数据,并且如果您存储它可能会过时,因此最好让他们处理它并使用 API。同一来源:
如果您需要将访问令牌应用于新的 Google_Client 对象(例如,如果您将访问令牌存储在用户会话中),请使用 setAccessToken 方法:
$client->setAccessToken($access_token);
$client->getAccessToken();
推荐阅读
- batch-file - 接受用户输入后脚本未运行第二个函数
- android - SceneForm OpenGLES 3.0 需要或更高版本在 android 模拟器上
- ios - 无法单击列表中的 NavigationLink (SwiftUI)
- javascript - 为什么我需要使用 setState 回调来为依赖于第一个项目的 setState 完成的第二个状态项目设置状态?
- powershell - 是否可以打开文件资源管理器并将所选文件附加到 Outlook 并在 powershell 中发送?
- android - 邀请测试人员使用旧版本的 Crashitycs Beta
- reactjs - 如何修复 nextCreate 不是设置 useMemo 设置身份验证反应路由器和挂钩的功能
- python-3.x - 如何使用 Selenium - Python 从 div 类中获取内容?
- r - 有没有办法在具有相同分类 X 变量的同一个图中绘制多个分类 Y 变量?
- ios - SwiftUI:拖动逻辑错误还是这是一个错误?