|
@@ -0,0 +1,193 @@
|
|
|
+package main
|
|
|
+import (
|
|
|
+ "github.com/gin-contrib/sessions"
|
|
|
+ "github.com/gin-gonic/gin"
|
|
|
+ "os"
|
|
|
+ "fmt"
|
|
|
+ "bytes"
|
|
|
+ "encoding/json"
|
|
|
+ "io/ioutil"
|
|
|
+
|
|
|
+ "net/http"
|
|
|
+ "net/url"
|
|
|
+
|
|
|
+)
|
|
|
+var port = "8081"
|
|
|
+var host = "http://passport.vogue.com.cn"
|
|
|
+var appkey = ""
|
|
|
+var appsecret = ""
|
|
|
+const scope = "all"
|
|
|
+const authorizeURL = "https://api.weibo.com/oauth2/authorize"
|
|
|
+const tokenURL = "https://api.weibo.com/oauth2/access_token"
|
|
|
+const grantType = "authorization_code"
|
|
|
+const userURL = "https://api.weibo.com/2/users/show.json"
|
|
|
+
|
|
|
+type Token struct {
|
|
|
+ Access_token string `json:"access_token"`
|
|
|
+ Expires_in int `json:"expires_in"`
|
|
|
+ Remind_in string `json:"remind_in"`
|
|
|
+ Uid string `json:"uid"`
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+func main() {
|
|
|
+ gin.SetMode(gin.ReleaseMode)
|
|
|
+ router := gin.Default()
|
|
|
+ router.LoadHTMLGlob("templates/*")
|
|
|
+ store := sessions.NewCookieStore([]byte("secret"))
|
|
|
+ router.Use(sessions.Sessions("oauth2", store))
|
|
|
+
|
|
|
+ router.GET("/", Index_get)
|
|
|
+ router.GET("/api", Api_get)
|
|
|
+ router.GET("/token", Token_get)
|
|
|
+ router.GET("/authorize", Authorize_get)
|
|
|
+ router.GET("/callback", Callback_get)
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ router.StaticFS("/static", http.Dir("static"))
|
|
|
+ envport := os.Getenv("OAUTHPORT")
|
|
|
+ envurl := os.Getenv("OAUTHHOST")
|
|
|
+ if envurl != "" {
|
|
|
+ host = "http://" + envurl
|
|
|
+ }
|
|
|
+ if envport != "" {
|
|
|
+ port = envport
|
|
|
+ }
|
|
|
+
|
|
|
+ appkey = os.Getenv("APPKEY")
|
|
|
+ appsecret = os.Getenv("APPSECRET")
|
|
|
+
|
|
|
+ router.Run(":" + port)
|
|
|
+}
|
|
|
+
|
|
|
+func Core(c *gin.Context) {
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+func test(c *gin.Context) {
|
|
|
+ fmt.Println("test")
|
|
|
+}
|
|
|
+
|
|
|
+func Index_get(c *gin.Context) {
|
|
|
+ session := sessions.Default(c)
|
|
|
+ access_token := session.Get("access_token")
|
|
|
+ uid := session.Get("uid")
|
|
|
+ if access_token == nil || uid == nil {
|
|
|
+ c.Redirect(http.StatusMovedPermanently, "authorize?refer=index")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ c.HTML(http.StatusOK, "index.html", gin.H{
|
|
|
+ "access_token": access_token,
|
|
|
+ "uid": uid,
|
|
|
+ })
|
|
|
+}
|
|
|
+
|
|
|
+func Token_get(c *gin.Context) {
|
|
|
+ session := sessions.Default(c)
|
|
|
+ access_token := session.Get("access_token")
|
|
|
+ uid := session.Get("uid")
|
|
|
+ if access_token == nil || uid == nil {
|
|
|
+ c.Redirect(http.StatusMovedPermanently, "authorize?refer=token")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ c.JSON(http.StatusOK, gin.H{"token": access_token, "uid" : uid})
|
|
|
+}
|
|
|
+
|
|
|
+func Api_get(c *gin.Context) {
|
|
|
+ apiUrl := c.Query("url")
|
|
|
+ session := sessions.Default(c)
|
|
|
+ access_token := session.Get("access_token").(string)
|
|
|
+ uid := session.Get("uid").(string)
|
|
|
+ buffer := bytes.Buffer{}
|
|
|
+ buffer.WriteString(apiUrl)
|
|
|
+ buffer.WriteString("?access_token=")
|
|
|
+ buffer.WriteString(access_token)
|
|
|
+ buffer.WriteString("&uid=")
|
|
|
+ buffer.WriteString(uid)
|
|
|
+ body, err := get(buffer.String())
|
|
|
+ if err == "no" {
|
|
|
+ c.String(http.StatusOK, "error")
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ c.HTML(http.StatusOK, "api.html", gin.H{
|
|
|
+ "json": string(body),
|
|
|
+ })
|
|
|
+}
|
|
|
+
|
|
|
+func Authorize_get(c *gin.Context) {
|
|
|
+ referUrl := c.Query("refer")
|
|
|
+ redirectURL := host + ":" + port + "/callback?refer=" + referUrl
|
|
|
+ redirect := bytes.Buffer{}
|
|
|
+ redirect.WriteString(authorizeURL)
|
|
|
+ redirect.WriteString("?client_id=")
|
|
|
+ redirect.WriteString(appkey)
|
|
|
+ redirect.WriteString("&redirect_uri=")
|
|
|
+ redirect.WriteString(redirectURL)
|
|
|
+ redirect.WriteString("&response_type=code")
|
|
|
+
|
|
|
+ c.Redirect(http.StatusMovedPermanently, redirect.String())
|
|
|
+}
|
|
|
+
|
|
|
+func Callback_get(c *gin.Context) {
|
|
|
+ redirectURL := host + ":" + port + "/callback"
|
|
|
+ code := c.Query("code")
|
|
|
+ referUrl := c.Query("refer")
|
|
|
+ body, err := post(tokenURL, url.Values{"client_id": {appkey}, "client_secret": {appsecret}, "grant_type": {grantType}, "code": {code}, "redirect_uri": {redirectURL}})
|
|
|
+ if err == "no" {
|
|
|
+ c.String(http.StatusOK, "error")
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ token := Token{}
|
|
|
+ json.Unmarshal(body, &token)
|
|
|
+ session := sessions.Default(c)
|
|
|
+ session.Set("access_token", token.Access_token)
|
|
|
+ session.Set("uid", token.Uid)
|
|
|
+ session.Set("expires_in", token.Expires_in)
|
|
|
+ session.Set("remind_in", token.Remind_in)
|
|
|
+ session.Save()
|
|
|
+ indexUrl := host + ":" + port + "/"
|
|
|
+ if referUrl == "token" {
|
|
|
+ indexUrl = indexUrl + "token"
|
|
|
+ }
|
|
|
+
|
|
|
+ c.Redirect(http.StatusMovedPermanently, indexUrl)
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+func post(url string, param url.Values) ([]byte, string) {
|
|
|
+ error := []byte{}
|
|
|
+ resp, err := http.PostForm(url, param)
|
|
|
+ if err != nil || resp.StatusCode != http.StatusOK {
|
|
|
+ return error, "no"
|
|
|
+ }
|
|
|
+ defer resp.Body.Close()
|
|
|
+
|
|
|
+ body, err := ioutil.ReadAll(resp.Body)
|
|
|
+ if err != nil {
|
|
|
+ return error, "no"
|
|
|
+ }
|
|
|
+
|
|
|
+ return body, "yes"
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+func get(url string) ([]byte, string) {
|
|
|
+ error := []byte{}
|
|
|
+ resp, err := http.Get(url)
|
|
|
+ if err != nil || resp.StatusCode != http.StatusOK {
|
|
|
+ return error, "no"
|
|
|
+ }
|
|
|
+ defer resp.Body.Close()
|
|
|
+
|
|
|
+ body, err := ioutil.ReadAll(resp.Body)
|
|
|
+ if err != nil {
|
|
|
+ return error, "no"
|
|
|
+ }
|
|
|
+ return body, "yes"
|
|
|
+}
|