Dingtalk is an enterprise communication tool similar to Jira/Confluence/WhatApp. It provides OAuth 2 service. The protocol is as below.
OAuth2 protocol, Dingtalk version Auth GET https://login.dingtalk.com/oauth2/auth? redirect_uri=https%3A%2F%2Fwww.baidu.com%2F&response_type=code&client_id=dingyourclientid&scope=openid&prompt=consent
The redirect callback after Auth will be with below format: https://www.baidu.com/?authCode=6b427e8bfab83e93bedd13f16a430702
Get token POST https://api.dingtalk.com/v1.0/oauth2/userAccessToken Content-Type:application/json
{ "clientId" : "ding your id", "clientSecret" : "your secret", "code" : "6b427e8bfab83e93bedd13f16a430702", "grantType" : "authorization_code" } The response will be
{ "expireIn": 7200, "accessToken": "a8f4e3215a703ce9a7164e91dbab53c0", "refreshToken": "b13e5a61b421342d95d86c9e64c275c6" } Retrieve user info GET https://api.dingtalk.com/v1.0/contact/users/me x-acs-dingtalk-access-token:a8f4e3215a703ce9a7164e91dbab53c0 Content-Type:application/json
defparse_form_data_to_json(form_data): parsed_data = parse_qs(form_data) result = {k: v[0] for k, v in parsed_data.items()} return result
defmain(event, context): print(f"event:\n{event}") s = event.get("body") if event.get("isBase64Encoded") and s: s = b64decode(s).decode("utf-8") body = parse_form_data_to_json(s)