private async void CheckTokenBtn_Click(object sender, EventArgs e) { await Task.Run(() => { var data = AuthTasks.DebugTokenAsync(AppAccessToken, UserAccessToken); //{ "data": { "app_id": 138483919580948, "application": "Social Cafe", "expires_at": 1352419328, "is_valid": true, "issued_at": 1347235328, "scopes": [ "email", "publish_actions" ], "user_id": 1207059 } } try { string app = data.application; DateTime expiresDateTime = Facebook.DateTimeConvertor.FromUnixTime(data.expires_at); AppendLineToOutput(string.Format("UserAccessToken obtained for \"{0}\" app and expires at {1} {2}", app, expiresDateTime.ToShortDateString(), expiresDateTime.ToShortTimeString())); if (data.error != null) { var error = data.error; AppendLineToOutput(string.Format(" ERRORS: code [{0}] subcode[{1}] message [{2}]", error.code, error.subcode, error.message)); } } catch (Exception ex) { AppendLineToOutput(string.Format("No token obtained (error: {0})", ex.Message)); } var msg = "AppAccessToken obtained"; if (AppAccessToken == null) { msg = "No " + msg; } AppendLineToOutput(msg); }); }
internal async static Task GetPageData(string query, int numToProcess, string userAccessToken, CancellationToken cancelToken) { int processedPostsCount = 0; try { do { var fb = new FacebookClient(userAccessToken); //chance to cancel before looping if (cancelToken.IsCancellationRequested) { break; } // hack for Twitter-FB-IB (1 year of posts) project: query += "&since=" + MainForm.DateTimeToUnixTimestamp(new DateTime(2015,09,01)); dynamic postsResult = fb.Get(query); //process single post var data = postsResult.data; if (data == null) { data = new JsonArray(1); data.Add(postsResult); } //process post array if (data != null && data.Count > 0) { var posts = (JsonArray)data; int numInBatch = 0; foreach (dynamic p in posts) { processedPostsCount++; numInBatch++; try { Post post = CreatePostFromJson(p); StringBuilder sb = new StringBuilder(Environment.NewLine); sb.AppendFormat("\t----- PROCESSING #{0} of {1} [batch: {2}/{3}]-----", processedPostsCount, numToProcess, numInBatch, posts.Count); sb.AppendLine(); sb.AppendFormat("\tfrom : {0} (postid: {1})", post.UserName, post.PostId); sb.AppendLine(); sb.AppendLine("\tmsg :" + ShortenText(post.Message)); sb.AppendLine("\tcreated : " + post.CreatedTime); sb.AppendFormat("\tlikes : {0} || comments : {1} || shares {2}", post.NumLikes, post.NumComments, post.NumShares); sb.AppendLine(); sb.Append("\t-------------------------------------------------"); MainForm.LogOutput(sb.ToString()); Task <int> savePostAsyncResult = DbTasks.SavePostAsync(post); // *** disable getting likes . // *** 2016-11-23 //Task<int> likesFromPostResult = Task.Run<int>(() => ProcessLikesFromPost(post, p as object, cancelToken)); Task <int> commentsResult = Task.Run <int>(() => ProcessComments(post, p as object)); //not cancellable #region unused //int[] results = await Task.WhenAll( // DbTasks.SavePostAsync(post) //save post to db async // , Task.Run<int>(() => ProcessLikesFromPost(post, p as object, cancelToken)) //process likes async // , Task.Run<int>(() => ProcessComments(post, p as object)) //process comments async // ); #endregion int numPosts = await savePostAsyncResult; // *** disable getting likes . // *** 2016-11-23 int numComments = await commentsResult; //when comments complete if (!cancelToken.IsCancellationRequested) { MainForm.UpdateCommentNext(""); //all comments were processed successfully } int numLikes = -1; //int numLikes = await likesFromPostResult; //when likes complete //if (!cancelToken.IsCancellationRequested) //{ // MainForm.UpdateLikeNext(""); //all likes were processed successfully //} MainForm.LogOutput(string.Format("#{0} ({1}-{2}) processed with {3} comments and {4} likes", processedPostsCount, post.UserName, post.PostId, numComments, numLikes)); } catch (FacebookApiException ex) { if (ex is FacebookOAuthException) { } if (ex is FacebookApiLimitException) { //this has never been encounted. } // handle all exceptions the same way anyway :( StringBuilder msg = new StringBuilder(ex.Message); msg.AppendLine(); try { var debug = AuthTasks.DebugTokenAsync(App.AppToken, userAccessToken); //{ "data": { "app_id": 138483919580948, "application": "Social Cafe", "expires_at": 1352419328, "is_valid": true, "issued_at": 1347235328, "scopes": [ "email", "publish_actions" ], "user_id": 1207059 } } bool isValid = debug.is_valid; DateTime expiresDateTime = DateTimeConvertor.FromUnixTime(debug.expires_at); msg.AppendFormat("UserAccessToken valid [{0}], expires at [{1}-{2}]", isValid, expiresDateTime.ToShortDateString(), expiresDateTime.ToShortTimeString()); msg.AppendLine(); if (debug.error != null) { var error = debug.error; msg.AppendFormat(" ERRORS: code [{0}] subcode[{1}] message [{2}]", error.code, error.subcode, error.message); msg.AppendLine(); } else { msg.AppendLine(" No token errors"); } } catch (Exception tokenDebugEx) { MainForm.LogOutput(" Error trying to debug UserAccessToken: " + tokenDebugEx.Message); } msg.Append("Sleeping for 30 seconds then continuing"); MainForm.LogOutput(msg.ToString()); Delay(ThirtySeconds); } } } query = postsResult.paging != null ? postsResult.paging.next : null; //chance to cancel after each post if (cancelToken.IsCancellationRequested) { break; } if (query != null) { Delay(FiveSeconds); } } while (query != null && !cancelToken.IsCancellationRequested && processedPostsCount < numToProcess); if (cancelToken.IsCancellationRequested) { MainForm.LogOutput("***** CANCELLED *****"); } else { MainForm.LogOutput(processedPostsCount + " posts processed. No more posts left"); } } catch (Exception ex) { MainForm.LogOutput(ex.Message + Environment.NewLine + ex.StackTrace); } }