C # WeChat portal and application development (17)-department management for address book management and development of WeChat enterprise account,

Source: Internet
Author: User

C # development portal and application (17)-department management for enterprise address book management and development,

In the previous article, I wrote some basic information about the enterprise number and introduced how to configure the callback method of the enterprise number to build a bridge for communication with the enterprise number server. This article mainly introduces the development of enterprise numbers, introduces the functions of Enterprise Number Address Book Management, and introduces how to obtain and manage Department Information in the Organization.

1. Create and configure enterprise organizations

First, we can create an organizational unit in the enterprise account management background, which creates a list of departments and personnel for our convenience of development and use.

For example, create a root structure of Guangzhou iqidi and create some organizational units in it, as shown in.


Then, add an administrator permission to the root node "" of the organizational structure, and then use the Secret value of the Administrator to call the interface.


CorpID is the enterprise ID, each of which has a unique CorpID, and Secret is the key of the Management Group creden.
The system administrator can create a management group by using the permission management function of the Management end, and assign the Management Group access permissions to applications, address books, and interfaces. Then, the Management Group can obtain a unique secret. The system administrator can view the secret of all management groups through permission management, and other administrators can view the secret through the developer creden。 in the settings.

The creator of my enterprise account is different from the Administrator of the organizational structure of "Guangzhou aiqidi". Because Secret is the credential key of the Management Group, if the manager is responsible for the management of different organizations, the management Secret value may be different. If you need to call an interface, you need to use this Secret value at your own permission level, as shown in.


If it is not the creator of the enterprise account, you may not be able to modify some permissions in it, but you can only view them.


2. Obtain the globally unique AccessToken for API access

Like the public account, the first step for us to call the enterprise account API is to first obtain the access ticket accesen en. This ticket is global and has a certain degree of timeliness and Frequency Control. Therefore, you need to cache the ticket properly. You cannot refresh the ticket every time you call it.

The main logic code for enterprise numbers to obtain access tickets is as follows. The main logic is to use the Secret value of the Administrator to obtain the corresponding password, in this way, it can know that the organizational structure is managed.

/// <summary>
///Get token access token of API for each operation
/// </summary>
///< param name = "Corpid" > Enterprise ID < / param >
///< param name = "corpsecret" > voucher key of management group < / param >
/// <returns></returns>
public string GetAccessTokenNoCache(string corpid, string corpsecret)
{
var url = string.Format("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={0}&amp;corpsecret={1}",
corpid, corpsecret);
HttpHelper helper = new HttpHelper();
string result = helper.GetHtml(url);
string regex = "\"access_token\":\"(?<token>.*?)\"";
string token = CRegex.GetText(result, regex, "token");
return token;
}

The enterprise number is described as follows:

When an enterprise application calls the enterprise ID interface, the enterprise ID backend verifies the validity of the access and the management permissions of the corresponding management group based on the AccessToken of the access to return the corresponding results.

Note:You should carefully configure the permissions of the Management Group, which is sufficient. Beyond the conference, the permissions will increase the possibility of misoperations and information security risks.

The AccessToken is the globally unique ticket of the enterprise account. When calling an interface, you must carry the AccessToken. The AccessToken must be replaced by the CorpID and Secret. Different Secret return different AccessToken.Under normal circumstances, the AccessToken is valid for 7200 seconds. The same results are returned and automatically renewed. Because the number of api calls to obtain access_token is very limited, we recommend that you store and update access_token globally. Refreshing access_token frequently will restrict api calls and affect your business..

 

2. Maintain department information in Address Book Management

With the access ticket in Section 1, we can use APIs to do a lot of things, including acquiring, creating, and deleting organizational structures.

The official interface for creating a department is defined as follows.

  • Request description

Https Request Method: POST

Https://qyapi.weixin.qq.com/cgi-bin/department/create? Access_token = ACCESS_TOKEN

The request package structure is:

{"Name": "email product group", "parentid": "1 "}
  • Parameter description
Parameters Required Description
Access_token Yes Interface call credential
Name Yes Department name. The length is limited to 1 ~ 64 characters
Parentid Yes The id of the parent department. The root department id is 1.

 

  • Returned results
{   "errcode": 0,   "errmsg": "created",   "id": 2}

