Example #1
0
        private async Task AuthenticateAsync()
        {
            var initializer = new Google.Apis.Auth.OAuth2.Flows.AuthorizationCodeFlow.Initializer(AUTH_ENDPOINT, TOKEN_ENDPOINT)
            {
                ClientSecrets = new Google.Apis.Auth.OAuth2.ClientSecrets {
                    ClientId = CLIENT_ID, ClientSecret = CLIENT_SECRET
                },
            };

            var authorizer = new Google.Apis.Auth.OAuth2.AuthorizationCodeInstalledApp(
                new Google.Apis.Auth.OAuth2.Flows.AuthorizationCodeFlow(initializer),
                new Google.Apis.Auth.OAuth2.LocalServerCodeReceiver(String.Format(Globalization.Strings.Localize("LABEL_CLOSE_AUTH_WINDOW")), Google.Apis.Auth.OAuth2.LocalServerCodeReceiver.CallbackUriChooserStrategy.ForceLocalhost));

            var result = await authorizer.AuthorizeAsync(string.Empty, System.Threading.CancellationToken.None);

            _accessToken = result.Token.AccessToken;
        }
Example #2
0
        public async Task InitializeAsync()
        {
            var initializer = new Google.Apis.Auth.OAuth2.Flows.AuthorizationCodeFlow.Initializer(_authServerUrl, _tokenServerUrl);

            initializer.ClientSecrets = new ClientSecrets()
            {
                ClientId     = "<CLIENTID>",
                ClientSecret = "<CLIENTSECRET>"
            };
            initializer.Scopes    = Scopes;
            initializer.DataStore = new StorageDataStore();
            var flow = new AuthorizationCodeFlow(initializer);
            // try to load access token response from storage
            TokenResponse tokenResponse = null;

            try
            {
                tokenResponse = await flow.LoadTokenAsync(_userId, CancellationToken.None);
            }
            catch (Exception)
            {
            }
            // if access token response is not found, exchange device code for access token
            if (tokenResponse == null)
            {
                TokenRequest tokenRequest = new DeviceCodeTokenRequest()
                {
                    ClientId     = initializer.ClientSecrets.ClientId,
                    ClientSecret = initializer.ClientSecrets.ClientSecret,
                    Code         = _deviceCode,
                    Scope        = Scopes[0]
                };
                tokenResponse = await flow.FetchTokenAsync(_userId, tokenRequest, CancellationToken.None);

                await flow.DataStore.StoreAsync(_userId, tokenResponse);
            }
            UserCredential credential = new UserCredential(flow, _userId, tokenResponse);

            service = new CalendarService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName       = AppName,
            });
        }