Three steps of service end butt nail

Three steps of service end butt nail

Step 1: obtain the appkey and appsecret for business docking

Log in the developer background, click "application development" - "enterprise internal application" - "find the application and click the application icon" - "view details", you can view the AppKey and AppSecret.


Step 2: obtain the login access_token according to appkey and appsecret

/**
 * Initialize access_token
 * @return
 */
public static String getAccessToken(){
	String accessToken ="";
	DefaultDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
	OapiGettokenRequest request = new OapiGettokenRequest();
	request.setAppkey(appkey);
	request.setAppsecret(appsecret);
	request.setHttpMethod("GET");
	try {
		OapiGettokenResponse response = client.execute(request);
		accessToken = response.getAccessToken();
		Long errcode = response.getErrcode();
		if(null!=errcode && errcode==0l){
			return accessToken;
		}
	} catch (ApiException e) {
		e.printStackTrace();
	}
	return accessToken;
}


Step 3: request the service API provided by the pin according to the business requirements

/**
 * Get user name
 * @param userId
 * @return
 */
public static String getUserName(String userId){
	String accessToken =getAccessToken();
	String userName= "";
	DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/get");
	OapiUserGetRequest request = new OapiUserGetRequest();
	request.setUserid(userId);
	request.setHttpMethod("GET");
	try {
		OapiUserGetResponse response = client.execute(request, accessToken);
		userName = response.getName();
	} catch (ApiException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return userName;
}

Summary: according to the interface API provided by the government, the information needed should be obtained according to the circumstances

Provide the API document address of pin server: https://ding-doc.dingtalk.com/docą· / serverapi2/ege851

Tags: Programming

Posted on Mon, 18 May 2020 11:44:14 -0400 by phreud