public static string ToJson(this MediaItems self) => JsonConvert.SerializeObject(self, Converter.Settings);
async static Task Main(string[] args) { string accessToken = "ya29.Il-0ByW3xPOZasjWYY0OX2fOXooFqzfqDxFrkQ0MH4s8T7xYsiFjFslna4TZOOWIjQwlJ2Ho7TZYMNy5UU-U_lE9OWeJ0OMCpTjEgpY8Kma7C8fhlonzvjfmz57TtfvIDQ"; string nextPageTokenFile = Path.Combine(Environment.CurrentDirectory, "nextToken.txt"); string photosResponseFile = Path.Combine(Environment.CurrentDirectory, "photos.json"); if (!File.Exists(nextPageTokenFile)) { File.CreateText(nextPageTokenFile).Close(); } if (!File.Exists(photosResponseFile)) { File.CreateText(photosResponseFile).Close(); } string nextPageToken = File.ReadAllText(nextPageTokenFile); string baseUrl = "https://photoslibrary.googleapis.com/v1/mediaItems/?pageSize=100"; var stream = new StringWriter(); do { try { using (HttpClient client = new HttpClient()) { client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); var finalUrl = baseUrl; if (!string.IsNullOrEmpty(nextPageToken)) { finalUrl = baseUrl + "&pageToken=" + nextPageToken; } var result = await client.GetAsync(finalUrl); if (result.StatusCode == System.Net.HttpStatusCode.OK) { var responseBody = await result.Content.ReadAsStringAsync(); var mediaItemsCollection = JsonConvert.DeserializeObject <List <MediaItems> >(File.ReadAllText(photosResponseFile), Converter.Settings) ?? new List <MediaItems>(); var mediaItems = MediaItems.FromJson(responseBody); mediaItemsCollection.Add(mediaItems); foreach (var mediaItem in mediaItems.MediaItemsMediaItems) { Debug.WriteLine(mediaItem.Filename); await stream.WriteLineAsync(mediaItem.Filename); } File.WriteAllText(photosResponseFile, JsonConvert.SerializeObject(mediaItemsCollection, Converter.Settings)); nextPageToken = mediaItems.NextPageToken; File.WriteAllText(nextPageTokenFile, nextPageToken); } else { throw new Exception(result.ReasonPhrase); } } } catch (Exception ex) { Console.WriteLine(ex.Message); nextPageToken = string.Empty; } } while (!string.IsNullOrEmpty(nextPageToken)); stream.Flush(); Console.WriteLine("Completed!"); Console.ReadKey(); }