登录谷歌邮箱开发者
https://console.developers.google.com/
-
先创建项目
我们用的web应用
-
设置回调
核心主要:
1.创建应用
2.创建客户端ID
3.设置域名和重定向URL
4.对外公开,这样所有的gmail邮箱 都能参与测试
PHP代码实现
- 引入第三方包
https://github.com/googleapis/google-api-php-client/releases
- 获取第三方跳转链接接口
public function googleUrl()
{
global $db,$request,$comId;
//引入第三方包
require_once 'inc/google_client/vendor/autoload.php';
$googleConfig = array(
"client_id"=>"客户端ID",
"project_id"=>"hudong",//项目名称
"auth_uri"=>"https://accounts.google.com/o/oauth2/auth",
"token_uri"=>"https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url"=>"https://www.googleapis.com/oauth2/v1/certs",
"client_secret"=>"客户端秘钥",
"redirect_uris"=>["https://vip.com/Google"],//设置的回调结合 可以设置多个
"javascript_origins"=>["https://vip.com"]//设置的获取JavaScript的来源配置
);
$clientID = $googleConfig['client_id'];
$clientSecret = $googleConfig['client_secret'];
$redirectUri = $googleConfig['callBack'] = "https://vip.com/Google"; //当前项目回调地址
// create Client Request to access Google API
$client = new \Google_Client();
$client->setClientId($clientID);
$client->setClientSecret($clientSecret);
$client->setRedirectUri($redirectUri);
$client->addScope("email");
$client->addScope("profile");
// authenticate code from Google OAuth Flow
$loginUrl = $client->createAuthUrl();
$return['code'] = 1;
$return['message'] = '获取成功';
$return['data'] = array();
$return['data']['url'] = $loginUrl;
return json_encode($return,JSON_UNESCAPED_UNICODE);
}
- 跳转之后 ,前端获取code 传给后台,读取邮箱相关信息
public function googleLogin()
{
global $db,$request,$comId;
require_once 'inc/google_client/vendor/autoload.php';
// echo ABSPATH.'inc/google_client/vendor/autoload.php'; die;
// require '../lib/internal/google-api-php-client--PHP7.0/vendor/autoload.php';
// $googleConfig = $this->snsaccount->googleAccount();
$googleConfig = array(
"client_id"=>"客户端ID",
"project_id"=>"hudong",//项目名称
"auth_uri"=>"https://accounts.google.com/o/oauth2/auth",
"token_uri"=>"https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url"=>"https://www.googleapis.com/oauth2/v1/certs",
"client_secret"=>"客户端秘钥",
"redirect_uris"=>["https://vip.com/Google"],//设置的回调结合 可以设置多个
"javascript_origins"=>["https://vip.com"]//设置的获取JavaScript的来源配置
);
$clientID = $googleConfig['client_id'];
$clientSecret = $googleConfig['client_secret'];
$redirectUri = $googleConfig['callBack'] = "https://tocmodels.com/Google"; //Google console redirect URI
$client = new \Google_Client();
$client->setClientId($clientID);
$client->setClientSecret($clientSecret);
$client->setRedirectUri($redirectUri);
$client->addScope("email");
$client->addScope("profile");
$code = $request['code'];
if(empty($code)) return '{"code":0,"message":"code为必传参数"}';
$token = $client->fetchAccessTokenWithAuthCode($request['code']);
// var_dump($token);
if(isset($token['error'])) return '{"code":0,"message":"'.$token['error'].'"}';
$client->setAccessToken($token['access_token']);
// get profile info
$google_oauth = new \Google_Service_Oauth2($client);
$google_account_info = $google_oauth->userinfo->get();
$email = $google_account_info->email;//谷歌邮箱
$gid = $google_account_info->id;//谷歌id
$gname = $google_account_info->name;//谷歌实名
$image = $google_account_info->picture;//谷歌头像
}
private function httpGet($url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 500);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_URL, $url);
$res = curl_exec($curl);
curl_close($curl);
return $res;
}