According to some similar interface definition descriptions above, we first define the maintenance interface for Organization Department data, and then implement and call it gradually.

#Region department management
/// <summary>
///Create a department.
///The administrator must have the interface permission of "operation address book" and the management permission of the parent department.
/// </summary>
CorpDeptCreateJson CreateDept(string accessToken, string name, string parentId);
/// <summary>
///Update department.
///The administrator shall have the interface authority of "operation address book" and the management authority of the Department.
/// </summary>
CommonResult DeleteDept(string accessToken, int id);
/// <summary>
///Delete department
///The administrator shall have the interface authority of "operation address book" and the management authority of the Department.
/// </summary>
CorpDeptListJson ListDept(string accessToken);
/// <summary>
///Get a list of departments
///The administrator must have the interface permission of "get Department list" and the view permission of the Department.
/// </summary>
CommonResult UpdateDept(string accessToken, int id, string name);
#endregion

As shown in the following figure, the interface for creating a department is implemented. It is mainly to construct URL and POST data packets, call and obtain the returned data in a unified manner, and convert the data to a specific Json object entity. The implementation of other interfaces is similar.

/// <summary>
///Create a department.
///The administrator must have the interface permission of "operation address book" and the management permission of the parent department.
/// </summary>
public CorpDeptCreateJson CreateDept(string accessToken, string name, string parentId)
{
string urlFormat = "https://qyapi.weixin.qq.com/cgi-bin/department/create?access_token={0}";
var data = new
{
Name = name,
parentId = parentId
}
var url = string.Format(urlFormat, accessToken);
var postData = data.ToJson();
CorpDeptCreateJson result = CorpJsonHelper<CorpDeptCreateJson>.ConvertJson(url, postData);
return result;
}

The definition of the CorpDeptCreateJson object class is as follows. We mainly define the Class Based on the returned results.

/// <summary>
///Create return result of Department
/// </summary>
public class CorpDeptCreateJson : BaseJsonResult
{
/// <summary>
///Error message returned
/// </summary>
public CorpReturnCode errcode { get; set; }
/// <summary>
///Text description of return code
/// </summary>
public string errmsg { get; set; }
/// <summary>
///Department ID created.
/// </summary>
public int id { get; set; }
}


 

3. Department-managed API calls

The above section describes how to encapsulate the APIS managed by the Department, so we have encapsulated the corresponding interfaces and interface implementations. How can we handle calls in the actual environment, to help me create a small Winform program to test the functions of the corresponding API, as shown below.


The following describes the calling code and result display.

private void btnCreateDeleteDept_Click(object sender, EventArgs e)
{
ICorpAddressBookApi bll = new CorpAddressBookApi();
String name = "test department";
CorpDeptCreateJson json = bll.CreateDept(token, name, "2");
if (json != null)
{
Console. Writeline ("created Department: {0}, ID: {1}", name, JSON. ID);
//Update department information
Name = "test department modification name";
CommonResult result = bll.UpdateDept(token, json.id, name);
if(result != null)
{
Console. Writeline ("modify Department Name: {0} {1}", (result. Success? "Success": "failure"), result. ErrorMessage);
}
//Delete Department
result = bll.DeleteDept(token, json.id);
if (result != null)
{
Console. Writeline ("delete Department Name: {0} {1}", (result. Success? "Success": "failure"), result. ErrorMessage);
}
}
}
/// <summary>
///Get Department list
/// </summary>
private void btnListDept_Click(object sender, EventArgs e)
{
ICorpAddressBookApi bll = new CorpAddressBookApi();
CorpDeptListJson list = bll.ListDept(token);
foreach (CorpDeptJson info in list.department)
{
string tips = string.Format("{0}:{1}", info.name, info.id);
Console.WriteLine(tips);
}
} 
 

 

 

If you are interested in this series of C # development portals and applications, you can follow my other articles as follows:

C # development portal and application (16)-enterprise number configuration and use

C # development portal and application (15)-added the scan, image sending, and geographic location functions in the menu

C # development portal and application (14)-use redirection in the menu to obtain user data

C # development portal and application (13)-use geographic location Extension

C # development portal and application (12)-use voice processing

