public async Task GetTokenWithCode()
        {
            var authHandler = new AuthHandler(Settings.InstanceName);
            var token       = await authHandler.GetTokenInfoAsync(Settings.ClientId, Settings.ClientSecret, Settings.OauthCode);

            Assert.IsTrue(!string.IsNullOrWhiteSpace(token.access_token));
        }
Пример #2
0
        public async Task <string> GetAccessTokenAsync(AppInfoWrapper appInfo, string mastodonName, string mastodonInstance)
        {
            using (var authHandler = new AuthHandler(mastodonInstance))
            {
                //Get Oauth Code
                var oauthCodeUrl = authHandler.GetOauthCodeUrl(appInfo.client_id, AppScopeEnum.Read | AppScopeEnum.Write | AppScopeEnum.Follow);

                var code = "";
                var t    = Task.Factory.StartNew
                           (
                    () =>
                {
                    var mastodonWindows = new MastodonOauth(oauthCodeUrl);
                    mastodonWindows.ShowDialog();
                    code = mastodonWindows.Code;
                },
                    CancellationToken.None,
                    TaskCreationOptions.None,
                    TaskScheduler.FromCurrentSynchronizationContext()
                           );

                t.Wait();

                //Get token
                var token = await authHandler.GetTokenInfoAsync(appInfo.client_id, appInfo.client_secret, code);

                return(token.access_token);
            }
        }
        public async Task GetToken()
        {
            using (var authHandler = new AuthHandler(Settings.InstanceName))
            {
                var tokenInfo = await authHandler.GetTokenInfoAsync(Settings.ClientId, Settings.ClientSecret,
                                                                    Settings.UserEmail, Settings.UserPassword, AppScopeEnum.Read);

                Assert.IsNotNull(tokenInfo);
                Assert.IsTrue(!string.IsNullOrWhiteSpace(tokenInfo.access_token));
            }
        }
        private async Task <string> GetAccessToken()
        {
            if (string.IsNullOrWhiteSpace(Settings.Token))
            {
                var authHandler = new AuthHandler(Settings.InstanceName);
                var tokenInfo   = await authHandler.GetTokenInfoAsync(Settings.ClientId, Settings.ClientSecret, Settings.UserEmail, Settings.UserPassword, AppScopeEnum.Write | AppScopeEnum.Read | AppScopeEnum.Follow);

                Settings.Token = tokenInfo.access_token;
            }

            return(Settings.Token);
        }
        public async Task <bool> FetchAccessToken(string email, string password)
        {
            using (log.BeginScope($"{ nameof(MastodonInstanceService) }->{ nameof(FetchAccessToken) }"))
            {
                // Skip when not registered
                if (!cfg.ClientCredentialsSaved)
                {
                    log.LogError("No clientCredentials given.");
                    return(false);
                }

                var instance = cfg.Application.Instance;

                // Fetch accessToken
                try
                {
                    log.LogDebug("Create AuthHandler");

                    using (var authHandler = new AuthHandler(instance.Name))
                    {
                        var tokenInfo = await authHandler
                                        .GetTokenInfoAsync(instance.ClientId, instance.ClientSecret, email, password, scopes);

                        log.LogDebug("AuthHandler returned");

                        // Is null if registration failed (e.g. if two-factor is enabled)
                        if (tokenInfo.access_token == null)
                        {
                            log.LogError("AccessToken is null");
                            return(false);
                        }

                        // Save token
                        log.LogDebug("Save new config");
                        cfg.Application.Instance.AccessToken = tokenInfo.access_token;
                        cfg.Save();
                    }
                }
                catch (Exception ex)
                {
                    log.LogError(ex, "FetchAccessToken - Exception");
                    return(false);
                }

                return(true);
            }
        }