谷歌授权登录
1. 申请应用
- 创建新项目
进入 Google 开发者中心,创建项目
- 创建凭据
创建凭据时选择 OAuth client ID
必须先配置应用接受认证的界面,根据提示进行操作
- 选择
User Type
为External
填入对应的信息后完成。
- 创建
WEB 应用
再次访问 "凭据" - "创建凭据" - "OAuth 客户端 ID",选择"Web 应用",然后按照提示填入内容
保存信息后,会弹出密钥和 ID
记录以下三个信息:
Client ID
Client Secret
Authorized redirect URIs
warning
重要提示
Client secrets
可保护你应用程序的安全,因此请确保其不会泄露!也不要与任何人共享你的Client secrets
!!!
2. 集成 GAuth
- 安装 GAuth
go get github.com/LeoInnovateLab/gauth
- 导入注册
import (
_ "github.com/LeoInnovateLab/gauth/register"
)
- 创建 AuthRequest
authRequest, err := gauth.New().
Source("google").
ClientId("your_client_id").
ClientSecret("your_client_secret").
RedirectUrl("your_callback_url").
Build()
- 生成授权 URL
authorizeUrl, err := authRequest.Authorize(utils.CreateState())
可以用这个authorizeUrl
重定向跳转
3. 授权
授权结果
{
"data": {
"uid": "1141681479691234214",
"username": "[email protected]",
"nickname": "Leo",
"avatar": "https://lh3.googleusercontent.com/a/ACg8ocKXtNIpsOKHCoJvFIjAHZU3U76vTcD8CMt2K9LQBh-5Y6pZq-Ui=s96-c",
"blog": "",
"company": "",
"location": "en",
"email": "[email protected]",
"remark": "",
"gender": -1,
"source": "google",
"token": {
"access_token": "ya29.a0AXooCgvThY6MU0w68CfESXcmKt-xxxxxxxxxxx",
"expire_in": 3598,
"refresh_token": "",
"refresh_token_expire_in": 0,
"uid": "",
"open_id": "",
"access_code": "",
"union_id": "",
"scope": "https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile openid",
"token_type": "Bearer",
"id_token": "eyJhbGciOiJSUzI1NiIsImtpZCIasdfzZTU1ODExMWM3YzdhNzVjNWI2NTEzNGQyMmY2M2VlMDA2ZDAiLCJ0eXAiOiJKV1QifQ.eyJpc3MiOiJodHRwczovL2FjY291bnRzLmdvb2dsZS5jb20iLCJhenAiOiI3MzgwODk3MzIzNzEtNmI0YWQzNGdtdmJqazFoaW80bW5mNnV2dnJmdGxwMm8uYXBwcy5nb29nbGV1c2VyY29udGVudC5jb20iLCJhdWQiOiI3MzgwODk3MzIzNzEtNmI0YWQzNGdtdmJqazFoaW80bW5mNnV2dnJmdGxwMm8uYXBwcy5nb29nbGV1c2VyY29udGVudC5jb20iLCJzdWIiOiIxMTQxNjgxNDc5Njk1MzYyMTQwMDIxxxdfszcXVpZXRAZ21haWwuY29tIiwiZW1haWxfdmVyaWZpZWQiOnRydWUsImF0X2hhc2giOiJUMWtERFRkTW1VcGg2NTlMeU02OVVnIiwibmFtZSI6IkxlbyBMYXUiLCJwaWN0dXJlIjoiaHR0cHM6LyasfZ2xldXNlcmNvbnRlbnQuY29tL2EvQUNnOG9jS1h0Tklwc09LSENvSnZGSWpBSFpVM1U3NnZUYasfs5TFFCaC01WTZwWnEtVWk9czk2LWMiLCJnaXZlbl9uYW1lIjoiTGVvIiwiZmFtaWx5X25hbWUiOiJMYXUiLCJpYXQiOjE3MTQ3MTI2MzAsImV4cCI6MTcxNDcxNjIzMH0.O2Y0-c4tUcTxW7ay2BkT3L4YI2qy0p9s1J79DXWeiWRS3GtI8PzmYHLc-_QN8Dr49ggPE_mHIL1VdJ7C4U0FxhNEJLbg4KJJIODcdNj09D2llMou8TIc7krPihHeNPU_3Mj-Fs3VS36YIo4Gcked83wXaWOZpeV8QZvvUA37Gqm14dzQ076Y-sOZH2bOhjUkEWP6-4Ymx5cbSwgHtUuuYkjCRvb_Ase9eHTk-3_KczJzT-HzP5uHk-0E8YFwAJhztmDZWA64F8Og-iorsGd524GApX3aSDFzWiBKt0uDbBZ4F5XZFClnTp7DZdzeVkA07-uD9CoXRRBH6adsfas",
"mac_algorithm": "",
"mac_key": "",
"code": "",
"snapshot_user": false,
"oauth_token": "",
"oauth_token_secret": "",
"user_id": "",
"screen_name": "",
"oauth_callback_confirmed": null
},
"raw_user_info": "{\n \"sub\": \"1141681479691234214\",\n \"name\": \"Leo\",\n \"given_name\": \"Leo\",\n \"family_name\": \"test\",\n \"picture\": \"https://lh3.googleusercontent.com/a/ACg8ocKXtNIpsOKHCoJvFIjAHZU3U76vTcD8CMt2K9LQBh-5Y6pZq-Ui\\u003ds96-c\",\n \"email\": \"[email protected]\",\n \"email_verified\": true,\n \"locale\": \"en\"\n}",
"snapshot_user": false
}
}
Demo
推荐使用 GAuth Demo 进行测试.
-
将申请的
App Id
和App secrets
填入.env.demo -
启动项目,访问
http://localhost:8080/