Skip to content

Commit

Permalink
Update zoom token
Browse files Browse the repository at this point in the history
  • Loading branch information
rstropek committed Sep 21, 2023
1 parent b2eae8d commit 7edc37f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
33 changes: 30 additions & 3 deletions CDWPlanner/Services/PlanZoomMeeting.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using CDWPlanner.DTO;
using Azure.Core;
using CDWPlanner.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace CDWPlanner
Expand All @@ -22,10 +24,12 @@ public interface IPlanZoomMeeting
public class PlanZoomMeeting : IPlanZoomMeeting
{
private readonly HttpClient client;
private readonly HttpClient zoomTokenClient;

public PlanZoomMeeting(IHttpClientFactory clientFactory)
{
client = clientFactory.CreateClient("zoom");
zoomTokenClient = clientFactory.CreateClient("zoomToken");
}

public async Task<IEnumerable<Meeting>> GetExistingMeetingsAsync()
Expand Down Expand Up @@ -68,10 +72,31 @@ public async Task<IEnumerable<User>> ListUsersAsync()

public User GetUser(IEnumerable<User> usersBuffer, string zoomUser) =>
usersBuffer.Where(u => (zoomUser == u.email) || (zoomUser == u.id)).FirstOrDefault();


internal record TokenResponse([property: JsonPropertyName("access_token")] string AccessToken);

internal async Task<string> GetAccessToken()
{
var zoomAccountId = Environment.GetEnvironmentVariable("ZOOMACCOUNTID", EnvironmentVariableTarget.Process);
var zoomClientId = Environment.GetEnvironmentVariable("ZOOMCLIENTID", EnvironmentVariableTarget.Process);
var zoomClientSecret = Environment.GetEnvironmentVariable("ZOOMCLIENTSECRET", EnvironmentVariableTarget.Process);
var body = $"grant_type=account_credentials&account_id={zoomAccountId}";
var tokenRequest = new HttpRequestMessage(HttpMethod.Post, "token")
{
Content = new StringContent(body, Encoding.UTF8, "application/x-www-form-urlencoded")
};
tokenRequest.Headers.Add("Authorization", $"Basic {Convert.ToBase64String(Encoding.UTF8.GetBytes($"{zoomClientId}:{zoomClientSecret}"))}");
var tokenResponse = zoomTokenClient.SendAsync(tokenRequest).Result;
tokenResponse.EnsureSuccessStatusCode();
var token = await JsonSerializer.DeserializeAsync<TokenResponse>(tokenResponse.Content.ReadAsStream());
return token.AccessToken;
}

private async Task<T> GetFromZoomAsync<T>(string url)
{
using var getResponse = await client.GetAsync(url);
var request = new HttpRequestMessage(HttpMethod.Get, url);
request.Headers.Add("Authorization", $"Bearer {await GetAccessToken()}");
using var getResponse = await client.SendAsync(request);
getResponse.EnsureSuccessStatusCode();
var getJsonContent = await getResponse.Content.ReadAsStringAsync();
return JsonSerializer.Deserialize<T>(getJsonContent);
Expand All @@ -88,6 +113,7 @@ public async void UpdateMeetingAsync(Meeting meeting, string time, string date,
Method = HttpMethod.Patch,
Content = CreateStringContentForMeeting(startTime, title, description, shortCode, meeting.password, userId)
};
meetingRequest.Headers.Add("Authorization", $"Bearer {await GetAccessToken()}");

using var getResponse = await client.SendAsync(meetingRequest);
var responseContent = await getResponse.Content.ReadAsStringAsync();
Expand All @@ -106,6 +132,7 @@ public async Task<Meeting> CreateZoomMeetingAsync(string time, string date, stri
Method = HttpMethod.Post,
Content = CreateStringContentForMeeting(startTime, title, description, shortCode, randomPsw, userId)
};
meetingRequest.Headers.Add("Authorization", $"Bearer {await GetAccessToken()}");

using var getResponse = await client.SendAsync(meetingRequest);
getResponse.EnsureSuccessStatusCode();
Expand Down
13 changes: 11 additions & 2 deletions CDWPlanner/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
using Discord.Rest;
using Azure.Messaging.ServiceBus;
using ShlinkDotnet.Extensions;
using System.Net.Http;
using System.Text;
using System.Text.Json.Serialization;

[assembly: FunctionsStartup(typeof(CDWPlanner.Startup))]
[assembly: InternalsVisibleTo("CDWPlanner.Tests")]
Expand All @@ -31,11 +34,17 @@ public override void Configure(IFunctionsHostBuilder builder)
c.DefaultRequestHeaders.Add("Timeout", "1000000000");
});

var zoomToken = Environment.GetEnvironmentVariable("ZOOMTOKEN", EnvironmentVariableTarget.Process);
builder.Services.AddHttpClient("zoomToken", c =>
{
c.BaseAddress = new Uri("https://zoom.us/oauth/");
c.DefaultRequestHeaders.Add("Host", "zoom.us");
c.DefaultRequestHeaders.Add(HttpRequestHeader.ContentType.ToString(), "application/x-www-form-urlencoded");
c.DefaultRequestHeaders.Add(HttpRequestHeader.Accept.ToString(), "application/json");
});

builder.Services.AddHttpClient("zoom", c =>
{
c.BaseAddress = new Uri("https://api.zoom.us/v2/");
c.DefaultRequestHeaders.Add(HttpRequestHeader.Authorization.ToString(), $"Bearer {zoomToken}");
c.DefaultRequestHeaders.Add(HttpRequestHeader.ContentType.ToString(), "application/json;charset='utf-8'");
c.DefaultRequestHeaders.Add(HttpRequestHeader.Accept.ToString(), "application/json");
c.DefaultRequestHeaders.Add("Timeout", "1000000000");
Expand Down

0 comments on commit 7edc37f

Please sign in to comment.