diff --git a/sdk/api_authentication_caller.go b/sdk/api_authentication_caller.go index b675d51..a8be41a 100644 --- a/sdk/api_authentication_caller.go +++ b/sdk/api_authentication_caller.go @@ -41,13 +41,13 @@ func (client *DingTalkClient) GetUserInfoFromAdmin(code string) (GetUserInfoFrom //服务端通过临时授权码获取授权用户的个人信息 //https://open-doc.dingtalk.com/microapp/serverapi3/vmzkak -func (s *DingTalkSDK) GetUserInfoByCode(code string) (GetUserInfoByCodeResp, error) { +func (client *DingTalkOauthClient) GetUserInfoByCode(code string) (GetUserInfoByCodeResp, error) { params := map[string]string{ "tmp_auth_code": code, } paramsJson, _ := json.ToJson(params) - - body, err := ExcuteOapi("https://oapi.dingtalk.com/sns/getuserinfo_bycode", s.SuiteKey, s.SuiteSecret, "_", "", paramsJson) + + body, err := ExcuteOapi("https://oapi.dingtalk.com/sns/getuserinfo_bycode", client.OauthAppId, client.OauthAppSecret, paramsJson) resp := GetUserInfoByCodeResp{} if err != nil { return resp, err diff --git a/sdk/api_authentication_test.go b/sdk/api_authentication_test.go index ca451ac..ba7f542 100644 --- a/sdk/api_authentication_test.go +++ b/sdk/api_authentication_test.go @@ -16,3 +16,9 @@ func TestGetUserInfoFromAdmin(t *testing.T) { t.Log(json.ToJson(resp)) t.Log(err) } + +func TestDingTalkSDK_GetUserInfoByCode(t *testing.T) { + resp, err := NewDingTalkOauthClient().GetUserInfoByCode("492b19afde343b559cc53f9b096adfe1") + t.Log(json.ToJson(resp)) + t.Log(err) +} diff --git a/sdk/sdk.go b/sdk/sdk.go index d823bc9..b0045b1 100644 --- a/sdk/sdk.go +++ b/sdk/sdk.go @@ -28,6 +28,18 @@ type DingTalkClient struct { AgentId int64 } +type DingTalkOauthClient struct { + OauthAppId string + OauthAppSecret string +} + +func NewDingTalkOauthClient() *DingTalkOauthClient { + return &DingTalkOauthClient{ + OauthAppId: os.Getenv("OAUTH_APP_ID"), + OauthAppSecret: os.Getenv("OAUTH_APP_SECRET"), + } +} + func NewSDK() *DingTalkSDK { appId, err := strconv.ParseInt(os.Getenv("APP_ID"), 10, 64) if err != nil { @@ -98,28 +110,21 @@ func (corp *Corp) CreateDingTalkClient() (*DingTalkClient, error) { return NewDingTalkClient(tokenInfo.AccessToken, authInfo.AuthInfo.Agent[0].AgentId), nil } -func ExcuteOapi(url string, accessKey string, accessSecret string, suiteTicket string, corpId string, body string) (string, error) { +func ExcuteOapi(url string, oauthAppId string, oauthAppSecret string, body string) (string, error) { timestamp := time.Now().UnixNano() / 1e6 nativeSignature := strconv.FormatInt(timestamp, 10) - if suiteTicket != "" { - nativeSignature += "\n" + suiteTicket - } - afterHmacSHA256 := encrypt.SHA256(nativeSignature, accessSecret) + oauthAppId = "dingoayheivmu34mylzrzg" + oauthAppSecret = "PeQfl0uBtO3VLr-Feix5cIEWM9Oo81Mhec-SRiO2SPiEPU18fg0YZaB7fHXVkX3U" + afterHmacSHA256 := encrypt.SHA256(nativeSignature, oauthAppSecret) afterBase64 := encrypt.BASE64(afterHmacSHA256) afterUrlEncode := encrypt.URLEncode(afterBase64) params := map[string]string{ "timestamp": strconv.FormatInt(timestamp, 10), - "accessKey": accessKey, + "accessKey": oauthAppId, "signature": afterUrlEncode, } - if suiteTicket != "" { - params["suiteTicket"] = suiteTicket - } - if corpId != "" { - params["corpId"] = corpId - } return http.Post(url, params, body) }