public static string GetAuthenticationUrl(Permissions permissions) { IDictionary <string, string> parameters = new Dictionary <string, string> { { "api_key", ApiKey }, { "format", "json" }, { "method", _getFrob } }; string apiSig = SignApiParameters(parameters); parameters.Add("api_sig", apiSig); string url = string.Format("{0}?{1}", _restUrl, EncodeParameters(parameters)); WebRequest webRequest = WebRequest.Create(url); webRequest.Method = WebRequestMethods.Http.Get; WebResponse webResponse = webRequest.GetResponse(); string data; using (StreamReader sr = new StreamReader(webResponse.GetResponseStream())) { data = sr.ReadToEnd(); sr.Close(); } RtmApiResponseRoot responseRoot = JsonConvert.DeserializeObject <RtmApiResponseRoot>(data); RtmApiResponse response = responseRoot.Response; _frob = response.Frob; string perms = string.Empty; switch (permissions) { case Permissions.Read: perms = "read"; break; case Permissions.Write: perms = "write"; break; case Permissions.Delete: perms = "delete"; break; } parameters.Remove("format"); parameters.Remove("method"); parameters.Remove("api_sig"); parameters.Add("perms", perms); parameters.Add("frob", _frob); apiSig = SignApiParameters(parameters).ToLower(); parameters.CreateNewOrUpdateExisting("api_sig", apiSig); return(string.Format("{0}?{1}", _authUrl, EncodeParameters(parameters))); }
private static void CheckAuthentication() { string apiKey; string secret; using (FileStream fs = new FileStream("apikey.apikey", FileMode.Open, FileAccess.Read)) { using (StreamReader sr = new StreamReader(fs)) { apiKey = sr.ReadLine(); sr.Close(); fs.Close(); } } using (FileStream fs = new FileStream("secret.secret", FileMode.Open, FileAccess.Read)) { using (StreamReader sr = new StreamReader(fs)) { secret = sr.ReadLine(); sr.Close(); fs.Close(); } } RtmConnectionHelper.InitializeRtmConnection(apiKey, secret); if (File.Exists("authtoken.authtoken")) { string authtoken; using (FileStream fs = new FileStream("authtoken.authtoken", FileMode.Open, FileAccess.Read)) { using (StreamReader sr = new StreamReader(fs)) { authtoken = sr.ReadLine(); sr.Close(); fs.Close(); } } RtmConnectionHelper.SetApiAuthToken(authtoken); } else { RefreshToken(); } RtmApiResponse tokenResponse = RtmConnectionHelper.CheckApiAuthToken(); if (tokenResponse.ErrorResponse.Code == 98) { RefreshToken(); } /*tokenResponse =*/ RtmConnectionHelper.CheckApiAuthToken(); }
public static RtmApiResponse GetApiAuthToken() { IDictionary <string, string> parameters = new Dictionary <string, string> { { "api_key", ApiKey }, { "format", "json" }, { "frob", _frob }, { "method", _getToken } }; RtmApiRequest request = new RtmApiRequest(); request.Parameters = new SortedDictionary <string, string>(parameters); RtmApiResponse response = SendRequest(WebRequestMethods.Http.Get, _getToken, request); AuthToken = response.Auth.Token; return(response); }
private static void Main(string[] args) { CheckAuthentication(); IDictionary <string, string> parameters = new Dictionary <string, string>(); RtmApiResponse listResponse = RtmConnection.SendRequest("rtm.lists.getList", parameters); RtmApiResponse taskResponse = RtmConnection.SendRequest("rtm.tasks.getList", parameters); IList <string> tssListIds = listResponse.ListCollection.Lists.Where(list => list.Name.ToLower().Contains("3ss")).Select(list => list.Id).ToList(); IList <RtmApiTaskSeriesList> test = taskResponse.TaskSeriesCollection.TaskSeriesList.Where(taskSeriesList => taskSeriesList.TaskSeries.Any() && tssListIds.Contains(taskSeriesList.Id)).ToList(); Console.WriteLine("Lists:"); Console.WriteLine(string.Join(Environment.NewLine, listResponse.ListCollection.Lists.Select(list => list.Name))); Console.WriteLine("{0}Tasks:", Environment.NewLine); Console.WriteLine(string.Join(Environment.NewLine, taskResponse.TaskSeriesCollection.TaskSeriesList.Where(series => series.TaskSeries != null && series.TaskSeries.Any()).Select(series => string.Join(Environment.NewLine, series.TaskSeries.Select(task => task.Name))))); Console.ReadKey(); }
private static void Main(string[] args) { CheckAuthentication(); RtmApiResponse listResponse = RtmMethodHelper.GetListsList(); RtmApiResponse taskResponse = RtmMethodHelper.GetTasksList(); string timeline = RtmConnectionHelper.CreateTimeline().TimeLine; RtmApiResponse newTaskResponse = RtmMethodHelper.AddTask(timeline, "try something new at 5 pm", parse: "1"); //string transactionId = newTaskResponse.Transaction.Id; //RtmApiResponse undoResponse = RtmMethodHelper.UndoTransaction(timeline, transactionId); //IList<string> tssListIds = listResponse.ListCollection.Lists.Where(list => string.Equals(list.Name, "3ss tasks", StringComparison.OrdinalIgnoreCase)).Select(list => list.Id).ToList(); //IList<RtmApiTaskSeries> tssTasks = taskResponse.TaskSeriesCollection.TaskSeriesList // .Where(taskSeriesList => taskSeriesList.TaskSeries.Any() && tssListIds.Contains(taskSeriesList.Id)) // .SelectMany(taskSeriesList => taskSeriesList.TaskSeries) // .ToList(); }
private static void RefreshToken() { string url = RtmConnectionHelper.GetAuthenticationUrl(RtmConnectionHelper.Permissions.Delete); Console.WriteLine("Press any key after auth is complete."); Process.Start(url); Console.ReadKey(); RtmApiResponse authResponse = RtmConnectionHelper.GetApiAuthToken(); using (FileStream fs = new FileStream("authtoken.authtoken", FileMode.Create, FileAccess.ReadWrite)) { using (StreamWriter sw = new StreamWriter(fs)) { sw.Write(authResponse.Auth.Token); sw.Close(); fs.Close(); } } }
/// <summary> /// TaskSeriesID and ListID are the same ! /// </summary> /// <param name="list_id"></param> /// <param name="timeline"></param> public void GetStoredRTMTasks(string list_id, string timeline) { if (list_id is null || timeline is null) { throw new ArgumentNullException(nameof(list_id)); } RtmApiResponse listResponse = RtmMethodHelper.GetListsList(); RtmApiResponse taskResponse = RtmMethodHelper.GetTasksList(); // return all lists int tssListIds2 = listResponse.ListCollection.Lists.Count(); logger.Info("number of RTM lists: " + tssListIds2); RtmApiListObject[] lst_array = listResponse.ListCollection.Lists.ToArray(); foreach (var list_name in lst_array) { logger.Info(list_name.Name); if (list_name.Name == "ID-MeetupRTM") { target_rtm_meetup_list = list_name.Id; } } logger.Info("target_rtm_meetup_list: -->" + target_rtm_meetup_list); var list_res = RtmMethodHelper.GetTasksList(target_rtm_meetup_list); var coun = list_res.TaskSeriesCollection.TaskSeriesList.Select(x => x.TaskSeries.Count()).Sum(); // var count = list_res.TaskSeriesCollection.TaskSeriesList.SelectMany(a => a.TaskSeries); logger.Info("count of tasks in the list: -->" + coun + ".......... " + "--------------"); var ssslist_id = taskResponse.TaskSeriesCollection.TaskSeriesList.FirstOrDefault(); logger.Info("list id and task/taskseries id: " + ssslist_id.Id); // TODO: List all tasks id, then check for their names and if we really need them again }
public static string GetAuthenticationUrl(Permissions permissions) { IDictionary <string, string> parameters = new Dictionary <string, string>(); RtmApiResponse response = SendRequest(GetFrobMethod, parameters); Frob = response.Frob; string perms = string.Empty; switch (permissions) { case Permissions.Read: perms = "read"; break; case Permissions.Write: perms = "write"; break; case Permissions.Delete: perms = "delete"; break; } parameters.Remove("format"); parameters.Remove("method"); parameters.Remove("api_sig"); parameters.Add("perms", perms); parameters.Add("frob", Frob); parameters.Add("api_key", ApiKey); string apiSig = SignApiParameters(parameters).ToLower(); parameters.CreateNewOrUpdateExisting("api_sig", apiSig); return($"{AuthUrl}?{EncodeParameters(parameters)}"); }
private static RtmApiResponse SendRequest(string webRequestMethod, string rtmMethod, RtmApiRequest request) { RtmApiResponse response = null; try { request.Parameters.CreateNewOrUpdateExisting("format", "json"); request.Parameters.CreateNewOrUpdateExisting("method", rtmMethod); request.Parameters.CreateNewOrUpdateExisting("api_key", ApiKey); request.Parameters.CreateNewOrUpdateExisting("auth_token", AuthToken); request.Parameters.Remove("api_sig"); string apiSig = SignApiParameters(request.Parameters); request.Parameters.Add("api_sig", apiSig); string url = string.Format("{0}?{1}", _restUrl, EncodeParameters(request.Parameters)); WebRequest webRequest = WebRequest.Create(url); webRequest.Method = webRequestMethod; WebResponse webResponse = webRequest.GetResponse(); string data; using (StreamReader sr = new StreamReader(webResponse.GetResponseStream())) { data = sr.ReadToEnd(); sr.Close(); } RtmApiResponseRoot responseRoot = JsonConvert.DeserializeObject <RtmApiResponseRoot>(data); response = responseRoot.Response; return(response); } catch (Exception) { return(response); } }
/// <summary> /// Take Auth keys and initiate basic RTM connection by creating or reading authtoken file /// </summary> /// <param name="authKeys">Passing <c>AuthKeys</c> keys</param> public void InitiateConnection(AuthKeys authKeys) { if (!string.IsNullOrEmpty(authKeys.MyRTMkey) || !string.IsNullOrEmpty(authKeys.MyRTMsecret)) { RtmConnectionHelper.InitializeRtmConnection(authKeys.MyRTMkey, authKeys.MyRTMsecret); } else { System.Windows.Application.Current.Shutdown(); } MainWindow.SetLoggingMessage_Other("RTM: Initiate Connection using: " + authKeys.MyRTMkey + " and " + authKeys.MyRTMsecret); string rtmUriWithWritePerms = RtmConnectionHelper.GetAuthenticationUrl(RtmConnectionHelper.Permissions.Write); MainWindow.SetLoggingMessage_Other("RTM: we are inside of InitiateConnection() method: " + rtmUriWithWritePerms); Process.Start(rtmUriWithWritePerms); System.Threading.Thread.Sleep(2000); RtmApiResponse authResponseToken = RtmConnectionHelper.GetApiAuthToken(); logger.Info("RTM: authResponseToken: -> " + authResponseToken.Auth.Token); MainWindow.SetLoggingMessage_Other("RTM: " + authResponseToken.Auth.User.FullName + " token: -> " + authResponseToken.Auth.Token); if (!File.Exists("authtoken.authtoken")) { // if file does not exist, then create it by writing into it CreateAuthFile("authtoken.authtoken", authResponseToken.Auth.Token); } else { // if file does exist, then check for its creation time DateTime file_created = File.GetLastWriteTime("authtoken.authtoken"); long file_len = new FileInfo("authtoken.authtoken").Length; // if that time is older than 1 day, check for token's validity if ((DateTime.Now - file_created).TotalHours >= 25 || file_len < 10) { try { RtmConnectionHelper.CheckApiAuthToken(); } catch (Exception e) { string mes = "RTM: checking for the RTM token has failed. Creating new one"; MainWindow.SetLoggingMessage_Other(mes); logger.Error(mes); MainWindow.SetLoggingMessage_Other(e.Message); } CreateAuthFile("authtoken.authtoken", authResponseToken.Auth.Token); } else { string file_text = File.ReadAllText(@"authtoken.authtoken"); string msg = "RTM: Our file_text to read from: "; logger.Info(msg + file_text); MainWindow.SetLoggingMessage_Other(msg + file_text); RtmConnectionHelper.SetApiAuthToken(file_text); } } }