请问一下,我需要获得钉钉通讯录的人员信息,显示在C#的DGV内,请问下述代码是否正确,另外,需要获得APPTOKEN的部分GetAccessTokenAsync,代码应该怎么写,请指导一下,谢谢;
using System;
using System.Data;
using System.Windows.Forms;
using AlibabaCloud.SDK.Dingtalkcontact_1_0;
using AlibabaCloud.SDK.Dingtalkcontact_1_0.Models;
using PlatBaseClass;
using System.Threading.Tasks;
using DingTalk.Api;
using DingTalk.Api.Response;
namespace SalaryCount
{
public partial class Form1 : BaseForm
{
private string appKey = "dingqnybw1hbp4lqyeqp";
private string appSecret = "UfXT4o9RG0CdKIP2sXy8NhuimP24tQKGrBRYpkLo1R8y0PLy_yfeI7fbCQHDPbQZ";
private string accessToken;
private DataTable dtUsers;
public Form1()
{
InitializeComponent();
dtUsers = new DataTable();
dtUsers.Columns.Add("UserID");
dtUsers.Columns.Add("Name");
dtUsers.Columns.Add("Department");
dataGridView1.DataSource = dtUsers;
}
private async void button1_Click(object sender, EventArgs e)
{
try
{
accessToken = await GetAccessTokenAsync();
var userList = await FetchUserListAsync(accessToken);
foreach (var user in userList)
{
dtUsers.Rows.Add(user.UserId, user.Name, user.Department);
}
}
catch (Exception ex)
{
MessageBox.Show($"Error: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private async Task<string> GetAccessTokenAsync()
{
// 实际的获取 access token 的逻辑待实现
throw new NotImplementedException();
}
private async Task<GetUserResponse> FetchUserListAsync(string accessToken)
{
try
{
var client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/list");
var request = new AlibabaCloud.SDK.Dingtalkcontact_1_0.Models.GetUserListRequest();
request.DeptId = "1"; // 部门 ID,如果要获取全部部门,可以不设置
request.Offset = 0;
request.Size = 10; // 每页大小,可以根据需要调整
var headers = new GetUserHeaders();
headers.XAcsDingtalkAccessToken = accessToken;
var options = new AlibabaCloud.TeaUtil.Models.RuntimeOptions();
var response = await client.GetUserListWithHttpInfoAsync(request, headers, options);
// 清空 dtUsers 中的数据
dtUsers.Clear();
// 将获取到的用户列表填充到 dtUsers 中
foreach (var user in response.Body.Result.List)
{
dtUsers.Rows.Add(user.UserId, user.Name, user.Department);
}
return response;
}
catch (Exception ex)
{
throw new Exception($"Error fetching users: {ex.Message}");
}
}
}
}