larksuite / oapi-sdk-go

larksuite oapi sdk by golang
MIT License
430 stars 87 forks source link

扫码登录获取用户信息是否支持该sdk #81

Open hq2333 opened 2 years ago

hq2333 commented 2 years ago

扫码登录获取用户信息的接口是https://passport.feishu.cn/suite/passport/oauth/token 对于该接口,可以使用oapi-sdk的原生API调用方式吗

hzgcoding commented 2 years ago

好像不能 我试过了

zhailuxubyte commented 2 years ago

好像不能 我试过了

这个接口不是服务端sdk接口哈,是js-sdk,参考:https://open.feishu.cn/document/common-capabilities/sso/web-application-sso/qr-sdk-documentation

thelstcn commented 2 years ago
    type AuthTokenV1Body struct {
        GrantType string `json:"grant_type"`
        Code      string `json:"code"`
    }
    type OauthTokenBody struct {
        AuthTokenV1Body
        ClientId     string `json:"client_id"`
        ClientSecret string `json:"client_secret"`
        RedirectUri  string `json:"redirect_uri"`
    }
    passportGroup.GET("/callback", func(c *gin.Context) {
        code := c.Query("code")
        body := OauthTokenBody{
            ClientId:     app.AppID,
            ClientSecret: app.AppSecret,
            RedirectUri:  "https://" + c.Request.Host + c.Request.URL.Path,
            AuthTokenV1Body: AuthTokenV1Body{
                GrantType: "authorization_code",
                Code:      code,
            },
        }
        resp, err := client.Do(context.Background(), &larkcore.ApiReq{
            HttpMethod:                http.MethodPost,
            ApiPath:                   "https://passport.feishu.cn/suite/passport/oauth/token",
            Body:                      body,
            SupportedAccessTokenTypes: []larkcore.AccessTokenType{larkcore.AccessTokenTypeNone},
        })
        fmt.Println(string(resp.RawBody), err)
        type OauthTokenResponseBody struct {
            AccessToken      string `json:"access_token"`
            RefreshToken     string `json:"refresh_token"`
            TokenType        string `json:"token_type"`
            ExpiresIn        int    `json:"expires_in"`
            RefreshExpiresIn int    `json:"refresh_expires_in"`
        }
        var responseBody OauthTokenResponseBody
        err = json.Unmarshal(resp.RawBody, &responseBody)
        if err != nil {
            fmt.Println(err)
        }
        resp, err = client.Do(context.Background(), &larkcore.ApiReq{
            HttpMethod:                http.MethodGet,
            ApiPath:                   "https://passport.feishu.cn/suite/passport/oauth/userinfo",
            SupportedAccessTokenTypes: []larkcore.AccessTokenType{larkcore.AccessTokenTypeUser},
        }, larkcore.WithUserAccessToken(responseBody.AccessToken))
        if err != nil {
            fmt.Println(err)
        }
        fmt.Println(string(resp.RawBody))
       })

测试可行,忽略我代码质量。

Pan-wh commented 1 year ago
  type AuthTokenV1Body struct {
      GrantType string `json:"grant_type"`
      Code      string `json:"code"`
  }
  type OauthTokenBody struct {
      AuthTokenV1Body
      ClientId     string `json:"client_id"`
      ClientSecret string `json:"client_secret"`
      RedirectUri  string `json:"redirect_uri"`
  }
  passportGroup.GET("/callback", func(c *gin.Context) {
      code := c.Query("code")
      body := OauthTokenBody{
          ClientId:     app.AppID,
          ClientSecret: app.AppSecret,
          RedirectUri:  "https://" + c.Request.Host + c.Request.URL.Path,
          AuthTokenV1Body: AuthTokenV1Body{
              GrantType: "authorization_code",
              Code:      code,
          },
      }
      resp, err := client.Do(context.Background(), &larkcore.ApiReq{
          HttpMethod:                http.MethodPost,
          ApiPath:                   "https://passport.feishu.cn/suite/passport/oauth/token",
          Body:                      body,
          SupportedAccessTokenTypes: []larkcore.AccessTokenType{larkcore.AccessTokenTypeNone},
      })
      fmt.Println(string(resp.RawBody), err)
      type OauthTokenResponseBody struct {
          AccessToken      string `json:"access_token"`
          RefreshToken     string `json:"refresh_token"`
          TokenType        string `json:"token_type"`
          ExpiresIn        int    `json:"expires_in"`
          RefreshExpiresIn int    `json:"refresh_expires_in"`
      }
      var responseBody OauthTokenResponseBody
      err = json.Unmarshal(resp.RawBody, &responseBody)
      if err != nil {
          fmt.Println(err)
      }
      resp, err = client.Do(context.Background(), &larkcore.ApiReq{
          HttpMethod:                http.MethodGet,
          ApiPath:                   "https://passport.feishu.cn/suite/passport/oauth/userinfo",
          SupportedAccessTokenTypes: []larkcore.AccessTokenType{larkcore.AccessTokenTypeUser},
      }, larkcore.WithUserAccessToken(responseBody.AccessToken))
      if err != nil {
          fmt.Println(err)
      }
      fmt.Println(string(resp.RawBody))
       })

测试可行,忽略我代码质量。

你好,请问能发我参考一下吗,最近要给自己后台接入飞书扫码登录