OAuth2
本项目演示了如何在 Go 应用中实现 OAuth2 认证。
前提条件
确保你已安装以下内容
- Golang
- OAuth2 包
设置
-
克隆仓库
git clone https://github.com/gofiber/recipes.git
cd recipes/oauth2 -
安装依赖
go get
运行应用
- 启动应用
go run main.go
环境变量
在根目录下创建 .env
文件并添加以下变量
# CLIENT_ID is the OAuth2 client ID
CLIENT_ID=
# CLIENT_SECRET is the OAuth2 client secret
CLIENT_SECRET=
# REDIRECT_URL is the OAuth2 redirect URL
REDIRECT_URL=
# AUTH_URL is the OAuth2 authorization URL
AUTH_URL=
# TOKEN_URL is the OAuth2 token URL
TOKEN_URL=
示例
这里是一个如何设置 OAuth2 配置的示例
package main
import (
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
)
func main() {
conf := &oauth2.Config{
ClientID: "your-client-id",
ClientSecret: "your-client-secret",
RedirectURL: "your-redirect-url",
Endpoint: google.Endpoint,
}
// Your code here
}