This example is based on an internal app and demonstrates how to retrieve user details from Contacts by calling the API. The same approach applies to other app types.
Prerequisites
- Basic concepts: Be familiar with the basic concepts of the DingTalk Open Platform and the documentation for each product module. See the basic concepts documentation for details.
- API rate limits: Be familiar with the API rate limits. See the API call frequency and rate limit documentation for details.
- Developer permissions: Have Sub Admin and developer permissions in the Developer Backend. You can also sign in to DingTalk Developer to register and activate a new user.
- Obtain the user
UserId: Sign in to the DingTalk Admin Console and view theUserIdunder Contacts > User management. See the basic concepts documentation for details.
Step 1: Create a DingTalk app
- Go to the Developer Backend and click App Development > DingTalk App > Create app.
-
Enter the app information and click Save.
Field Required Description App name Yes Enter the app name. The minimum length is 2 characters. App description Yes Briefly describe the products or services provided by the app. The minimum length is 4 characters. App icon No Upload the app icon. The icon must be in JPG or PNG format, at least 240 px * 240 px, with a 1:1 aspect ratio, no rounded corners, and no larger than 2 MB. - Go to the app details page and view the app credential and basic information under Basic information > Credential and basic information.
- Save the Client ID and Client Secret of the app for subsequent API calls.
Note
For details about how to obtain the Client ID and Client Secret, see the basic concepts documentation. Keep them safe after you obtain them to prevent leaks.Step 2: Configure permissions
-
Find the required permission: According to the API documentation, calling the user details API requires the
Read user informationpermission. -
Add the permission: Go to the Developer Backend, locate the app you created, go to the app details page, search for
Read user informationunder Manage permissions, and click Apply now.
Step 3: Obtain the Access Token
Standard-permission API
Call the API to obtain the Token
- Open the Obtain the access token of an internal app API page and click the API Debug button on the right to open the Server-side debugging tool.
- In the debugging tool, replace the values of
appkey (replace with the Client ID you obtained earlier)andappSecret (replace with the Client Secret you obtained earlier), and click Debug.
Important
You must be signed in to use the server-side debugging console and must bind the corresponding app. You can also copy the code below and use a cURL command. Replace the values ofappkey (with the Client ID you obtained earlier) and appSecret (with the Client Secret you obtained earlier), then send the request directly. Example:
Parse the response
After the call succeeds, an access token value is generated, as shown below:Cache and refresh
- Cache the
access_tokenin a database or memory and set up scheduled refreshes (10 minutes before expiration). - Avoid calling the Token API frequently, otherwise rate limits may be triggered.
Sensitive-permission API
To enable cross-organization data sharing and feature invocation, third-party apps must go through a strict authorization process. They need to obtain usage permission and user data access permission from the target organization through both organization-level and user-level authorization.Apply for the API permission
Apply for the API permission according to the Use sensitive permissions documentation.Integrate the authorization suite
After applying for the sensitive permission, integrate the authorization suite. When a user accepts the authorization in the prompt, anauthCode is returned. Then call the Obtain user token API to exchange it for a user-level access_token.
Step 4: Call the user details API
Standard-permission API
To retrieve user details, call the Query user details API. You can use the DingTalk SDK or send an HTTP request directly. Choose the method that fits your needs.Option 1: Use the DingTalk SDK (recommended)
Before using the DingTalk SDK, learn about the differences in the Server API, then download the corresponding SDK version. See the Server API differences documentation for details.
-
Install the SDK (Python example)
-
Code example
Option 2: Send an HTTP request directly
Successful response
If the response contains"errcode":"0" and "errmsg":"ok", the call succeeded.
Error handling
- Insufficient permissions: Check whether the
Read user informationpermission has been requested. - User does not exist: Verify that the
useridis valid and free of typos. - Token expired: Refresh the
access_tokenand try again. Make sure theaccess_tokenis valid and free of typos.
Sensitive-permission API
After exchanging theauthCode for a user-level access_token, call Retrieve user profile from Contacts to obtain the user’s name, unionId, and other information.
If the API returns 200, the call succeeded. The response is as follows: