1. 程式人生 > >【轉載】釘釘開發c#幫助類 獲取用戶信息 DingHelper.cs

【轉載】釘釘開發c#幫助類 獲取用戶信息 DingHelper.cs

todo tail user utf-8 userinfo .config ESS test gen

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Net;
using test;
using Newtonsoft.Json.Linq;


/// <summary>
/// DingHelper 的摘要說明
/// </summary>
public class DingHelper
{
public DingHelper()
{
//
// TODO: 在此處添加構造函數邏輯
//

}
/// <summary>
/// 獲取Access_Token
/// </summary>
/// <returns></returns>
public static string getAccess_Token()
{
string Access_Token = "";
string corpid = ConfigurationSettings.AppSettings["ddQyCorpId"].ToString();
string corpsecret = ConfigurationSettings.AppSettings["CorpSecret"].ToString();
test.HttpHelper http = new test.HttpHelper();
string url = "https://oapi.dingtalk.com/gettoken?corpid=" + corpid + "&corpsecret=" + corpsecret;
string res = http.GetString(url);
if (res != null)
{
JObject obj = JObject.Parse(res);
if (obj["errcode"].ToString() == "0")
{
Access_Token = obj["access_token"].ToString();
}
}
return Access_Token;
}
/// <summary>
/// 獲取用戶簡單信息
/// </summary>
/// <param name="code"></param>
/// <param name="accessToken"></param>
/// <returns></returns>
public static string getUserInfo(string code, string accessToken)
{
string result = null;
HttpHelper http = new HttpHelper();
string url = "https://oapi.dingtalk.com/user/getuserinfo?access_token=" + accessToken + "&code=" + code;
result = http.GetString(url);
return result;
}
/// <summary>
/// 獲取用戶詳細信息
/// </summary>
/// <param name="accessToken"></param>
/// <param name="userid"></param>
/// <returns></returns>
public static string getuserDetail(string accessToken, string userid)
{
string result = null;
HttpHelper http = new HttpHelper();
string url = "https://oapi.dingtalk.com/user/get?access_token=" + accessToken + "&userid=" + userid;
result = http.GetString(url, "", "", false, "UTF-8");
return result;
}
/// <summary>
/// 獲取部門詳細信息(只支持中文)
/// </summary>
/// <param name="accessToken"></param>
/// <param name="departmentId">部門id</param>
/// <returns></returns>
public static string getDepartmentInfo(string accessToken, string departmentId)
{
string result = null;
HttpHelper http = new HttpHelper();
string url = "https://oapi.dingtalk.com/department/get?access_token=" + accessToken + "&id=" + departmentId;
result = http.GetString(url, "", "", false, "UTF-8");
return result;
}
}

【轉載園主】七維月ok

【轉載】釘釘開發c#幫助類 獲取用戶信息 DingHelper.cs