正常的版本

This commit is contained in:
eson
2023-07-18 16:44:46 +08:00
parent e4e9339071
commit c13225d2fa
6 changed files with 46 additions and 23 deletions

View File

@@ -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)
}