microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
core/test/ABSTokenServiceClient/UserTokenCLIService.cs
78lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using System.Text.Json; |
| 5 | using Microsoft.Extensions.Hosting; |
| 6 | using Microsoft.Extensions.Logging; |
| 7 | using Microsoft.Teams.Core; |
| 8 | |
| 9 | namespace ABSTokenServiceClient |
| 10 | { |
| 11 | internal class UserTokenCLIService(UserTokenClient userTokenClient, ILogger<UserTokenCLIService> logger) : IHostedService |
| 12 | { |
| 13 | public Task StartAsync(CancellationToken cancellationToken) |
| 14 | { |
| 15 | return ExecuteAsync(cancellationToken); |
| 16 | } |
| 17 | |
| 18 | public Task StopAsync(CancellationToken cancellationToken) |
| 19 | { |
| 20 | return Task.CompletedTask; |
| 21 | } |
| 22 | |
| 23 | protected async Task ExecuteAsync(CancellationToken cancellationToken) |
| 24 | { |
| 25 | string userId = Environment.GetEnvironmentVariable("TEST_USER_ID") ?? throw new ArgumentNullException("TEST_USER_ID not found"); |
| 26 | string connectionName = Environment.GetEnvironmentVariable("TEST_CONNECTION_NAME") ?? throw new ArgumentNullException("TEST Connection name nor found"); |
| 27 | const string channelId = "msteams"; |
| 28 | |
| 29 | logger.LogInformation("Application started"); |
| 30 | |
| 31 | try |
| 32 | { |
| 33 | logger.LogInformation("=== Testing GetTokenStatus ==="); |
| 34 | GetTokenStatusResult[] tokenStatus = await userTokenClient.GetTokenStatusAsync(userId, channelId, null, cancellationToken); |
| 35 | logger.LogInformation("GetTokenStatus result: {Result}", JsonSerializer.Serialize(tokenStatus, new JsonSerializerOptions { WriteIndented = true })); |
| 36 | |
| 37 | if (tokenStatus[0].HasToken == true) |
| 38 | { |
| 39 | GetTokenResult? tokenResponse = await userTokenClient.GetTokenAsync(userId, connectionName, channelId, null, cancellationToken); |
| 40 | logger.LogInformation("GetToken result: {Result}", JsonSerializer.Serialize(tokenResponse, new JsonSerializerOptions { WriteIndented = true })); |
| 41 | } |
| 42 | else |
| 43 | { |
| 44 | GetSignInResourceResult req = await userTokenClient.GetSignInResource(userId, connectionName, channelId, null, cancellationToken); |
| 45 | logger.LogInformation("GetSignInResource result: {Result}", JsonSerializer.Serialize(req, new JsonSerializerOptions { WriteIndented = true })); |
| 46 | |
| 47 | Console.WriteLine("Code?"); |
| 48 | string code = Console.ReadLine()!; |
| 49 | |
| 50 | GetTokenResult? tokenResponse2 = await userTokenClient.GetTokenAsync(userId, connectionName, channelId, code, cancellationToken); |
| 51 | logger.LogInformation("GetToken With Code result: {Result}", JsonSerializer.Serialize(tokenResponse2, new JsonSerializerOptions { WriteIndented = true })); |
| 52 | } |
| 53 | |
| 54 | Console.WriteLine("Want to signout? y/n"); |
| 55 | string yn = Console.ReadLine()!; |
| 56 | if ("y".Equals(yn, StringComparison.OrdinalIgnoreCase)) |
| 57 | { |
| 58 | try |
| 59 | { |
| 60 | await userTokenClient.SignOutUserAsync(userId, connectionName, channelId, cancellationToken); |
| 61 | logger.LogInformation("SignOutUser completed successfully"); |
| 62 | } |
| 63 | catch (Exception ex) |
| 64 | { |
| 65 | logger.LogError(ex, "Error during SignOutUser"); |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | catch (Exception ex) |
| 70 | { |
| 71 | |
| 72 | logger.LogError(ex, "Error during API testing"); |
| 73 | } |
| 74 | |
| 75 | logger.LogInformation("Application completed successfully"); |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | |