Exemplo n.º 1
0
        public void ReceiveAccessToken(string code)
        {
            try
            {
                Logger.Info("Receiving access token");

                // https://developers.google.com/identity/protocols/OAuth2InstalledApp

                var redirectURI = "urn:ietf:wg:oauth:2.0:oob";
                code = WebUtility.UrlEncode(code);

                var url = "https://www.googleapis.com/oauth2/v4/token";

                string postData = $"code={code}";
                postData += $"&client_id={AuthInfo.client_id}";
                postData += $"&client_secret={AuthInfo.client_secret}";
                postData += $"&redirect_uri={redirectURI}";
                postData += $"&grant_type=authorization_code";

                AccessToken            = GAPICommunication.SendRequest <GAPIAccessToken>(url, postData, "POST", null);
                AccessToken.expires_at = DateTime.Now.AddSeconds(Convert.ToDouble(AccessToken.expires_in));

                AccessToken.SaveToFile("token.json");
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw;
            }
        }
Exemplo n.º 2
0
        public void Connect()
        {
            try
            {
                var tokenPath = $"{GAPIBaseObject.AppDataDir}token.json";

                if (File.Exists(tokenPath))
                {
                    AccessToken = GAPIBaseObject.LoadFromFile <GAPIAccessToken>("token.json");

                    if (string.IsNullOrEmpty(AccessToken.refresh_token))
                    {
                        Logger.Info("Empty refresh token");

                        Authenticate();
                    }
                    else
                    {
                        if (AccessToken.expires_at < DateTime.Now)
                        {
                            Logger.Info($"Access token expired at [{AccessToken.expires_at.ToString()}]");

                            RefreshAccessToken();
                        }
                    }
                }
                else
                {
                    Logger.Info("Access token does not exist");

                    Authenticate();
                }

                Logger.Debug($"Access token will expire at [{AccessToken.expires_at.ToString()}]");
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw;
            }
        }