Thinkphp6 对接google谷歌第三方登录接口

259次阅读
没有评论

共计 2708 个字符,预计需要花费 7 分钟才能阅读完成。

在开发项目当中经常会遇到需要第三方登录的开发,国内的 QQ、微信、微博、支付宝相应的开发接口都很成熟,配置也很简单,今天我要介绍一下谷歌登录接口的开发方式。

1. 首先下载谷歌 SDK
Releases · googleapis/google-api-php-client · GitHub
A PHP client library for accessing Google APIs. Contribute to googleapis/google-api-php-client development by creating an account on GitHub.
https://github.com/googleapis/google-api-php-client/releases

Thinkphp6 对接 google 谷歌第三方登录接口

根据自己的 PHP 版本下载相应的接口程序版本。

2. 获取谷歌应用的 ID 和密钥。

https://console.developers.google.com/ 打开网址

Thinkphp6 对接 google 谷歌第三方登录接口

根据图示,获取授权。

接下来回提示你获取同意屏幕。

Thinkphp6 对接 google 谷歌第三方登录接口

如需创建 OAuth 客户端 ID,您必须先配置同意屏幕 — 下一步选择外部。确定。

 

这里就根据要求去填写就好了。都是中文,能看懂。

完成以后继续获取授权 ID。然后复制授权 ID 和密钥,配置到你的后台,或者配置文件。

第三步,写登录和回调文件

登录文件 googleindex.php

// 包含配置信息
$data = cache(“config”, true); // 注意这里我是获取的自己系统的配置缓存,这每个人根据自己系统情况修改。
require ‘google-api-php-client–PHP7.4/vendor/autoload.php’;
$params = input(‘param’);
$clientID = trim($data[‘google_appid’]);
$clientSecret = trim($data[‘google_appkey’]);
$redirectUri = HTTP_TYPE.$_SERVER[‘HTTP_HOST’].”/index.php?s=/home/Api/oa_google_callback”; //Google console redirect URI

// 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
if (isset($params[‘code’])) {
$token = $client->fetchAccessTokenWithAuthCode($params[‘code’]);
$client->setAccessToken($token[‘access_token’]);
// get profile info
$google_oauth = new \Google_Service_Oauth2($client);
$google_account_info = $google_oauth->userinfo->get();

//next…
} else {
$loginUrl = $client->createAuthUrl();
header(‘location:’.$loginUrl);
}
回调文件 callback.php

$data = cache(“config”, true); // 获取缓存设置参数
require ‘google-api-php-client–PHP7.4/vendor/autoload.php’;
$params = input(‘param’);
$clientID = trim($data[‘google_appid’]);
$clientSecret = trim($data[‘google_appkey’]);
$redirectUri = HTTP_TYPE.$_SERVER[‘HTTP_HOST’].”/index.php?s=/home/Api/oa_google_callback”; //Google console redirect URI

// 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
if (isset($params[‘code’])) {
$token = $client->authenticate($params[‘code’]);
$client->setAccessToken($token[‘access_token’]);
$q = ‘https://www.googleapis.com/oauth2/v1/userinfo?access_token=’.$token[‘access_token’];
$json = httpGet($q);
$userInfoArray = json_decode($json,true);
// print_r($userInfoArray);
$type = ‘google’;
$client_id = $userInfoArray[‘id’];
$client_name = $userInfoArray[‘name’];
$client_picture = $userInfoArray[‘picture’];
$client_email = $userInfoArray[’email’];
//next…
}
页面登陆效果:

Thinkphp6 对接 google 谷歌第三方登录接口

————————————————

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

原文链接:https://blog.csdn.net/qq19124152/article/details/124731628

正文完
 1
评论(没有评论)