|
|
@ -7,12 +7,12 @@ package social |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
import ( |
|
|
|
"encoding/json" |
|
|
|
"encoding/json" |
|
|
|
|
|
|
|
"io/ioutil" |
|
|
|
"net/http" |
|
|
|
"net/http" |
|
|
|
"net/url" |
|
|
|
"net/url" |
|
|
|
"strconv" |
|
|
|
"strconv" |
|
|
|
"strings" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
oauth "github.com/gogits/oauth2" |
|
|
|
"github.com/macaron-contrib/oauth2" |
|
|
|
|
|
|
|
|
|
|
|
"github.com/gogits/gogs/models" |
|
|
|
"github.com/gogits/gogs/models" |
|
|
|
"github.com/gogits/gogs/modules/log" |
|
|
|
"github.com/gogits/gogs/modules/log" |
|
|
@ -27,16 +27,11 @@ type BasicUserInfo struct { |
|
|
|
|
|
|
|
|
|
|
|
type SocialConnector interface { |
|
|
|
type SocialConnector interface { |
|
|
|
Type() int |
|
|
|
Type() int |
|
|
|
SetRedirectUrl(string) |
|
|
|
UserInfo(*oauth2.Token, *url.URL) (*BasicUserInfo, error) |
|
|
|
UserInfo(*oauth.Token, *url.URL) (*BasicUserInfo, error) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
AuthCodeURL(string) string |
|
|
|
|
|
|
|
Exchange(string) (*oauth.Token, error) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
var ( |
|
|
|
var ( |
|
|
|
SocialBaseUrl = "/user/login" |
|
|
|
SocialMap = make(map[string]SocialConnector) |
|
|
|
SocialMap = make(map[string]SocialConnector) |
|
|
|
|
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
func NewOauthService() { |
|
|
|
func NewOauthService() { |
|
|
@ -47,24 +42,29 @@ func NewOauthService() { |
|
|
|
setting.OauthService = &setting.Oauther{} |
|
|
|
setting.OauthService = &setting.Oauther{} |
|
|
|
setting.OauthService.OauthInfos = make(map[string]*setting.OauthInfo) |
|
|
|
setting.OauthService.OauthInfos = make(map[string]*setting.OauthInfo) |
|
|
|
|
|
|
|
|
|
|
|
socialConfigs := make(map[string]*oauth.Config) |
|
|
|
socialConfigs := make(map[string]*oauth2.Options) |
|
|
|
allOauthes := []string{"github", "google", "qq", "twitter", "weibo"} |
|
|
|
allOauthes := []string{"github", "google", "qq", "twitter", "weibo"} |
|
|
|
// Load all OAuth config data.
|
|
|
|
// Load all OAuth config data.
|
|
|
|
for _, name := range allOauthes { |
|
|
|
for _, name := range allOauthes { |
|
|
|
|
|
|
|
if !setting.Cfg.MustBool("oauth."+name, "ENABLED") { |
|
|
|
|
|
|
|
continue |
|
|
|
|
|
|
|
} |
|
|
|
setting.OauthService.OauthInfos[name] = &setting.OauthInfo{ |
|
|
|
setting.OauthService.OauthInfos[name] = &setting.OauthInfo{ |
|
|
|
ClientId: setting.Cfg.MustValue("oauth."+name, "CLIENT_ID"), |
|
|
|
Options: oauth2.Options{ |
|
|
|
ClientSecret: setting.Cfg.MustValue("oauth."+name, "CLIENT_SECRET"), |
|
|
|
ClientID: setting.Cfg.MustValue("oauth."+name, "CLIENT_ID"), |
|
|
|
Scopes: setting.Cfg.MustValue("oauth."+name, "SCOPES"), |
|
|
|
ClientSecret: setting.Cfg.MustValue("oauth."+name, "CLIENT_SECRET"), |
|
|
|
AuthUrl: setting.Cfg.MustValue("oauth."+name, "AUTH_URL"), |
|
|
|
Scopes: setting.Cfg.MustValueArray("oauth."+name, "SCOPES", " "), |
|
|
|
TokenUrl: setting.Cfg.MustValue("oauth."+name, "TOKEN_URL"), |
|
|
|
PathLogin: "/user/login/oauth2/" + name, |
|
|
|
|
|
|
|
PathCallback: setting.AppSubUrl + "/user/login/" + name, |
|
|
|
|
|
|
|
RedirectURL: setting.AppUrl + "user/login/" + name, |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
AuthUrl: setting.Cfg.MustValue("oauth."+name, "AUTH_URL"), |
|
|
|
|
|
|
|
TokenUrl: setting.Cfg.MustValue("oauth."+name, "TOKEN_URL"), |
|
|
|
} |
|
|
|
} |
|
|
|
socialConfigs[name] = &oauth.Config{ |
|
|
|
socialConfigs[name] = &oauth2.Options{ |
|
|
|
ClientId: setting.OauthService.OauthInfos[name].ClientId, |
|
|
|
ClientID: setting.OauthService.OauthInfos[name].ClientID, |
|
|
|
ClientSecret: setting.OauthService.OauthInfos[name].ClientSecret, |
|
|
|
ClientSecret: setting.OauthService.OauthInfos[name].ClientSecret, |
|
|
|
RedirectURL: strings.TrimSuffix(setting.AppUrl, "/") + SocialBaseUrl + name, |
|
|
|
Scopes: setting.OauthService.OauthInfos[name].Scopes, |
|
|
|
Scope: setting.OauthService.OauthInfos[name].Scopes, |
|
|
|
|
|
|
|
AuthURL: setting.OauthService.OauthInfos[name].AuthUrl, |
|
|
|
|
|
|
|
TokenURL: setting.OauthService.OauthInfos[name].TokenUrl, |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
enabledOauths := make([]string, 0, 10) |
|
|
|
enabledOauths := make([]string, 0, 10) |
|
|
@ -91,11 +91,11 @@ func NewOauthService() { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Twitter.
|
|
|
|
// Twitter.
|
|
|
|
if setting.Cfg.MustBool("oauth.twitter", "ENABLED") { |
|
|
|
// if setting.Cfg.MustBool("oauth.twitter", "ENABLED") {
|
|
|
|
setting.OauthService.Twitter = true |
|
|
|
// setting.OauthService.Twitter = true
|
|
|
|
newTwitterOauth(socialConfigs["twitter"]) |
|
|
|
// newTwitterOauth(socialConfigs["twitter"])
|
|
|
|
enabledOauths = append(enabledOauths, "Twitter") |
|
|
|
// enabledOauths = append(enabledOauths, "Twitter")
|
|
|
|
} |
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
// Weibo.
|
|
|
|
// Weibo.
|
|
|
|
if setting.Cfg.MustBool("oauth.weibo", "ENABLED") { |
|
|
|
if setting.Cfg.MustBool("oauth.weibo", "ENABLED") { |
|
|
@ -115,38 +115,25 @@ func NewOauthService() { |
|
|
|
// \/ \/ \/
|
|
|
|
// \/ \/ \/
|
|
|
|
|
|
|
|
|
|
|
|
type SocialGithub struct { |
|
|
|
type SocialGithub struct { |
|
|
|
Token *oauth.Token |
|
|
|
opts *oauth2.Options |
|
|
|
*oauth.Transport |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (s *SocialGithub) Type() int { |
|
|
|
|
|
|
|
return int(models.GITHUB) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func newGitHubOauth(config *oauth.Config) { |
|
|
|
func newGitHubOauth(opts *oauth2.Options) { |
|
|
|
SocialMap["github"] = &SocialGithub{ |
|
|
|
SocialMap["github"] = &SocialGithub{opts} |
|
|
|
Transport: &oauth.Transport{ |
|
|
|
|
|
|
|
Config: config, |
|
|
|
|
|
|
|
Transport: http.DefaultTransport, |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (s *SocialGithub) SetRedirectUrl(url string) { |
|
|
|
func (s *SocialGithub) Type() int { |
|
|
|
s.Transport.Config.RedirectURL = url |
|
|
|
return int(models.GITHUB) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (s *SocialGithub) UserInfo(token *oauth.Token, _ *url.URL) (*BasicUserInfo, error) { |
|
|
|
func (s *SocialGithub) UserInfo(token *oauth2.Token, _ *url.URL) (*BasicUserInfo, error) { |
|
|
|
transport := &oauth.Transport{ |
|
|
|
transport := s.opts.NewTransportFromToken(token) |
|
|
|
Token: token, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
var data struct { |
|
|
|
var data struct { |
|
|
|
Id int `json:"id"` |
|
|
|
Id int `json:"id"` |
|
|
|
Name string `json:"login"` |
|
|
|
Name string `json:"login"` |
|
|
|
Email string `json:"email"` |
|
|
|
Email string `json:"email"` |
|
|
|
} |
|
|
|
} |
|
|
|
var err error |
|
|
|
r, err := transport.Client().Get("https://api.github.com/user") |
|
|
|
r, err := transport.Client().Get(s.Transport.Scope) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
} |
|
|
@ -169,38 +156,25 @@ func (s *SocialGithub) UserInfo(token *oauth.Token, _ *url.URL) (*BasicUserInfo, |
|
|
|
// \/ /_____/ \/
|
|
|
|
// \/ /_____/ \/
|
|
|
|
|
|
|
|
|
|
|
|
type SocialGoogle struct { |
|
|
|
type SocialGoogle struct { |
|
|
|
Token *oauth.Token |
|
|
|
opts *oauth2.Options |
|
|
|
*oauth.Transport |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (s *SocialGoogle) Type() int { |
|
|
|
func (s *SocialGoogle) Type() int { |
|
|
|
return int(models.GOOGLE) |
|
|
|
return int(models.GOOGLE) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func newGoogleOauth(config *oauth.Config) { |
|
|
|
func newGoogleOauth(opts *oauth2.Options) { |
|
|
|
SocialMap["google"] = &SocialGoogle{ |
|
|
|
SocialMap["google"] = &SocialGoogle{opts} |
|
|
|
Transport: &oauth.Transport{ |
|
|
|
|
|
|
|
Config: config, |
|
|
|
|
|
|
|
Transport: http.DefaultTransport, |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (s *SocialGoogle) SetRedirectUrl(url string) { |
|
|
|
|
|
|
|
s.Transport.Config.RedirectURL = url |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (s *SocialGoogle) UserInfo(token *oauth.Token, _ *url.URL) (*BasicUserInfo, error) { |
|
|
|
func (s *SocialGoogle) UserInfo(token *oauth2.Token, _ *url.URL) (*BasicUserInfo, error) { |
|
|
|
transport := &oauth.Transport{Token: token} |
|
|
|
transport := s.opts.NewTransportFromToken(token) |
|
|
|
var data struct { |
|
|
|
var data struct { |
|
|
|
Id string `json:"id"` |
|
|
|
Id string `json:"id"` |
|
|
|
Name string `json:"name"` |
|
|
|
Name string `json:"name"` |
|
|
|
Email string `json:"email"` |
|
|
|
Email string `json:"email"` |
|
|
|
} |
|
|
|
} |
|
|
|
var err error |
|
|
|
r, err := transport.Client().Get("https://www.googleapis.com/userinfo/v2/me") |
|
|
|
|
|
|
|
|
|
|
|
reqUrl := "https://www.googleapis.com/oauth2/v1/userinfo" |
|
|
|
|
|
|
|
r, err := transport.Client().Get(reqUrl) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
} |
|
|
@ -223,62 +197,35 @@ func (s *SocialGoogle) UserInfo(token *oauth.Token, _ *url.URL) (*BasicUserInfo, |
|
|
|
// \__> \__>
|
|
|
|
// \__> \__>
|
|
|
|
|
|
|
|
|
|
|
|
type SocialTencent struct { |
|
|
|
type SocialTencent struct { |
|
|
|
Token *oauth.Token |
|
|
|
opts *oauth2.Options |
|
|
|
*oauth.Transport |
|
|
|
} |
|
|
|
reqUrl string |
|
|
|
|
|
|
|
|
|
|
|
func newTencentOauth(opts *oauth2.Options) { |
|
|
|
|
|
|
|
SocialMap["qq"] = &SocialTencent{opts} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (s *SocialTencent) Type() int { |
|
|
|
func (s *SocialTencent) Type() int { |
|
|
|
return int(models.QQ) |
|
|
|
return int(models.QQ) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func newTencentOauth(config *oauth.Config) { |
|
|
|
func (s *SocialTencent) UserInfo(token *oauth2.Token, URL *url.URL) (*BasicUserInfo, error) { |
|
|
|
SocialMap["qq"] = &SocialTencent{ |
|
|
|
r, err := http.Get("https://graph.z.qq.com/moc2/me?access_token=" + url.QueryEscape(token.AccessToken)) |
|
|
|
reqUrl: "https://open.t.qq.com/api/user/info", |
|
|
|
if err != nil { |
|
|
|
Transport: &oauth.Transport{ |
|
|
|
return nil, err |
|
|
|
Config: config, |
|
|
|
|
|
|
|
Transport: http.DefaultTransport, |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
defer r.Body.Close() |
|
|
|
|
|
|
|
|
|
|
|
func (s *SocialTencent) SetRedirectUrl(url string) { |
|
|
|
|
|
|
|
s.Transport.Config.RedirectURL = url |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (s *SocialTencent) UserInfo(token *oauth.Token, URL *url.URL) (*BasicUserInfo, error) { |
|
|
|
body, err := ioutil.ReadAll(r.Body) |
|
|
|
var data struct { |
|
|
|
|
|
|
|
Data struct { |
|
|
|
|
|
|
|
Id string `json:"openid"` |
|
|
|
|
|
|
|
Name string `json:"name"` |
|
|
|
|
|
|
|
Email string `json:"email"` |
|
|
|
|
|
|
|
} `json:"data"` |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
var err error |
|
|
|
|
|
|
|
// https://open.t.qq.com/api/user/info?
|
|
|
|
|
|
|
|
//oauth_consumer_key=APP_KEY&
|
|
|
|
|
|
|
|
//access_token=ACCESSTOKEN&openid=openid
|
|
|
|
|
|
|
|
//clientip=CLIENTIP&oauth_version=2.a
|
|
|
|
|
|
|
|
//scope=all
|
|
|
|
|
|
|
|
var urls = url.Values{ |
|
|
|
|
|
|
|
"oauth_consumer_key": {s.Transport.Config.ClientId}, |
|
|
|
|
|
|
|
"access_token": {token.AccessToken}, |
|
|
|
|
|
|
|
"openid": URL.Query()["openid"], |
|
|
|
|
|
|
|
"oauth_version": {"2.a"}, |
|
|
|
|
|
|
|
"scope": {"all"}, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
r, err := http.Get(s.reqUrl + "?" + urls.Encode()) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
} |
|
|
|
defer r.Body.Close() |
|
|
|
vals, err := url.ParseQuery(string(body)) |
|
|
|
if err = json.NewDecoder(r.Body).Decode(&data); err != nil { |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return &BasicUserInfo{ |
|
|
|
return &BasicUserInfo{ |
|
|
|
Identity: data.Data.Id, |
|
|
|
Identity: vals.Get("openid"), |
|
|
|
Name: data.Data.Name, |
|
|
|
|
|
|
|
Email: data.Data.Email, |
|
|
|
|
|
|
|
}, nil |
|
|
|
}, nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -289,54 +236,54 @@ func (s *SocialTencent) UserInfo(token *oauth.Token, URL *url.URL) (*BasicUserIn |
|
|
|
// |____| \/\_/ |__||__| |__| \___ >__|
|
|
|
|
// |____| \/\_/ |__||__| |__| \___ >__|
|
|
|
|
// \/
|
|
|
|
// \/
|
|
|
|
|
|
|
|
|
|
|
|
type SocialTwitter struct { |
|
|
|
// type SocialTwitter struct {
|
|
|
|
Token *oauth.Token |
|
|
|
// Token *oauth2.Token
|
|
|
|
*oauth.Transport |
|
|
|
// *oauth2.Transport
|
|
|
|
} |
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
func (s *SocialTwitter) Type() int { |
|
|
|
// func (s *SocialTwitter) Type() int {
|
|
|
|
return int(models.TWITTER) |
|
|
|
// return int(models.TWITTER)
|
|
|
|
} |
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
func newTwitterOauth(config *oauth.Config) { |
|
|
|
// func newTwitterOauth(config *oauth2.Config) {
|
|
|
|
SocialMap["twitter"] = &SocialTwitter{ |
|
|
|
// SocialMap["twitter"] = &SocialTwitter{
|
|
|
|
Transport: &oauth.Transport{ |
|
|
|
// Transport: &oauth.Transport{
|
|
|
|
Config: config, |
|
|
|
// Config: config,
|
|
|
|
Transport: http.DefaultTransport, |
|
|
|
// Transport: http.DefaultTransport,
|
|
|
|
}, |
|
|
|
// },
|
|
|
|
} |
|
|
|
// }
|
|
|
|
} |
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
func (s *SocialTwitter) SetRedirectUrl(url string) { |
|
|
|
// func (s *SocialTwitter) SetRedirectUrl(url string) {
|
|
|
|
s.Transport.Config.RedirectURL = url |
|
|
|
// s.Transport.Config.RedirectURL = url
|
|
|
|
} |
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
//https://github.com/mrjones/oauth
|
|
|
|
// //https://github.com/mrjones/oauth
|
|
|
|
func (s *SocialTwitter) UserInfo(token *oauth.Token, _ *url.URL) (*BasicUserInfo, error) { |
|
|
|
// func (s *SocialTwitter) UserInfo(token *oauth2.Token, _ *url.URL) (*BasicUserInfo, error) {
|
|
|
|
// transport := &oauth.Transport{Token: token}
|
|
|
|
// // transport := &oauth.Transport{Token: token}
|
|
|
|
// var data struct {
|
|
|
|
// // var data struct {
|
|
|
|
// Id string `json:"id"`
|
|
|
|
// // Id string `json:"id"`
|
|
|
|
// Name string `json:"name"`
|
|
|
|
// // Name string `json:"name"`
|
|
|
|
// Email string `json:"email"`
|
|
|
|
// // Email string `json:"email"`
|
|
|
|
// }
|
|
|
|
// // }
|
|
|
|
// var err error
|
|
|
|
// // var err error
|
|
|
|
|
|
|
|
|
|
|
|
// reqUrl := "https://www.googleapis.com/oauth2/v1/userinfo"
|
|
|
|
// // reqUrl := "https://www.googleapis.com/oauth2/v1/userinfo"
|
|
|
|
// r, err := transport.Client().Get(reqUrl)
|
|
|
|
// // r, err := transport.Client().Get(reqUrl)
|
|
|
|
// if err != nil {
|
|
|
|
// // if err != nil {
|
|
|
|
// return nil, err
|
|
|
|
// // return nil, err
|
|
|
|
// }
|
|
|
|
// // }
|
|
|
|
// defer r.Body.Close()
|
|
|
|
// // defer r.Body.Close()
|
|
|
|
// if err = json.NewDecoder(r.Body).Decode(&data); err != nil {
|
|
|
|
// // if err = json.NewDecoder(r.Body).Decode(&data); err != nil {
|
|
|
|
// return nil, err
|
|
|
|
// // return nil, err
|
|
|
|
// }
|
|
|
|
// // }
|
|
|
|
// return &BasicUserInfo{
|
|
|
|
// // return &BasicUserInfo{
|
|
|
|
// Identity: data.Id,
|
|
|
|
// // Identity: data.Id,
|
|
|
|
// Name: data.Name,
|
|
|
|
// // Name: data.Name,
|
|
|
|
// Email: data.Email,
|
|
|
|
// // Email: data.Email,
|
|
|
|
// }, nil
|
|
|
|
// // }, nil
|
|
|
|
return nil, nil |
|
|
|
// return nil, nil
|
|
|
|
} |
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
// __ __ ._____.
|
|
|
|
// __ __ ._____.
|
|
|
|
// / \ / \ ____ |__\_ |__ ____
|
|
|
|
// / \ / \ ____ |__\_ |__ ____
|
|
|
@ -346,37 +293,25 @@ func (s *SocialTwitter) UserInfo(token *oauth.Token, _ *url.URL) (*BasicUserInfo |
|
|
|
// \/ \/ \/
|
|
|
|
// \/ \/ \/
|
|
|
|
|
|
|
|
|
|
|
|
type SocialWeibo struct { |
|
|
|
type SocialWeibo struct { |
|
|
|
Token *oauth.Token |
|
|
|
opts *oauth2.Options |
|
|
|
*oauth.Transport |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (s *SocialWeibo) Type() int { |
|
|
|
func newWeiboOauth(opts *oauth2.Options) { |
|
|
|
return int(models.WEIBO) |
|
|
|
SocialMap["weibo"] = &SocialWeibo{opts} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func newWeiboOauth(config *oauth.Config) { |
|
|
|
func (s *SocialWeibo) Type() int { |
|
|
|
SocialMap["weibo"] = &SocialWeibo{ |
|
|
|
return int(models.WEIBO) |
|
|
|
Transport: &oauth.Transport{ |
|
|
|
|
|
|
|
Config: config, |
|
|
|
|
|
|
|
Transport: http.DefaultTransport, |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (s *SocialWeibo) SetRedirectUrl(url string) { |
|
|
|
|
|
|
|
s.Transport.Config.RedirectURL = url |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (s *SocialWeibo) UserInfo(token *oauth.Token, _ *url.URL) (*BasicUserInfo, error) { |
|
|
|
func (s *SocialWeibo) UserInfo(token *oauth2.Token, _ *url.URL) (*BasicUserInfo, error) { |
|
|
|
transport := &oauth.Transport{Token: token} |
|
|
|
transport := s.opts.NewTransportFromToken(token) |
|
|
|
var data struct { |
|
|
|
var data struct { |
|
|
|
Name string `json:"name"` |
|
|
|
Name string `json:"name"` |
|
|
|
} |
|
|
|
} |
|
|
|
var err error |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var urls = url.Values{ |
|
|
|
var urls = url.Values{ |
|
|
|
"access_token": {token.AccessToken}, |
|
|
|
"access_token": {token.AccessToken}, |
|
|
|
"uid": {token.Extra["id_token"]}, |
|
|
|
"uid": {token.Extra("uid")}, |
|
|
|
} |
|
|
|
} |
|
|
|
reqUrl := "https://api.weibo.com/2/users/show.json" |
|
|
|
reqUrl := "https://api.weibo.com/2/users/show.json" |
|
|
|
r, err := transport.Client().Get(reqUrl + "?" + urls.Encode()) |
|
|
|
r, err := transport.Client().Get(reqUrl + "?" + urls.Encode()) |
|
|
@ -384,11 +319,12 @@ func (s *SocialWeibo) UserInfo(token *oauth.Token, _ *url.URL) (*BasicUserInfo, |
|
|
|
return nil, err |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
} |
|
|
|
defer r.Body.Close() |
|
|
|
defer r.Body.Close() |
|
|
|
|
|
|
|
|
|
|
|
if err = json.NewDecoder(r.Body).Decode(&data); err != nil { |
|
|
|
if err = json.NewDecoder(r.Body).Decode(&data); err != nil { |
|
|
|
return nil, err |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
} |
|
|
|
return &BasicUserInfo{ |
|
|
|
return &BasicUserInfo{ |
|
|
|
Identity: token.Extra["id_token"], |
|
|
|
Identity: token.Extra("uid"), |
|
|
|
Name: data.Name, |
|
|
|
Name: data.Name, |
|
|
|
}, nil |
|
|
|
}, nil |
|
|
|
} |
|
|
|
} |
|
|
|