[Go] 新浪微博SDK golang实现(目前只完成OAuth认证以及发送文字微博以及带图片的微博) →→→→→进入此内容的聊天室

来自 , 2021-03-10, 写在 Go, 查看 112 次.
URL http://www.code666.cn/view/2ed08286
  1. /*
  2. 新浪微博登录
  3. */
  4.  
  5. package sina
  6.  
  7. import (
  8.         "encoding/json"
  9.         "net/http"
  10.         "net/url"
  11. )
  12.  
  13. /*
  14. Sina API author
  15. */
  16. type SaeTOAuth struct {
  17.         clientId     string
  18.         clientSecret string
  19. }
  20.  
  21. type AccessToken struct {
  22.         Access_Token string `json:access_token`
  23.         Remind_In    string `json:remind_in`
  24.         Expires_In   int    `json:expires_in`
  25.         Uid          string `json:uid`
  26. }
  27.  
  28. const (
  29.         SINA_OAUTH_API_URL = "https://api.weibo.com/oauth2"
  30. )
  31.  
  32. func NewSaeTOAuth(clientID, clientSecret string) *SaeTOAuth {
  33.         return &SaeTOAuth{clientID, clientSecret}
  34. }
  35.  
  36. func (s *SaeTOAuth) GetAuthorizeURL(redirect_url, response_type, state, display string) string {
  37.         if response_type == "" {
  38.                 response_type = "code"
  39.         }
  40.         if display == "" {
  41.                 display = "default"
  42.         }
  43.         v := url.Values{}
  44.         v.Add("client_id", s.clientId)
  45.         v.Add("redirect_uri", redirect_url)
  46.         v.Add("response_type", response_type)
  47.         if state != "" {
  48.                 v.Add("state", state)
  49.         }
  50.         v.Add("display", display)
  51.  
  52.         params := v.Encode()
  53.  
  54.         return SINA_OAUTH_API_URL + "/authorize?" + params
  55. }
  56.  
  57. func (s *SaeTOAuth) GetAccessToken(grant_type string, keys map[string]string) (AccessToken, error) {
  58.         v := url.Values{}
  59.         v.Add("client_id", s.clientId)
  60.         v.Add("client_secret", s.clientSecret)
  61.         if grant_type == "code" {
  62.                 v.Add("grant_type", "authorization_code")
  63.                 v.Add("code", keys["code"])
  64.                 v.Add("redirect_uri", keys["redirect_uri"])
  65.         } else if grant_type == "token" {
  66.                 v.Add("grant_type", "refresh_token")
  67.                 v.Add("refresh_token", keys["refresh_token"])
  68.         } else if grant_type == "password" {
  69.                 v.Add("grant_type", "password")
  70.                 v.Add("username", keys["username"])
  71.                 v.Add("password", keys["password"])
  72.         }
  73.  
  74.         accessTokenUrl := SINA_OAUTH_API_URL + "/access_token"
  75.  
  76.         response, err := http.PostForm(accessTokenUrl, v)
  77.  
  78.         if err != nil {
  79.                 return AccessToken{}, err
  80.         }
  81.         defer response.Body.Close()
  82.         jsonMap := AccessToken{}
  83.         json.NewDecoder(response.Body).Decode(&jsonMap)
  84.         return jsonMap, nil
  85.  
  86. }
  87.  
  88.  
  89. //go/5414

回复 " 新浪微博SDK golang实现(目前只完成OAuth认证以及发送文字微博以及带图片的微博)"

这儿你可以回复上面这条便签

captcha