Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 我应该如何使用Google oAuth 2.0?_Javascript_Php_Jquery_Oauth - Fatal编程技术网

Javascript 我应该如何使用Google oAuth 2.0?

Javascript 我应该如何使用Google oAuth 2.0?,javascript,php,jquery,oauth,Javascript,Php,Jquery,Oauth,我试图实现的是允许任何人登录到该网站,并且只允许某些具有权限的注册用户启动“点击此按钮…” 问题是,我非常不确定如何验证用户是否已登录并具有访问按钮的合法令牌。我是否应该将令牌存储在客户端浏览器上,并在按下按钮时将其推送到服务器,这样安全吗 在这种情况下我应该使用PHP吗 我的主页: Javascript和AJAX: 我对文档也很模糊: 如果有人能启发我的道路并消除我的沮丧,我会非常高兴。是的,这是您链接的文档中推荐的方法 由于您使用的是PHP,因此将使用验证令牌。下面是一个简单的例子: /

我试图实现的是允许任何人登录到该网站,并且只允许某些具有权限的注册用户启动“点击此按钮…”

问题是,我非常不确定如何验证用户是否已登录并具有访问按钮的合法令牌。我是否应该将令牌存储在客户端浏览器上,并在按下按钮时将其推送到服务器,这样安全吗

在这种情况下我应该使用PHP吗

我的主页:

Javascript和AJAX:

我对文档也很模糊:


如果有人能启发我的道路并消除我的沮丧,我会非常高兴。

是的,这是您链接的文档中推荐的方法

由于您使用的是PHP,因此将使用验证令牌。下面是一个简单的例子:

/* Create the client, and add your credentials */
$client = new Google_Client();
$client->setClientId('your_client_id');
$client->setClientSecret('your_client_secret');

/* Verify the token you received */
$ticket = $client->verifyIdToken($_POST['token']);

/* Get the response details */
$data = $ticket->getAttributes();

/* Retrieve user's unique Google ID */
echo json_encode(['user_id' => $data['payload']['sub']]);
/* Create the client, and add your credentials */
$client = new Google_Client();
$client->setClientId('your_client_id');
$client->setClientSecret('your_client_secret');

/* Verify the token you received */
$ticket = $client->verifyIdToken($_POST['token']);

/* Get the response details */
$data = $ticket->getAttributes();

/* Retrieve user's unique Google ID */
echo json_encode(['user_id' => $data['payload']['sub']]);