C # development portal and application (11)-menu presentation

C # development portal and application (10) -- synchronize user group information in the management system

C # development portal and application (9)-portal menu management and submission to server

C # development portal and application (8)-portal application management system function Introduction

C # development portal and application (7)-Multi-customer service functions and development integration

C # development portal and application (6)-portal menu management operations

C # development portal and application (5) -- User Group Information Management

C # development portal and application (4) -- Focus on the user list and detailed information management

C # development portal and application (3) -- Response to text messages and text messages

C # development portal and application (2) -- Message Processing and response

C # development portal and application (1) -- getting started with Interfaces


C :\

Yes

Refer to this to clean up the C drive:
1. Disable System Restoration: My computer properties/System Restoration/disable System Restoration on all disks, but I will not be able to use system restoration in the future!
2. Disable System sleep: Control Panel/Power Supply/sleep/remove the check before starting system sleep
3. move the virtual memory, my computer properties/advanced/performance/settings/advanced/change/select the C disk, that is, the system disk, select the no-score page, and then set the virtual memory to its disk, A disk with more disk space remaining, such as D, E, and F. set to 1.5 ~ of memory ~ 2.5 times. The size can be set to the same!
5. Clear temporary IE folders, internet Options, and delete temporary and offline files.
6. delete system logs and program logs, my computer/control panel/management tools/Computer Management/Event Viewer/application, right-click/clear events, and clear system logs in sequence
7. Clear system cache: 2000 all files in the system: C: \ WINNT \ system32 \ dllcache
The XP system is: C: \ windows \ system32 \ dllcache all files under the system cache (open my computer/tool/file and Folder Options/hide the protected system file hook off to hide all files on the hook) ). You can also run the sfc.exe/purgecache command to automatically delete the file.
8. Clear the recycle bin
9. delete the files under c: \ windows \ SoftwareDistribution \ Download (the files downloaded when the system is updated are useless if you have installed the updates)
10. Delete all directories under c: \ windows \ RegisteredPackages
11. Delete all Files under C: \ WINDOWS \ Downloaded Program Files
12. view the hidden files that are known to be protected by the system in my computer folder option, and check all the files.
13. Delete c: \ windows \ All files with $8882305 $ (backup files after system update)

Zhidao.baidu.com/question/11035955.html
Zhidao.baidu.com/question/12223613.html
Zhidao.baidu.com/question/14874715.html
... The remaining full text>

C :\

Yes

Refer to this to clean up the C drive:
1. Disable System Restoration: My computer properties/System Restoration/disable System Restoration on all disks, but I will not be able to use system restoration in the future!
2. Disable System sleep: Control Panel/Power Supply/sleep/remove the check before starting system sleep
3. move the virtual memory, my computer properties/advanced/performance/settings/advanced/change/select the C disk, that is, the system disk, select the no-score page, and then set the virtual memory to its disk, A disk with more disk space remaining, such as D, E, and F. set to 1.5 ~ of memory ~ 2.5 times. The size can be set to the same!
5. Clear temporary IE folders, internet Options, and delete temporary and offline files.
6. delete system logs and program logs, my computer/control panel/management tools/Computer Management/Event Viewer/application, right-click/clear events, and clear system logs in sequence
7. Clear system cache: 2000 all files in the system: C: \ WINNT \ system32 \ dllcache
The XP system is: C: \ windows \ system32 \ dllcache all files under the system cache (open my computer/tool/file and Folder Options/hide the protected system file hook off to hide all files on the hook) ). You can also run the sfc.exe/purgecache command to automatically delete the file.
8. Clear the recycle bin
9. delete the files under c: \ windows \ SoftwareDistribution \ Download (the files downloaded when the system is updated are useless if you have installed the updates)
10. Delete all directories under c: \ windows \ RegisteredPackages
11. Delete all Files under C: \ WINDOWS \ Downloaded Program Files
12. view the hidden files that are known to be protected by the system in my computer folder option, and check all the files.
13. Delete c: \ windows \ All files with $8882305 $ (backup files after system update)

Zhidao.baidu.com/question/11035955.html
Zhidao.baidu.com/question/12223613.html
Zhidao.baidu.com/question/14874715.html
... The remaining full text>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.