/// <summary> /// A test lambda function /// </summary> /// <param name="context"></param> /// <returns></returns> public void FunctionHandler(ILambdaContext context) { //Create a list to store all the users from the database query List <User> UserList = new List <User>(); //Use the entity framework to create a request through the database context using (var db = new canvasremindwebappContext()) { //Put the whole database table into a list UserList = db.User.ToList(); foreach (var user in UserList) { //Refresh the access token Refresh(user).Wait(); } } //Use a new instance of the DbContext to get an updated database list to work from using (var db = new canvasremindwebappContext()) { List <User> users = new List <User>(); users = db.User.ToList(); //Do the rest of the steps foreach (var u in users) { Message message = new Message(); GetCourses(u, message).Wait(); SendEmail(u, message); SendText(u, message); } } }
//Function to refresh the users access token public async Task Refresh(User user) { //Setup a JSON data contract serializer to parse through the returned JSON parameters var serializer = new DataContractJsonSerializer(typeof(Refresh)); HttpClient client = new HttpClient(); //Set the header values for the http request using a dictionary var headerValues = new Dictionary <string, string>() { { "grant_type", "refresh_token" }, { "client_id", GetAWSParameter($"/CanvasProd/AppSecrets/Client_Id") }, { "client_secret", GetAWSParameter($"/CanvasProd/AppSecrets/Client_Secret") }, { "refresh_token", Decrypt(user.RefreshToken) } //May need to add the redirect uri header parameter }; //Form encode the header values for a POST request var content = new FormUrlEncodedContent(headerValues); //Send a POST request asynchronously var response = await client.PostAsync("https://" + GetAWSParameter($"/CanvasProd/AppSecrets/CanvasURL") + "/login/oauth2/token", content); //Read the response asynchronously var stream = response.Content.ReadAsStreamAsync(); //Parse the results of the stream var results = (Refresh)serializer.ReadObject(stream.Result); // <---- Change courses to an OAUTH class //Update the database using (var db = new canvasremindwebappContext()) { var change = db.User.Where(b => b.Name == user.Name && b.Email == user.Email && b.Phone == user.Phone).FirstOrDefault(); change.AccessToken = Encrypt(results.accessToken); db.SaveChanges(); //may have fixed the warning } }