基于AAD Token的C#后端服务调用Yammer API代码实现问询
无用户交互的Yammer后端服务实现(C#)
一、获取Yammer访问令牌(客户端凭据流)
因为服务无需用户登录页面,采用客户端凭据流直接向Azure AD申请令牌,前提是管理员已完成应用权限的授予操作。
代码实现
using System; using System.Net.Http; using System.Threading.Tasks; using System.Collections.Generic; public class YammerTokenService { private readonly HttpClient _httpClient; private readonly string _tenantId; private readonly string _clientId; private readonly string _clientSecret; public YammerTokenService(string tenantId, string clientId, string clientSecret) { _httpClient = new HttpClient(); _tenantId = tenantId; _clientId = clientId; _clientSecret = clientSecret; } public async Task<string> GetAccessTokenAsync() { var tokenEndpoint = $"https://login.microsoftonline.com/{_tenantId}/oauth2/token"; var requestBody = new FormUrlEncodedContent(new Dictionary<string, string> { ["grant_type"] = "client_credentials", ["client_id"] = _clientId, ["client_secret"] = _clientSecret, ["resource"] = "https://www.yammer.com" }); var response = await _httpClient.PostAsync(tokenEndpoint, requestBody); response.EnsureSuccessStatusCode(); var tokenResponse = await response.Content.ReadAsAsync<dynamic>(); return tokenResponse.access_token; } }
说明
- 替换代码中的
_tenantId、_clientId、_clientSecret为管理员提供的对应值 resource固定为https://www.yammer.com,是Yammer API的标识资源
二、调用Yammer API(创建用户+添加至群组)
拿到令牌后,通过Authorization: Bearer {token}请求头调用Yammer的相关接口。
代码实现
public class YammerApiService { private readonly HttpClient _httpClient; public YammerApiService() { _httpClient = new HttpClient(); } // 创建Yammer用户 public async Task CreateYammerUserAsync(string accessToken, string email, string fullName) { _httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", accessToken); var userData = new Dictionary<string, string> { ["email"] = email, ["full_name"] = fullName, ["active"] = "true" }; var content = new FormUrlEncodedContent(userData); var response = await _httpClient.PostAsync("https://www.yammer.com/api/v1/users.json", content); response.EnsureSuccessStatusCode(); } // 将用户添加至指定Yammer群组 public async Task AddUserToGroupAsync(string accessToken, long userId, long groupId) { _httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", accessToken); var membershipData = new Dictionary<string, string> { ["user_id"] = userId.ToString(), ["group_id"] = groupId.ToString() }; var content = new FormUrlEncodedContent(membershipData); var response = await _httpClient.PostAsync("https://www.yammer.com/api/v1/group_memberships.json", content); response.EnsureSuccessStatusCode(); } }
使用示例
class Program { static async Task Main(string[] args) { // 替换为管理员提供的配置信息 string tenantId = "你的租户ID"; string clientId = "你的客户端ID"; string clientSecret = "你的客户端密钥"; var tokenService = new YammerTokenService(tenantId, clientId, clientSecret); string accessToken = await tokenService.GetAccessTokenAsync(); var apiService = new YammerApiService(); // 创建用户(需替换为实际用户信息) await apiService.CreateYammerUserAsync(accessToken, "newuser@company.com", "New User"); // 解析返回的userId,再调用添加群组接口(需替换为实际群组ID) // long userId = ...; // 从CreateYammerUserAsync的返回结果中提取 // await apiService.AddUserToGroupAsync(accessToken, userId, 123456); } }
关键注意事项
- 确保Azure应用已被授予Yammer的应用权限(如
User.ReadWrite.All、GroupMember.ReadWrite.All等),且管理员已完成权限同意 - Yammer API的调用限制需参考官方文档,避免触发限流
- 生产环境中建议使用HttpClient工厂管理HttpClient实例,避免资源泄漏
内容的提问来源于stack exchange,提问作者Bárbara Malafaia
相关产品推荐
相关产品推荐

