You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
1.2 KiB
37 lines
1.2 KiB
package gitee
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/dtapps/go-library/utils/gojson"
|
|
"github.com/dtapps/go-library/utils/gorequest"
|
|
"net/http"
|
|
)
|
|
|
|
type OauthTokenResponse struct {
|
|
AccessToken string `json:"access_token"`
|
|
}
|
|
|
|
type OauthTokenResult struct {
|
|
Result OauthTokenResponse // 结果
|
|
Body []byte // 内容
|
|
Http gorequest.Response // 请求
|
|
Err error // 错误
|
|
}
|
|
|
|
func newOauthTokenResult(result OauthTokenResponse, body []byte, http gorequest.Response, err error) *OauthTokenResult {
|
|
return &OauthTokenResult{Result: result, Body: body, Http: http, Err: err}
|
|
}
|
|
|
|
// OauthToken OAuth2 获取 AccessToken 认证步骤
|
|
// https://gitee.com/api/v5/oauth_doc#/list-item-2
|
|
func (c *Client) OauthToken(code string) *OauthTokenResult {
|
|
// 参数
|
|
params := gorequest.NewParamsWith()
|
|
// 请求
|
|
request, err := c.request(apiUrl+fmt.Sprintf("/oauth/token?grant_type=authorization_code&code=%s&client_id=%s&redirect_uri=%s&client_secret=%s", code, c.config.ClientID, c.config.RedirectUri, c.config.ClientSecret), params, http.MethodPost)
|
|
// 定义
|
|
var response OauthTokenResponse
|
|
err = gojson.Unmarshal(request.ResponseBody, &response)
|
|
return newOauthTokenResult(response, request.ResponseBody, request, err)
|
|
}
|