正常的版本
This commit is contained in:
@@ -6,18 +6,22 @@ import (
|
||||
"github.com/zeromicro/go-zero/rest"
|
||||
)
|
||||
|
||||
type OAuth struct {
|
||||
Name string `yaml:"name"`
|
||||
Appid string `yaml:"appid"`
|
||||
Secret string `yaml:"secret"`
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
rest.RestConf
|
||||
SourceMysql string
|
||||
Auth types.Auth
|
||||
|
||||
OAuth []OAuth
|
||||
OAuth struct {
|
||||
Google struct {
|
||||
Appid string
|
||||
Secret string
|
||||
}
|
||||
|
||||
Facebook struct {
|
||||
Appid string
|
||||
Secret string
|
||||
}
|
||||
}
|
||||
|
||||
Stripe struct {
|
||||
SK string
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"fusenapi/utils/auth"
|
||||
"fusenapi/utils/basic"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"context"
|
||||
|
||||
@@ -12,6 +13,7 @@ import (
|
||||
|
||||
"github.com/474420502/requests"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"golang.org/x/net/proxy"
|
||||
"golang.org/x/oauth2"
|
||||
"golang.org/x/oauth2/google"
|
||||
)
|
||||
@@ -34,20 +36,35 @@ func (l *UserGoogleLoginLogic) UserGoogleLogin(req *types.RequestGoogleLogin, us
|
||||
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
|
||||
// userinfo 传入值时, 一定不为null
|
||||
|
||||
dialer, err := proxy.SOCKS5("tcp", "127.0.0.1:1080", nil, proxy.Direct)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
customClient := &http.Client{
|
||||
Transport: &http.Transport{
|
||||
Dial: dialer.Dial,
|
||||
},
|
||||
}
|
||||
|
||||
ctx := context.WithValue(context.Background(), oauth2.HTTPClient, customClient)
|
||||
|
||||
var googleOauthConfig = &oauth2.Config{
|
||||
RedirectURL: "http://localhost:9900/api/user/oauth2/login/google",
|
||||
ClientID: l.svcCtx.Config.OAuth[0].Appid,
|
||||
ClientSecret: l.svcCtx.Config.OAuth[0].Secret,
|
||||
ClientID: l.svcCtx.Config.OAuth.Google.Appid,
|
||||
ClientSecret: l.svcCtx.Config.OAuth.Google.Secret,
|
||||
Scopes: []string{"https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/userinfo.profile"},
|
||||
Endpoint: google.Endpoint,
|
||||
}
|
||||
|
||||
token, err := googleOauthConfig.Exchange(context.Background(), req.Code)
|
||||
token, err := googleOauthConfig.Exchange(ctx, req.Code)
|
||||
if err != nil {
|
||||
resp.SetStatus(basic.CodeApiErr)
|
||||
}
|
||||
ses := requests.NewSession()
|
||||
ses.Config().SetProxy("socks5://127.0.0.1:1080")
|
||||
|
||||
r, err := requests.Get("https://www.googleapis.com/oauth2/v2/userinfo" + token.AccessToken).Execute()
|
||||
r, err := ses.Get("https://www.googleapis.com/oauth2/v2/userinfo?access_token=" + token.AccessToken).Execute()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user