注意:首先要确保用户电脑和网站服务器都能访问谷歌。
先登录谷歌凭据API管理平台
https://console.cloud.google.com/apis/credentials
选择【凭据】->【创建凭据】
[适用于来自浏览器的请求] 这里增加一个URL,是自己的域名。
[适用于来自 Web 服务器的请求] 这里增加一个URL, 是本代码所在的路径。
session_start();
//谷歌API ID
$googleClientID = 'xxxx.apps.googleusercontent.com';
$googleClientSecret = '谷歌秘钥';
//获取code的接口
$authorizeURL = 'https://accounts.google.com/o/oauth2/v2/auth';
//获取token的接口
$tokenURL = 'https://www.googleapis.com/oauth2/v4/token';
//
$baseURL = 'https://我的域名/本文件.php' ;
//如果没有code参数,则进入code请求
if(!isset($_GET['code'])) {
//生成一个随机的字符作为state
$state = md5(time() . rand(1000, 9999));
$_SESSION['state'] = $state