Exemple #1
0
        private async Task _doOauthAsync()
        {
            // AccessToken is existed, if it is not expired, use it directly, otherwise refresh it.
            if (!string.IsNullOrEmpty(_oauthWrapper.Provider.AccessToken))
            {
                if (!_oauthWrapper.IsAccessTokenExpired)
                {
                    return;
                }

                TextStatus.Text = "Refreshing access token ...";
                try
                {
                    await _oauthWrapper.RefreshAccessTokenAsync();

                    return;
                }
                catch
                {
                    TextStatus.Text = "Failed to refresh access token, try to get a new access token ...";
                }
            }

            OauthBrowser.Navigate(
                new Uri(_oauthWrapper.Provider.GetFullAuthUri())
                );

            ShowOauthPanel(true);
            while (OauthViewer.Visibility != Visibility.Collapsed)
            {
                await Task.Delay(100);
            }

            TextStatus.Text = "Requesting access token ...";
            await _oauthWrapper.RequestAccessTokenAndUserEmailAsync();
        }
Exemple #2
0
        private async Task _doOauthAsync()
        {
            if (_oauthWrapper == null)
            {
                /*
                 *  To use Google OAUTH in your application, you must create a project in Google Developers Console.
                 *
                 * - Create your project at https://console.developers.google.com/project.
                 * - Select your project -> APIs & Services -> Dashboard -> Credentials;
                 * - Credentials -> Create Credentials -> OAuth client ID -> Web application or Other (Desktop Application).
                 *  It depends on your application type.
                 *
                 * - Input a name for your application, input your current ASP/ASP.NET URL at Authorized redirect URIs,
                 *  for example: http://localhost/gmailoauth/default.aspx. (Desktop Application doesn't require this step)
                 *  Click "Create", you will get your client id and client secret.
                 *
                 * - Finally you can also set detail information for your project at Credentials -> OAuth consent screen.
                 * - If you used https://mail.google.com scope, you should verify your application that is inroduced in cosent screen.
                 *  If you don't verify your application, your application is limited by some conditions.
                 *
                 * You must apply for your client id and client secret, don't use the client id in the sample project, because it is limited now.
                 * If you got "This app isn't verified" information, please click "advanced" -> Go to ... for test.
                 */
                OauthProviderInterface provider = GoogleOauthProvider.Create("1072602369179-aru4rj97ateiho9rt4pf5i8l1r01mc16.apps.googleusercontent.com",
                                                                             "Lnw8r5FvfKFNS_CSEucbdIE-");
                _oauthWrapper = new OauthDesktopWrapper(provider);
            }

            // AccessToken is existed, if it is not expired, use it directly, otherwise refresh it.
            if (!string.IsNullOrEmpty(_oauthWrapper.OauthProvider.AccessToken))
            {
                if (!_oauthWrapper.IsAccessTokenExpired)
                {
                    return;
                }

                TextStatus.Text = "Refreshing access token ...";
                try
                {
                    await _oauthWrapper.RefreshAccessTokenAsync();

                    return;
                }
                catch
                {
                    TextStatus.Text = "Failed to refresh access token, try to get a new access token ...";
                }
            }

            OauthBrowser.Navigate(
                new Uri(_oauthWrapper.OauthProvider.GenerateFullAuthUri())
                );

            ShowOauthPanel(true);
            while (OauthViewer.Visibility != Visibility.Collapsed)
            {
                await Task.Delay(100);
            }

            TextStatus.Text = "Requesting access token ...";
            await _oauthWrapper.RequestAccessTokenAndUserEmailAsync();
        }