コード例 #1
0
        // log in to all servers
        public async Task Login(DiscordServer server)
        {
            var credentialPath = $@"{_credentialPathPrefix}/{server.ServerId}";

            using (var stream = new FileStream(_filePath, FileMode.Open, FileAccess.Read))
            {
                // build code flow manager to authenticate token
                var flowManager = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
                {
                    ClientSecrets = GoogleClientSecrets.Load(stream).Secrets,
                    Scopes        = _scopes,
                    DataStore     = new FileDataStore(credentialPath, true)
                });

                var fileDataStore = new FileDataStore(credentialPath, true);
                var token         = await fileDataStore.GetAsync <TokenResponse>("token");

                // load token from file
                // var token = await flowManager.LoadTokenAsync(_userId, CancellationToken.None).ConfigureAwait(false);

                // check if we need to get a new token
                if (flowManager.ShouldForceTokenRetrieval() || token == null ||
                    token.RefreshToken == null && token.IsExpired(flowManager.Clock))
                {
                    return;
                }

                // set credentials to use for syncing
                server.GoogleUserCredential = new UserCredential(flowManager, _userId, token);
            }
        }