core.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. package main
  2. import (
  3. "github.com/gin-contrib/sessions"
  4. "github.com/gin-gonic/gin"
  5. "os"
  6. "fmt"
  7. "bytes"
  8. "encoding/json"
  9. "io/ioutil"
  10. //"strings"
  11. "net/http"
  12. "net/url"
  13. //"reflect"
  14. )
  15. var port = "8081"
  16. var host = "http://passport.vogue.com.cn"
  17. var appkey = ""
  18. var appsecret = ""
  19. const scope = "all"
  20. const authorizeURL = "https://api.weibo.com/oauth2/authorize"
  21. const tokenURL = "https://api.weibo.com/oauth2/access_token"
  22. const grantType = "authorization_code"
  23. const userURL = "https://api.weibo.com/2/users/show.json"
  24. type Token struct {
  25. Access_token string `json:"access_token"`
  26. Expires_in int `json:"expires_in"`
  27. Remind_in string `json:"remind_in"`
  28. Uid string `json:"uid"`
  29. //Data map[string]interface{} `json:data`
  30. }
  31. func main() {
  32. gin.SetMode(gin.ReleaseMode)//gin.ReleaseMode gin.DebugMode
  33. router := gin.Default()
  34. router.LoadHTMLGlob("templates/*")
  35. store := sessions.NewCookieStore([]byte("secret"))
  36. router.Use(sessions.Sessions("oauth2", store))
  37. //router.Use(Core)
  38. router.GET("/", Index_get)
  39. router.GET("/api", Api_get)
  40. router.GET("/token", Token_get)
  41. router.GET("/authorize", Authorize_get)
  42. router.GET("/callback", Callback_get)
  43. //router.POST("/simple/server/post", PostHandler)
  44. //router.PUT("/simple/server/put", PutHandler)
  45. //router.DELETE("/simple/server/delete", DeleteHandler)
  46. router.StaticFS("/static", http.Dir("static"))
  47. envport := os.Getenv("OAUTHPORT")
  48. envurl := os.Getenv("OAUTHHOST")
  49. if envurl != "" {
  50. host = "http://" + envurl
  51. }
  52. if envport != "" {
  53. port = envport
  54. }
  55. appkey = os.Getenv("APPKEY")
  56. appsecret = os.Getenv("APPSECRET")
  57. router.Run(":" + port)
  58. }
  59. func Core(c *gin.Context) {
  60. return
  61. }
  62. func test(c *gin.Context) {
  63. fmt.Println("test")
  64. }
  65. func Index_get(c *gin.Context) {
  66. session := sessions.Default(c)
  67. access_token := session.Get("access_token")
  68. uid := session.Get("uid")
  69. if access_token == nil || uid == nil {
  70. c.Redirect(http.StatusMovedPermanently, "authorize?refer=index")
  71. return
  72. }
  73. c.HTML(http.StatusOK, "index.html", gin.H{
  74. "access_token": access_token,
  75. "uid": uid,
  76. })
  77. }
  78. func Token_get(c *gin.Context) {
  79. session := sessions.Default(c)
  80. access_token := session.Get("access_token")
  81. uid := session.Get("uid")
  82. if access_token == nil || uid == nil {
  83. c.Redirect(http.StatusMovedPermanently, "authorize?refer=token")
  84. return
  85. }
  86. c.JSON(http.StatusOK, gin.H{"token": access_token, "uid" : uid})
  87. }
  88. func Api_get(c *gin.Context) {
  89. apiUrl := c.Query("url")
  90. session := sessions.Default(c)
  91. access_token := session.Get("access_token").(string)
  92. uid := session.Get("uid").(string)
  93. buffer := bytes.Buffer{}
  94. buffer.WriteString(apiUrl)
  95. buffer.WriteString("?access_token=")
  96. buffer.WriteString(access_token)
  97. buffer.WriteString("&uid=")
  98. buffer.WriteString(uid)
  99. body, err := get(buffer.String())
  100. if err == "no" {
  101. c.String(http.StatusOK, "error")
  102. return
  103. }
  104. //var data map[string]interface{}
  105. //json.Unmarshal(body, &data)
  106. c.HTML(http.StatusOK, "api.html", gin.H{
  107. "json": string(body),
  108. })
  109. }
  110. func Authorize_get(c *gin.Context) {
  111. referUrl := c.Query("refer")
  112. redirectURL := host + ":" + port + "/callback?refer=" + referUrl
  113. redirect := bytes.Buffer{}
  114. redirect.WriteString(authorizeURL)
  115. redirect.WriteString("?client_id=")
  116. redirect.WriteString(appkey)
  117. redirect.WriteString("&redirect_uri=")
  118. redirect.WriteString(redirectURL)
  119. redirect.WriteString("&response_type=code")
  120. //c.String(http.StatusOK, "正在为您跳转到微博进行授权登录...")
  121. c.Redirect(http.StatusMovedPermanently, redirect.String())
  122. }
  123. func Callback_get(c *gin.Context) {
  124. redirectURL := host + ":" + port + "/callback"
  125. code := c.Query("code")
  126. referUrl := c.Query("refer")
  127. body, err := post(tokenURL, url.Values{"client_id": {appkey}, "client_secret": {appsecret}, "grant_type": {grantType}, "code": {code}, "redirect_uri": {redirectURL}})
  128. if err == "no" {
  129. c.String(http.StatusOK, "error")
  130. return
  131. }
  132. //fmt.Println(string(body))
  133. token := Token{}
  134. json.Unmarshal(body, &token)
  135. session := sessions.Default(c)
  136. session.Set("access_token", token.Access_token)
  137. session.Set("uid", token.Uid)
  138. session.Set("expires_in", token.Expires_in)
  139. session.Set("remind_in", token.Remind_in)
  140. session.Save()
  141. indexUrl := host + ":" + port + "/"
  142. if referUrl == "token" {
  143. indexUrl = indexUrl + "token"
  144. }
  145. c.Redirect(http.StatusMovedPermanently, indexUrl)
  146. }
  147. //param map[string][]string
  148. func post(url string, param url.Values) ([]byte, string) {
  149. error := []byte{}
  150. resp, err := http.PostForm(url, param)
  151. if err != nil || resp.StatusCode != http.StatusOK {
  152. return error, "no"
  153. }
  154. defer resp.Body.Close()
  155. body, err := ioutil.ReadAll(resp.Body)
  156. if err != nil {
  157. return error, "no"
  158. }
  159. //fmt.Println("type:", reflect.TypeOf(body))
  160. return body, "yes"
  161. //data = map[string]interface{}
  162. //return json.Unmarshal(body, &data)
  163. }
  164. func get(url string) ([]byte, string) {
  165. error := []byte{}
  166. resp, err := http.Get(url)
  167. if err != nil || resp.StatusCode != http.StatusOK {
  168. return error, "no"
  169. }
  170. defer resp.Body.Close()
  171. body, err := ioutil.ReadAll(resp.Body)
  172. if err != nil {
  173. return error, "no"
  174. }
  175. return body, "yes"
  176. }