Пример #1
0
        public static async Task AuthPocket(string accessCode = null)
        {
            if (string.IsNullOrEmpty(accessCode) && AuthBrowserAction != null)
            {
                pocketClient = new PocketClient(POCKETCONSUMERKEY)
                {
                    CallbackUri = "https://derekantrican.github.io/authsuccess"
                };
                string requestCode = await pocketClient.GetRequestCode();

                AuthBrowserAction.Invoke(pocketClient.GenerateAuthenticationUri().ToString());

                PocketUser user;
                while (true)
                {
                    try
                    {
                        user = await pocketClient.GetUser(requestCode);

                        break;
                    }
                    catch { }
                    Thread.Sleep(500);
                }

                accessCode = user.Code;
                SaveAccessCodeAction?.Invoke(accessCode);
            }

            pocketClient = new PocketClient(POCKETCONSUMERKEY, accessCode);
        }
Пример #2
0
        private static async Task <int> Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            var    _client     = new PocketClient(_config.PocketConsumerKey, callbackUri: _config.PocketRedirectUri);
            string requestCode = await _client.GetRequestCode();

            Console.WriteLine(_client.GenerateRegistrationUri(requestCode).ToString());
            Console.WriteLine("Press enter after authorizing app...");
            Console.ReadLine();
            PocketUser pocketUser = await _client.GetUser(requestCode);

            IUserService userService = UserService.BuildUserService(_config.StorageConnectionString);

            Console.WriteLine("Input your kindle email:");
            var kindleEmail = Console.ReadLine();

            var user = new User()
            {
                AccessCode         = pocketUser.Code,
                PocketUsername     = pocketUser.Username,
                KindleEmail        = kindleEmail,
                LastProcessingDate = DateTime.UtcNow
            };

            await userService.AddUserAsync(user);

            Console.WriteLine("Bye World!");
            Console.ReadLine();

            return(0);
        }
Пример #3
0
        public async Task <bool> LoginUser()
        {
            var user = await client.GetUser();

            if (user == null)
            {
                return(false);
            }
            SaveCacheUser(user);
            return(true);
        }
Пример #4
0
        public async Task <PocketUser> GetUser(string reqCode)
        {
            try
            {
                var user = await _client.GetUser(reqCode);

                return(new PocketUser(user.Username, user.Code));
            }
            catch (PocketException)
            {
                return(null);
            }
        }
Пример #5
0
        public static async Task <IActionResult> RunAsync(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req,
            ILogger log,
            ExecutionContext context)
        {
            var _config      = new ConfigBuilder(context.FunctionAppDirectory).Build();
            var _emailSender = new MailgunSender(_config.MailGunSenderOptions.ApiKey, _config.MailGunSenderOptions.HostEmail);
            var _client      = new PocketClient(_config.PocketConsumerKey, callbackUri: _config.PocketRedirectUri);

            string          requestBody = new StreamReader(req.Body).ReadToEnd();
            RegisterRequest request     = JsonConvert.DeserializeObject <RegisterRequest>(requestBody);

            if (!IsValidEmail(request.KindleEmail))
            {
                log.LogError($"Not valid email: {request.KindleEmail}.");
                return(new BadRequestObjectResult("email provided is not valid"));
            }

            PocketUser pocketUser = new PocketUser();

            try
            {
                pocketUser = await _client.GetUser(request.RequestCode);
            }
            catch (PocketException pocketException)
            {
                log.LogError($"Something went wrong: {pocketException.Message}.");
                return(new BadRequestObjectResult(pocketException.Message));
            }

            IUserService userService = UserService.BuildUserService(_config.StorageConnectionString);
            await userService.AddUserAsync(new User
            {
                AccessCode         = pocketUser.Code,
                PocketUsername     = pocketUser.Username,
                KindleEmail        = request.KindleEmail,
                LastProcessingDate = DateTime.UtcNow,
                Token = Guid.NewGuid().ToString()
            });

            await SendWelcomeEmail(_emailSender, request.KindleEmail);

            log.LogInformation($"Successfully registered user: {request.KindleEmail}.");

            return(new OkObjectResult("Registration successful"));
        }