Skip to main content

Slack 认证登录

1. 创建 Slack 应用

  • 进入 Slack developer center

  • 来到你的 app 页面, 然后,点击 Create New App 按钮。

  • 选择 From scratch.

create slack app

成功后将获得 ClientId and ClientSecret.

  • 添加 Redirect URLsScopes

slack scope

Scopes 加上 users.profile:read, users:read and users:read.email.

  • 安装你的 app

slack install app

warning

重要提示

Client secrets 可保护你应用程序的安全,因此请确保其不会泄露!也不要与任何人共享你的Client secrets!!!

2. 整合 GAuth

  • 安装 GAuth
go get github.com/LeoInnovateLab/gauth
  • import register
import (
_ "github.com/LeoInnovateLab/gauth/register"
)
  • 创建 AuthRequest
authRequest, err := gauth.New().
Source("slack").
ClientId("your_app_id").
ClientSecret("your_app_secret").
RedirectUrl("your_callback_url").
Build()
  • 生成授权 URL
authorizeUrl, err := authRequest.Authorize(utils.CreateState())

可以用这个authorizeUrl重定向跳转

3. 授权

授权结果


"data": {
"uid": "U0734xxx",
"username": "Leo",
"nickname": "Leo",
"avatar": "https://avatars.slack-edge.com/2024-05-07/7083458955538_ab4691c70b61ab7e6316_original.jpg",
"blog": "",
"company": "",
"location": "",
"email": "[email protected]",
"remark": "",
"gender": -1,
"source": "slack",
"token": {
"access_token": "xoxb-7096124864113-7083663139090",
"expire_in": 0,
"refresh_token": "",
"refresh_token_expire_in": 0,
"uid": "U07347xxxx",
"open_id": "",
"access_code": "",
"union_id": "",
"scope": "users:read,users:read.email,users.profile:read",
"token_type": "bot",
"id_token": "",
"mac_algorithm": "",
"mac_key": "",
"code": "",
"snapshot_user": false,
"oauth_token": "",
"oauth_token_secret": "",
"user_id": "",
"screen_name": "",
"oauth_callback_confirmed": null
},
"raw_user_info": "{\"ok\":true,\"user\":{\"id\":\"U07347xxx\",\"team_id\":\"T072U3xxx\",\"name\":\"leo\",\"deleted\":false,\"color\":\"9f69e7\",\"real_name\":\"Leo\",\"tz\":\"Asia\\/Chongqing\",\"tz_label\":\"China Standard Time\",\"tz_offset\":28800,\"profile\":{\"title\":\"\",\"phone\":\"\",\"skype\":\"\",\"real_name\":\"Leo\",\"real_name_normalized\":\"Leo Lau\",\"display_name\":\"\",\"display_name_normalized\":\"\",\"fields\":null,\"status_text\":\"\",\"status_emoji\":\"\",\"status_emoji_display_info\":[],\"status_expiration\":0,\"avatar_hash\":\"ab4691c\",\"image_original\":\"https:\\/\\/avatars.slack-edge.com\\/2024-05-07\\/7083458955538_ab4691c70b61ab7e6316_original.jpg\",\"is_custom_image\":true,\"email\":\"[email protected]\",\"first_name\":\"Leo\",\"last_name\":\"\",\"image_24\":\"https:\\/\\/avatars.slack-edge.com\\/2024-05-07\\/7083458955538_ab4691c70b61ab7e6316_24.jpg\",\"image_32\":\"https:\\/\\/avatars.slack-edge.com\\/2024-05-07\\/7083458955538_ab4691c70b61ab7e6316_32.jpg\",\"image_48\":\"https:\\/\\/avatars.slack-edge.com\\/2024-05-07\\/7083458955538_ab4691c70b61ab7e6316_48.jpg\",\"image_72\":\"https:\\/\\/avatars.slack-edge.com\\/2024-05-07\\/7083458955538_ab4691c70b61ab7e6316_72.jpg\",\"image_192\":\"https:\\/\\/avatars.slack-edge.com\\/2024-05-07\\/7083458955538_ab4691c70b61ab7e6316_192.jpg\",\"image_512\":\"https:\\/\\/avatars.slack-edge.com\\/2024-05-07\\/7083458955538_ab4691c70b61ab7e6316_512.jpg\",\"image_1024\":\"https:\\/\\/avatars.slack-edge.com\\/2024-05-07\\/7083458955538_ab4691c70b61ab7e6316_1024.jpg\",\"status_text_canonical\":\"\",\"team\":\"T072U3xxxx\"},\"is_admin\":true,\"is_owner\":true,\"is_primary_owner\":true,\"is_restricted\":false,\"is_ultra_restricted\":false,\"is_bot\":false,\"is_app_user\":false,\"updated\":1715133779,\"is_email_confirmed\":true,\"who_can_share_contact_card\":\"EVERYONE\"}}",
"snapshot_user": false
}
}

Demo

推荐使用 GAuth Demo 进行测试.

  • 将申请的App IdApp secrets填入.env.demo

  • 启动项目,访问 http://localhost:8080/

Demo