2025-04-28 21:16:35 +08:00
|
|
|
|
package worker
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"StuAcaWorksAI/proto"
|
|
|
|
|
|
"bytes"
|
|
|
|
|
|
"encoding/json"
|
|
|
|
|
|
"errors"
|
2025-05-06 11:16:55 +08:00
|
|
|
|
"fmt"
|
2025-04-28 21:16:35 +08:00
|
|
|
|
"io"
|
2025-04-29 13:02:00 +08:00
|
|
|
|
"log"
|
2025-04-28 21:16:35 +08:00
|
|
|
|
"net/http"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func GetGiteeAccessTokenByCode(code string, redirectURI string, clientID string, clientSecret string) (proto.GiteeOAuthTokenResponse, error) {
|
|
|
|
|
|
req := proto.GiteeOAuthRequest{
|
|
|
|
|
|
ClientID: clientID,
|
|
|
|
|
|
ClientSecret: clientSecret,
|
|
|
|
|
|
Code: code,
|
|
|
|
|
|
RedirectURI: redirectURI,
|
|
|
|
|
|
GrantType: "authorization_code",
|
|
|
|
|
|
}
|
|
|
|
|
|
var resp proto.GiteeOAuthTokenResponse
|
|
|
|
|
|
reqBytes, err := json.Marshal(req)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return resp, err
|
|
|
|
|
|
}
|
|
|
|
|
|
url := "https://gitee.com/oauth/token"
|
|
|
|
|
|
|
|
|
|
|
|
err2, respBytes := DoPostRequestJSON(url, reqBytes, nil)
|
|
|
|
|
|
if err2 != nil {
|
|
|
|
|
|
return resp, err2
|
|
|
|
|
|
}
|
|
|
|
|
|
err = json.Unmarshal(respBytes, &resp)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return resp, err
|
|
|
|
|
|
}
|
|
|
|
|
|
return resp, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func GetGiteeUserInfo(accessToken string) (proto.GitHubUserInfo, error) {
|
|
|
|
|
|
url := "https://gitee.com/api/v5/user?access_token=" + accessToken
|
|
|
|
|
|
var resp proto.GitHubUserInfo
|
|
|
|
|
|
err2, respBytes := DoGetRequest(url, nil)
|
|
|
|
|
|
if err2 != nil {
|
|
|
|
|
|
return resp, err2
|
|
|
|
|
|
}
|
|
|
|
|
|
err := json.Unmarshal(respBytes, &resp)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return resp, err
|
|
|
|
|
|
}
|
|
|
|
|
|
return resp, nil
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func GetGogsAccessTokenByCode() {
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func GetGogsUserInfo() {
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 获取access token
|
|
|
|
|
|
func ExchangeCodeForAccessToken(clientID, clientSecret, code, redirectURI string) (proto.GitHubOAuthResponse, error) {
|
|
|
|
|
|
request := proto.GitHubOAuthRequest{
|
|
|
|
|
|
ClientID: clientID,
|
|
|
|
|
|
ClientSecret: clientSecret,
|
|
|
|
|
|
Code: code,
|
|
|
|
|
|
RedirectURI: redirectURI,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
payload, err := json.Marshal(request)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return proto.GitHubOAuthResponse{}, err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
req, err := http.NewRequest("POST", "https://github.com/login/oauth/access_token", bytes.NewBuffer(payload))
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return proto.GitHubOAuthResponse{}, err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
|
|
req.Header.Set("Accept", "application/json")
|
|
|
|
|
|
|
|
|
|
|
|
client := &http.Client{}
|
|
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return proto.GitHubOAuthResponse{}, err
|
|
|
|
|
|
}
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return proto.GitHubOAuthResponse{}, err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var response proto.GitHubOAuthResponse
|
|
|
|
|
|
err = json.Unmarshal(body, &response)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return proto.GitHubOAuthResponse{}, err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return response, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-29 13:00:29 +08:00
|
|
|
|
func ExchangeCodeForAccessTokenGithub(clientID, clientSecret, code, redirectURI string) (proto.GitHubOAuthResponse, error) {
|
|
|
|
|
|
request := proto.GitHubOAuthRequest{
|
|
|
|
|
|
ClientID: clientID,
|
|
|
|
|
|
ClientSecret: clientSecret,
|
|
|
|
|
|
Code: code,
|
|
|
|
|
|
RedirectURI: redirectURI,
|
|
|
|
|
|
}
|
|
|
|
|
|
var githubOAuthResponse proto.GitHubOAuthResponse
|
|
|
|
|
|
|
|
|
|
|
|
var onlineReq proto.OnlineServerReq
|
|
|
|
|
|
onlineReq.Type = "post"
|
2025-04-30 16:10:52 +08:00
|
|
|
|
onlineReq.PostType = "json"
|
2025-04-29 13:00:29 +08:00
|
|
|
|
onlineReq.Url = "https://github.com/login/oauth/access_token"
|
|
|
|
|
|
onlineReq.Data = request
|
|
|
|
|
|
|
|
|
|
|
|
header := make([]proto.OutlineServerReqData, 0)
|
|
|
|
|
|
header = append(header, proto.OutlineServerReqData{
|
|
|
|
|
|
Key: "Content-Type",
|
|
|
|
|
|
Value: "application/json",
|
|
|
|
|
|
})
|
|
|
|
|
|
header = append(header, proto.OutlineServerReqData{
|
|
|
|
|
|
Key: "Accept",
|
|
|
|
|
|
Value: "application/json",
|
|
|
|
|
|
})
|
|
|
|
|
|
superTokens := GetRedisSetMembers("super_permission_tokens")
|
|
|
|
|
|
header = append(header, proto.OutlineServerReqData{
|
|
|
|
|
|
Key: "token",
|
|
|
|
|
|
Value: superTokens[0],
|
|
|
|
|
|
})
|
|
|
|
|
|
onlineReq.Header = header
|
|
|
|
|
|
onlineReqBytes, _ := json.Marshal(onlineReq)
|
|
|
|
|
|
headers := map[string]string{
|
|
|
|
|
|
"token": superTokens[0],
|
|
|
|
|
|
"super_id": "1",
|
|
|
|
|
|
}
|
2025-04-29 13:02:00 +08:00
|
|
|
|
log.Println("ExchangeCodeForAccessTokenGithub onlineReqBytes:", string(onlineReqBytes))
|
2025-04-29 13:00:29 +08:00
|
|
|
|
|
|
|
|
|
|
err, respBytes := DoPostRequestJSON("https://vis.ljsea.top/tool/online_server_request?super_id=1", onlineReqBytes, headers)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return githubOAuthResponse, err
|
|
|
|
|
|
}
|
2025-04-29 13:02:00 +08:00
|
|
|
|
log.Println("ExchangeCodeForAccessTokenGithub respBytes:", string(respBytes))
|
2025-04-29 13:00:29 +08:00
|
|
|
|
var onlineResp proto.OutlineServerReqResp
|
|
|
|
|
|
err = json.Unmarshal(respBytes, &onlineResp)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return githubOAuthResponse, err
|
|
|
|
|
|
}
|
|
|
|
|
|
err = json.Unmarshal([]byte(onlineResp.Data.Response.Response), &githubOAuthResponse)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return githubOAuthResponse, err
|
|
|
|
|
|
}
|
|
|
|
|
|
return githubOAuthResponse, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-28 21:16:35 +08:00
|
|
|
|
// 获取用户信息
|
|
|
|
|
|
func GetGitHubUserInfo(accessToken string) (proto.GitHubUserInfo, error) {
|
|
|
|
|
|
|
|
|
|
|
|
url := "https://api.github.com/user"
|
|
|
|
|
|
headers := map[string]string{
|
|
|
|
|
|
"Authorization": "Bearer " + accessToken,
|
|
|
|
|
|
}
|
|
|
|
|
|
err, data := DoGetRequest(url, headers)
|
|
|
|
|
|
var resp proto.GitHubUserInfo
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return resp, err
|
|
|
|
|
|
}
|
|
|
|
|
|
err = json.Unmarshal(data, &resp)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return resp, err
|
|
|
|
|
|
}
|
|
|
|
|
|
if resp.UserID == 0 {
|
|
|
|
|
|
return resp, errors.New("获取用户信息失败,请检查access_token是否正确")
|
|
|
|
|
|
}
|
|
|
|
|
|
return resp, err
|
|
|
|
|
|
}
|
2025-04-29 13:00:29 +08:00
|
|
|
|
func GetGitHubUserInfoV2(accessToken string) (proto.GitHubUserInfo, error) {
|
|
|
|
|
|
url := "https://api.github.com/user"
|
|
|
|
|
|
var onlineReq proto.OnlineServerReq
|
|
|
|
|
|
onlineReq.Type = "get"
|
|
|
|
|
|
onlineReq.Url = url
|
|
|
|
|
|
|
|
|
|
|
|
header := make([]proto.OutlineServerReqData, 0)
|
|
|
|
|
|
header = append(header, proto.OutlineServerReqData{
|
|
|
|
|
|
Key: "Authorization",
|
|
|
|
|
|
Value: "Bearer " + accessToken,
|
|
|
|
|
|
})
|
|
|
|
|
|
onlineReq.Header = header
|
|
|
|
|
|
superTokens := GetRedisSetMembers("super_permission_tokens")
|
|
|
|
|
|
onlineReqBytes, _ := json.Marshal(onlineReq)
|
|
|
|
|
|
headers := map[string]string{
|
|
|
|
|
|
"token": superTokens[0],
|
|
|
|
|
|
"super_id": "1",
|
|
|
|
|
|
}
|
2025-04-29 13:02:00 +08:00
|
|
|
|
log.Println("GetGitHubUserInfoV2 onlineReqBytes:", string(onlineReqBytes))
|
2025-04-29 13:00:29 +08:00
|
|
|
|
err, respBytes := DoPostRequestJSON("https://vis.ljsea.top/tool/online_server_request?super_id=1", onlineReqBytes, headers)
|
2025-04-29 13:02:00 +08:00
|
|
|
|
log.Println("GetGitHubUserInfoV2 respBytes:", string(respBytes))
|
2025-04-29 13:00:29 +08:00
|
|
|
|
var onlineResp proto.OutlineServerReqResp
|
|
|
|
|
|
var resp proto.GitHubUserInfo
|
|
|
|
|
|
err = json.Unmarshal(respBytes, &onlineResp)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return resp, err
|
|
|
|
|
|
}
|
|
|
|
|
|
err = json.Unmarshal([]byte(onlineResp.Data.Response.Response), &resp)
|
|
|
|
|
|
//err, data := DoGetRequest(url, headers)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return resp, err
|
|
|
|
|
|
}
|
|
|
|
|
|
if resp.UserID == 0 {
|
|
|
|
|
|
return resp, errors.New("获取用户信息失败,请检查access_token是否正确")
|
|
|
|
|
|
}
|
|
|
|
|
|
return resp, err
|
|
|
|
|
|
}
|
2025-04-28 21:16:35 +08:00
|
|
|
|
|
|
|
|
|
|
// 谷歌登录授权
|
|
|
|
|
|
const (
|
|
|
|
|
|
GoogleClientID = "194888366727-2uvqs43mimk46mmilc04pptrjkqfjn97.apps.googleusercontent.com"
|
|
|
|
|
|
GoogleClientSecret = "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"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func GetGoogleAccessTokenByCode(code string, redirectURI string, clientID string, clientSecret string) (proto.GoogleOAuthResponse, error) {
|
|
|
|
|
|
var resp proto.GoogleOAuthResponse
|
|
|
|
|
|
|
2025-04-30 16:10:52 +08:00
|
|
|
|
url := "https://www.googleapis.com/oauth2/v4/token"
|
2025-04-28 21:16:35 +08:00
|
|
|
|
req := proto.GoogleOAuthRequest{
|
|
|
|
|
|
ClientID: clientID,
|
|
|
|
|
|
ClientSecret: clientSecret,
|
|
|
|
|
|
Code: code,
|
2025-04-30 16:10:52 +08:00
|
|
|
|
RedirectURI: redirectURI,
|
|
|
|
|
|
GrantType: "authorization_code",
|
2025-04-28 21:16:35 +08:00
|
|
|
|
}
|
2025-04-30 16:10:52 +08:00
|
|
|
|
var onlineReq proto.OnlineServerReq
|
|
|
|
|
|
onlineReq.Type = "post"
|
|
|
|
|
|
onlineReq.PostType = "form"
|
|
|
|
|
|
onlineReq.Url = url
|
|
|
|
|
|
superTokens := GetRedisSetMembers("super_permission_tokens")
|
|
|
|
|
|
onlineReq.Data = req
|
|
|
|
|
|
onlineReqBytes, _ := json.Marshal(onlineReq)
|
|
|
|
|
|
headers := map[string]string{
|
|
|
|
|
|
"token": superTokens[0],
|
|
|
|
|
|
"super_id": "1",
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
log.Println("GetGoogleAccessTokenByCode onlineReqBytes:", string(onlineReqBytes))
|
|
|
|
|
|
err, respBytes := DoPostRequestJSON("https://vis.ljsea.top/tool/online_server_request?super_id=1", onlineReqBytes, headers)
|
|
|
|
|
|
log.Println("GetGoogleAccessTokenByCode respBytes:", string(respBytes))
|
|
|
|
|
|
var onlineResp proto.OutlineServerReqResp
|
|
|
|
|
|
err = json.Unmarshal(respBytes, &onlineResp)
|
2025-04-28 21:16:35 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return resp, err
|
|
|
|
|
|
}
|
2025-04-30 16:10:52 +08:00
|
|
|
|
err = json.Unmarshal([]byte(onlineResp.Data.Response.Response), &resp)
|
2025-04-28 21:16:35 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return resp, err
|
|
|
|
|
|
}
|
|
|
|
|
|
return resp, nil
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-30 14:37:36 +08:00
|
|
|
|
func GetGoogleUserInfo(accessToken string) (proto.GoogleUserInfoResp, error) {
|
|
|
|
|
|
var resp proto.GoogleUserInfoResp
|
|
|
|
|
|
url := "https://www.googleapis.com/oauth2/v2/userinfo?access_token=" + accessToken
|
|
|
|
|
|
var onlineReq proto.OnlineServerReq
|
|
|
|
|
|
onlineReq.Type = "get"
|
|
|
|
|
|
onlineReq.Url = url
|
|
|
|
|
|
superTokens := GetRedisSetMembers("super_permission_tokens")
|
|
|
|
|
|
onlineReqBytes, _ := json.Marshal(onlineReq)
|
|
|
|
|
|
headers := map[string]string{
|
|
|
|
|
|
"token": superTokens[0],
|
|
|
|
|
|
"super_id": "1",
|
|
|
|
|
|
}
|
|
|
|
|
|
log.Println("GetGoogleUserInfo onlineReqBytes:", string(onlineReqBytes))
|
|
|
|
|
|
err, respBytes := DoPostRequestJSON("https://vis.ljsea.top/tool/online_server_request?super_id=1", onlineReqBytes, headers)
|
|
|
|
|
|
log.Println("GetGoogleUserInfo respBytes:", string(respBytes))
|
|
|
|
|
|
var onlineResp proto.OutlineServerReqResp
|
|
|
|
|
|
err = json.Unmarshal(respBytes, &onlineResp)
|
2025-04-28 21:16:35 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return resp, err
|
|
|
|
|
|
}
|
2025-04-30 14:37:36 +08:00
|
|
|
|
err = json.Unmarshal([]byte(onlineResp.Data.Response.Response), &resp)
|
|
|
|
|
|
//err, respBytes := DoGetRequest(url, nil)
|
|
|
|
|
|
//if err != nil {
|
|
|
|
|
|
// return resp, err
|
|
|
|
|
|
//}
|
2025-04-28 21:16:35 +08:00
|
|
|
|
err = json.Unmarshal(respBytes, &resp)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return resp, err
|
|
|
|
|
|
}
|
|
|
|
|
|
return resp, nil
|
|
|
|
|
|
}
|
2025-05-01 12:54:54 +08:00
|
|
|
|
|
|
|
|
|
|
// facebook
|
|
|
|
|
|
const (
|
|
|
|
|
|
FacebookClientID = "1171721801397908"
|
|
|
|
|
|
FacebookClientSecret = "e5eadef4d764e4458fc68bd1e4792dbf"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func GetFacebookAccessTokenByCode(code string, redirectURI string, clientID string, clientSecret string) (proto.FacebookOAuthResponse, error) {
|
|
|
|
|
|
var resp proto.FacebookOAuthResponse
|
|
|
|
|
|
|
2025-05-01 13:12:25 +08:00
|
|
|
|
url := "https://graph.facebook.com/v22.0/oauth/access_token" + "?client_id=" + clientID + "&client_secret=" + clientSecret + "&code=" + code + "&redirect_uri=" + redirectURI
|
|
|
|
|
|
//req := proto.FaceBookOAuthRequest{
|
|
|
|
|
|
// ClientID: clientID,
|
|
|
|
|
|
// ClientSecret: clientSecret,
|
|
|
|
|
|
// Code: code,
|
|
|
|
|
|
// RedirectURI: redirectURI,
|
|
|
|
|
|
//}
|
2025-05-01 13:21:21 +08:00
|
|
|
|
log.Println("GetFacebookAccessTokenByCode url:", url)
|
2025-05-01 12:54:54 +08:00
|
|
|
|
var onlineReq proto.OnlineServerReq
|
|
|
|
|
|
onlineReq.Type = "get"
|
|
|
|
|
|
onlineReq.Url = url
|
|
|
|
|
|
superTokens := GetRedisSetMembers("super_permission_tokens")
|
2025-05-01 13:12:25 +08:00
|
|
|
|
//onlineReq.Data = req
|
2025-05-01 12:54:54 +08:00
|
|
|
|
onlineReqBytes, _ := json.Marshal(onlineReq)
|
|
|
|
|
|
headers := map[string]string{
|
|
|
|
|
|
"token": superTokens[0],
|
|
|
|
|
|
"super_id": "1",
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
log.Println("GetFacebookAccessTokenByCode onlineReqBytes:", string(onlineReqBytes))
|
|
|
|
|
|
err, respBytes := DoPostRequestJSON("https://vis.ljsea.top/tool/online_server_request?super_id=1", onlineReqBytes, headers)
|
|
|
|
|
|
log.Println("GetFacebookAccessTokenByCode respBytes:", string(respBytes))
|
|
|
|
|
|
var onlineResp proto.OutlineServerReqResp
|
|
|
|
|
|
err = json.Unmarshal(respBytes, &onlineResp)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return resp, err
|
|
|
|
|
|
}
|
|
|
|
|
|
err = json.Unmarshal([]byte(onlineResp.Data.Response.Response), &resp)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return resp, err
|
|
|
|
|
|
}
|
|
|
|
|
|
return resp, nil
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func GetFaceBookUserInfo(accessToken string) (proto.FaceBookUserInfoResp, error) {
|
|
|
|
|
|
var resp proto.FaceBookUserInfoResp
|
|
|
|
|
|
url := "https://graph.facebook.com/v22.0/me?fields=id,name"
|
|
|
|
|
|
var onlineReq proto.OnlineServerReq
|
|
|
|
|
|
onlineReq.Type = "get"
|
|
|
|
|
|
onlineReq.Url = url
|
|
|
|
|
|
onlineReqHeader := make([]proto.OutlineServerReqData, 0)
|
|
|
|
|
|
onlineReqHeader = append(onlineReqHeader, proto.OutlineServerReqData{
|
|
|
|
|
|
Key: "Authorization",
|
|
|
|
|
|
Value: "Bearer " + accessToken,
|
|
|
|
|
|
})
|
|
|
|
|
|
onlineReq.Header = onlineReqHeader
|
|
|
|
|
|
superTokens := GetRedisSetMembers("super_permission_tokens")
|
|
|
|
|
|
onlineReqBytes, _ := json.Marshal(onlineReq)
|
|
|
|
|
|
headers := map[string]string{
|
|
|
|
|
|
"token": superTokens[0],
|
|
|
|
|
|
"super_id": "1",
|
|
|
|
|
|
}
|
|
|
|
|
|
log.Println("GetGoogleUserInfo onlineReqBytes:", string(onlineReqBytes))
|
|
|
|
|
|
err, respBytes := DoPostRequestJSON("https://vis.ljsea.top/tool/online_server_request?super_id=1", onlineReqBytes, headers)
|
|
|
|
|
|
log.Println("GetGoogleUserInfo respBytes:", string(respBytes))
|
|
|
|
|
|
var onlineResp proto.OutlineServerReqResp
|
|
|
|
|
|
err = json.Unmarshal(respBytes, &onlineResp)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return resp, err
|
|
|
|
|
|
}
|
|
|
|
|
|
err = json.Unmarshal([]byte(onlineResp.Data.Response.Response), &resp)
|
|
|
|
|
|
//err, respBytes := DoGetRequest(url, nil)
|
|
|
|
|
|
//if err != nil {
|
|
|
|
|
|
// return resp, err
|
|
|
|
|
|
//}
|
|
|
|
|
|
err = json.Unmarshal(respBytes, &resp)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return resp, err
|
|
|
|
|
|
}
|
|
|
|
|
|
return resp, nil
|
|
|
|
|
|
}
|
2025-05-06 11:16:55 +08:00
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
|
StackOverflowClientID = "32093"
|
2025-05-06 12:53:34 +08:00
|
|
|
|
StackOverflowClientSecret = "SOvAmbfCBeei1ikqH4IwNA(("
|
2025-05-06 11:16:55 +08:00
|
|
|
|
StackOverflowKey = "rl_s6vbPNPhbFbMcyWp7YbaTeg18"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func GetStackoverflowAccessTokenByCode(code string, redirectURI string, clientID string, clientSecret string) (proto.StackoverflowOAuthResponse, error) {
|
|
|
|
|
|
var resp proto.StackoverflowOAuthResponse
|
|
|
|
|
|
|
|
|
|
|
|
url := "https://stackoverflow.com/oauth/access_token/json"
|
|
|
|
|
|
req := proto.FaceBookOAuthRequest{
|
|
|
|
|
|
ClientID: clientID,
|
|
|
|
|
|
ClientSecret: clientSecret,
|
|
|
|
|
|
Code: code,
|
|
|
|
|
|
RedirectURI: redirectURI,
|
|
|
|
|
|
}
|
|
|
|
|
|
log.Println("GetFacebookAccessTokenByCode url:", url)
|
|
|
|
|
|
var onlineReq proto.OnlineServerReq
|
|
|
|
|
|
onlineReq.Type = "post"
|
2025-05-06 12:08:20 +08:00
|
|
|
|
onlineReq.PostType = "form-url-encoded"
|
2025-05-06 11:16:55 +08:00
|
|
|
|
onlineReq.Url = url
|
|
|
|
|
|
onlineReq.Data = req
|
|
|
|
|
|
superTokens := GetRedisSetMembers("super_permission_tokens")
|
|
|
|
|
|
//onlineReq.Data = req
|
|
|
|
|
|
onlineReqBytes, _ := json.Marshal(onlineReq)
|
|
|
|
|
|
headers := map[string]string{
|
|
|
|
|
|
"token": superTokens[0],
|
|
|
|
|
|
"super_id": "1",
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
log.Println("GetStackoverflowAccessTokenByCode onlineReqBytes:", string(onlineReqBytes))
|
|
|
|
|
|
err, respBytes := DoPostRequestJSON("https://vis.ljsea.top/tool/online_server_request?super_id=1", onlineReqBytes, headers)
|
|
|
|
|
|
log.Println("GetStackoverflowAccessTokenByCode respBytes:", string(respBytes))
|
|
|
|
|
|
var onlineResp proto.OutlineServerReqResp
|
|
|
|
|
|
err = json.Unmarshal(respBytes, &onlineResp)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return resp, err
|
|
|
|
|
|
}
|
|
|
|
|
|
err = json.Unmarshal([]byte(onlineResp.Data.Response.Response), &resp)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return resp, err
|
|
|
|
|
|
}
|
|
|
|
|
|
return resp, nil
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func GetStackoverflowUserInfo(accessToken string) (proto.StackoverflowUserInfoResponse, error) {
|
|
|
|
|
|
var resp proto.StackoverflowUserInfoResponse
|
|
|
|
|
|
url := "https://api.stackexchange.com/2.3/me?site=stackoverflow&order=desc&sort=reputation"
|
|
|
|
|
|
url = fmt.Sprintf("%s&access_token=%s&key=%s", url, accessToken, StackOverflowKey)
|
|
|
|
|
|
var onlineReq proto.OnlineServerReq
|
|
|
|
|
|
onlineReq.Type = "get"
|
|
|
|
|
|
onlineReq.Url = url
|
|
|
|
|
|
superTokens := GetRedisSetMembers("super_permission_tokens")
|
|
|
|
|
|
onlineReqBytes, _ := json.Marshal(onlineReq)
|
|
|
|
|
|
headers := map[string]string{
|
|
|
|
|
|
"token": superTokens[0],
|
|
|
|
|
|
"super_id": "1",
|
|
|
|
|
|
}
|
|
|
|
|
|
log.Println("GetStackoverflowUserInfo onlineReqBytes:", string(onlineReqBytes))
|
|
|
|
|
|
err, respBytes := DoPostRequestJSON("https://vis.ljsea.top/tool/online_server_request?super_id=1", onlineReqBytes, headers)
|
|
|
|
|
|
log.Println("GetStackoverflowUserInfo respBytes:", string(respBytes))
|
|
|
|
|
|
var onlineResp proto.OutlineServerReqResp
|
|
|
|
|
|
err = json.Unmarshal(respBytes, &onlineResp)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return resp, err
|
|
|
|
|
|
}
|
|
|
|
|
|
err = json.Unmarshal([]byte(onlineResp.Data.Response.Response), &resp)
|
|
|
|
|
|
//err, respBytes := DoGetRequest(url, nil)
|
|
|
|
|
|
//if err != nil {
|
|
|
|
|
|
// return resp, err
|
|
|
|
|
|
//}
|
|
|
|
|
|
err = json.Unmarshal(respBytes, &resp)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return resp, err
|
|
|
|
|
|
}
|
|
|
|
|
|
return resp, nil
|
|
|
|
|
|
}
|
2025-05-06 13:47:44 +08:00
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
|
QQClientID = "102774740"
|
|
|
|
|
|
QQClientSecret = "iIfpkRuAF8rPn13q"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func GetQQAccessTokenByCode(code string, redirectURI string, clientID string, clientSecret string) (proto.QQOAuthTokenResponse, error) {
|
|
|
|
|
|
req := proto.QQOAuthRequest{
|
|
|
|
|
|
ClientID: clientID,
|
|
|
|
|
|
ClientSecret: clientSecret,
|
|
|
|
|
|
Code: code,
|
|
|
|
|
|
RedirectURI: redirectURI,
|
|
|
|
|
|
GrantType: "authorization_code",
|
|
|
|
|
|
}
|
|
|
|
|
|
var resp proto.QQOAuthTokenResponse
|
|
|
|
|
|
url := "https://graph.qq.com/oauth2.0/token"
|
|
|
|
|
|
url = fmt.Sprintf("%s?grant_type=%s&client_id=%s&client_secret=%s&code=%s&redirect_uri=%s&fmt=json", url, req.GrantType, req.ClientID, req.ClientSecret, req.Code, req.RedirectURI)
|
|
|
|
|
|
|
|
|
|
|
|
err2, respBytes := DoGetRequest(url, nil)
|
|
|
|
|
|
if err2 != nil {
|
|
|
|
|
|
return resp, err2
|
|
|
|
|
|
}
|
|
|
|
|
|
err := json.Unmarshal(respBytes, &resp)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return resp, err
|
|
|
|
|
|
}
|
|
|
|
|
|
return resp, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func GetQQUserInfo(accessToken string) (proto.QQUserInfoResponse, error) {
|
|
|
|
|
|
var resp proto.QQUserInfoResponse
|
|
|
|
|
|
|
|
|
|
|
|
var openIDInfo proto.GetQQOpenIDResponse
|
|
|
|
|
|
//先获取openid
|
|
|
|
|
|
url := fmt.Sprintf("https://graph.qq.com/oauth2.0/me?access_token=%s&fmt=json", accessToken)
|
|
|
|
|
|
err2, respBytes := DoGetRequest(url, nil)
|
|
|
|
|
|
if err2 != nil {
|
|
|
|
|
|
log.Println("GetQQUserInfo get openid err:", err2)
|
|
|
|
|
|
return resp, err2
|
|
|
|
|
|
}
|
|
|
|
|
|
err := json.Unmarshal(respBytes, &openIDInfo)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return resp, err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//如果openid获取成功,获取用户信息
|
|
|
|
|
|
url = fmt.Sprintf("https://graph.qq.com/user/get_user_info?access_token=%s&oauth_consumer_key=%s&openid=%s&fmt=json", accessToken, QQClientID, openIDInfo.OpenID)
|
|
|
|
|
|
err3, respBytes2 := DoGetRequest(url, nil)
|
|
|
|
|
|
if err3 != nil {
|
|
|
|
|
|
log.Println("GetQQUserInfo get user info err:", err2)
|
|
|
|
|
|
return resp, err2
|
|
|
|
|
|
}
|
|
|
|
|
|
err = json.Unmarshal(respBytes2, &resp)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return resp, err
|
|
|
|
|
|
}
|
|
|
|
|
|
resp.OpenID = openIDInfo.OpenID
|
|
|
|
|
|
return resp, nil
|
|
|
|
|
|
|
|
|
|
|
|
}
|