Esempio n. 1
0
        private static async Task <string> Authorize()
        {
            var patreonApi = new PatreonApi(clientId, clientSecret, redirectURI);

            var settings = new WebListenerSettings();

            settings.UrlPrefixes.Add("http://localhost:3000");

            var uri =
                patreonApi.BuildAuthorizeEndpoint(new List <string>( )
            {
                "identity", "identity.memberships", "campaigns", "campaigns.members"
            });

            string queryString = string.Empty;

            using (WebListener listener = new WebListener(settings))
            {
                listener.Start();

                // Opens request in the browser.
                OpenBrowser(uri.ToString());

                using (var context = await listener.AcceptAsync())
                {
                    byte[] bytes = Encoding.ASCII.GetBytes("<html><head><meta http-equiv=\'refresh\'></head><body>Success</body></html>");
                    context.Response.ContentLength = bytes.Length;
                    context.Response.ContentType   = "text/html";

                    await context.Response.Body.WriteAsync(bytes, 0, bytes.Length);

                    queryString = context.Request.QueryString;
                }
            }

            var queryDictionary = HttpUtility.ParseQueryString(queryString);

            // Checks for errors.
            if (queryDictionary.GetValues("error")?.FirstOrDefault() != null)
            {
                return(string.Empty);
            }
            if (queryDictionary.GetValues("code")?.FirstOrDefault() == null)
            {
                return(string.Empty);
            }

            // extracts the code
            return(queryDictionary.GetValues("code").FirstOrDefault());
        }
Esempio n. 2
0
        public async Task <IActionResult> Current([FromServices] IMemoryCache cache,
                                                  [FromServices] PatreonApi patreonApi, [FromServices] ILogger <GoalsController> logger)
        {
            var currentGoal = cache.Get <PatreonGoal>("patreonCurrentGoal");

            if (currentGoal == null)
            {
                try
                {
                    currentGoal = await patreonApi.GetCurrentGoalAsync();

                    cache.Set("patreonCurrentGoal", currentGoal, TimeSpan.FromHours(1));
                }
                catch (Exception ex)
                {
                    logger.LogError($"Error while loading patreon goals: {ex.Message}");
                }
            }
            return(Ok(currentGoal));
        }
Esempio n. 3
0
        static async Task MainAsync()
        {
            var code = await Authorize();

            var patreonApi = new PatreonApi(clientId, clientSecret, redirectURI);
            var tokenData  = await patreonApi.PerformCodeExchange(code);

            patreonApi = new PatreonApi(tokenData.AccessToken);

            var fields = new Fields();

            fields.AddAllField <User>();
            fields.AddAllField <Campaign>();
            var includes = new Includes();

            includes.Add <User>(x => x.Campaign);

            var user = await patreonApi.FetchUser(fields, includes);

            Console.ReadLine();
        }