Ejemplo n.º 1
0
        public async Task <string> GetToken()
        {
            if (!m_IsInit)
            {
                throw new InvalidOperationException($"{GetType().Name} must be initialised before first usage. Ensure you call {nameof(Init)}() first");
            }
            ;

            if (m_CurrentAuthState.IsExpiring())
            {
                m_CurrentAuthState = await m_GoogleLogin.GetNewAccessToken(m_CurrentAuthState);
            }

            return(await Task.FromResult(m_CurrentAuthState.AccessToken));
        }
Ejemplo n.º 2
0
        public async Task <bool> Init()
        {
            m_Logger.LogDebug("Debug Check");
            m_Logger.LogInformation("Authenticating");
            m_CurrentAuthState = await m_GoogleLogin.DoOAuth(m_GoogleScope);

            if (m_CurrentAuthState == null)
            {
                m_Logger.LogError("Authentication Failed");
                return(false);
            }

            m_Logger.LogInformation("Authentication succeeded");
            m_IsInit = true;
            return(true);
        }
Ejemplo n.º 3
0
        public async Task <GoogleAuthState> GetNewAccessToken(GoogleAuthState googleAuthState)
        {
            Output("Getting new access token via refresh token...");

            // builds the  request
            // ReSharper disable once UseStringInterpolation
            var tokenRequestBody = string.Format("client_id={0}&client_secret={1}&refresh_token={2}&grant_type=refresh_token",
                                                 m_ClientId,
                                                 m_ClientSecret,
                                                 googleAuthState.RefreshToken
                                                 );

            // sends the request
            var tokenRequest = (HttpWebRequest)WebRequest.Create(_TOKEN_ENDPOINT);

            tokenRequest.Method      = "POST";
            tokenRequest.ContentType = "application/x-www-form-urlencoded";
            tokenRequest.Accept      = "Accept=text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
            var byteVersion = Encoding.ASCII.GetBytes(tokenRequestBody);

            tokenRequest.ContentLength = byteVersion.Length;
            var stream = tokenRequest.GetRequestStream();
            await stream.WriteAsync(byteVersion, 0, byteVersion.Length);

            stream.Close();

            try
            {
                // gets the response
                var tokenResponse = await tokenRequest.GetResponseAsync();

                // ReSharper disable once AssignNullToNotNullAttribute
                using var reader = new StreamReader(tokenResponse.GetResponseStream());
                // reads response body
                var responseText = await reader.ReadToEndAsync();

                Output(responseText);

                // converts to dictionary
                var tokenEndpointDecoded = JsonConvert.DeserializeObject <Dictionary <string, string> >(responseText);

                return(new GoogleAuthState(tokenEndpointDecoded["access_token"],
                                           int.Parse(tokenEndpointDecoded["expires_in"]),
                                           googleAuthState.RefreshToken
                                           ));
            }
            catch (WebException ex)
            {
                if (ex.Status != WebExceptionStatus.ProtocolError)
                {
                    return(null);
                }

                if (!(ex.Response is HttpWebResponse response))
                {
                    return(null);
                }

                Output("HTTP: " + response.StatusCode);
                // ReSharper disable once AssignNullToNotNullAttribute
                using var reader = new StreamReader(response.GetResponseStream());
                var responseText = await reader.ReadToEndAsync();

                Output(responseText);
            }

            return(null);
        }