将 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

请任何人帮我解决这个问题。谢谢。


慕码人2483693
浏览 129回答 1
1回答

萧十郎

我建议存储 Google API 的 OAuth 凭据信息,而不是在您的数据库中,而是通过 API 本身。如果您打算以任何身份验证方式使用它,则会遇到问题,如文档所述:访问令牌会定期过期并成为相关 API 请求的无效凭证。 Google Identity Platform:将 OAuth 2.0 用于 Web 服务器应用程序但是,相同的文档还显示了一种可以在 API 中本地设置或检索令牌的方法。由于它是与 google 的身份验证过程相关的数据,并且如果您存储它可能会过时,因此最好让他们处理它并使用 API。同一来源:如果您需要将访问令牌应用于新的 Google_Client 对象(例如,如果您将访问令牌存储在用户会话中),请使用 setAccessToken 方法:$client->setAccessToken($access_token); $client->getAccessToken();
打开App,查看更多内容
随时随地看视频慕课网APP