コード例 #1
0
        /// <summary>
        /// GetAuthenticationDetailAsync
        /// </summary>
        /// <returns></returns>
        public async Task <OAuth2AccessResponse> GetAuthenticationDetailAsync()
        {
            var mailResponse = new OAuth2AccessResponse();
            var token        = string.Empty;

            try
            {
                token = _clientConfig.MailDataStore.Get($"{_clientConfig.MailDataStore.StoreKey}_GMailToken") as string;

                if (string.IsNullOrEmpty(token))
                {
                    mailResponse.ErrorMessage = "Access Token has expired.";
                }

                if (string.IsNullOrWhiteSpace(token))
                {
                    var refreshData = _clientConfig.MailDataStore.Get($"{_clientConfig.MailDataStore.StoreKey}_GMailRefreshData") as GoogleTokenResponse;

                    if (refreshData != null)
                    {
                        var refreshRquest = new GoogleRefreshTokenRequest()
                        {
                            ClientId     = _clientConfig.OAuth2TokenizeRequestInfo.ClientId,
                            ClientSecret = _clientConfig.OAuth2TokenizeRequestInfo.ClientSecret,
                            RefreshToken = refreshData.RefreshToken
                        };

                        var result = await RefreshToken(_clientConfig.OAuth2TokenizeRequestInfo.TokenEndPointUrl, refreshRquest);

                        if (!string.IsNullOrWhiteSpace(result.AccessToken))
                        {
                            var tokenExpireTime = double.Parse(result.ExpiresIn);
                            var tokenExpireDate = DateTimeOffset.Now.AddSeconds(tokenExpireTime);
                            _clientConfig.MailDataStore.Add($"{_clientConfig.MailDataStore.StoreKey}_GMailToken", result.AccessToken, tokenExpireDate);

                            token = result.AccessToken;
                        }
                    }
                }

                if (string.IsNullOrWhiteSpace(token))
                {
                    mailResponse.ErrorMessage += " Re-authenticate.";
                }
            }
            catch (Exception ex)
            {
                mailResponse.ErrorMessage += ex.Message;
            }

            mailResponse.IsSucces = !string.IsNullOrWhiteSpace(token);
            mailResponse.Token    = token;
            _token = token;
            return(mailResponse);
        }
コード例 #2
0
        /// <summary>
        /// RefreshToken
        /// </summary>
        /// <param name="url"></param>
        /// <param name="postData"></param>
        /// <returns></returns>
        private async Task <GoogleTokenResponse> RefreshToken(string url, GoogleRefreshTokenRequest postData)
        {
            var result    = new GoogleTokenResponse();
            var keyValues = new List <KeyValuePair <string, string> >();

            keyValues.Add(new KeyValuePair <string, string>("refresh_token", postData.RefreshToken));
            keyValues.Add(new KeyValuePair <string, string>("client_id", postData.ClientId));
            keyValues.Add(new KeyValuePair <string, string>("client_secret", postData.ClientSecret));
            keyValues.Add(new KeyValuePair <string, string>("grant_type", "refresh_token"));

            HttpContent cont = new FormUrlEncodedContent(keyValues);

            using (HttpClient client = new HttpClient())
            {
                using (HttpResponseMessage response = await client.PostAsync(url, cont))
                {
                    using (HttpContent content = response.Content)
                    {
                        string data = await content.ReadAsStringAsync();

                        if (!string.IsNullOrWhiteSpace(data))
                        {
                            if (data.Contains("error"))
                            {
                                result.ErrorMessage = data;
                            }
                            else
                            {
                                result = JsonToObject.FromJson <GoogleTokenResponse>(data);
                            }
                        }
                    }
                }
            }

            return(result);
        }