Пример #1
0
        public static OAuth20Token FromJson(string json)
        {
            if (string.IsNullOrEmpty(json)) return null;
            var parser = JObject.Parse(json);
            if (parser == null) return null;

            var accessToken = parser.Value<string>("access_token");

            if (string.IsNullOrEmpty(accessToken))
                return null;

            var token = new OAuth20Token
                {
                    AccessToken = accessToken,
                    RefreshToken = parser.Value<string>("refresh_token"),
                    ClientID = parser.Value<string>("client_id"),
                    ClientSecret = parser.Value<string>("client_secret"),
                    RedirectUri = parser.Value<string>("redirect_uri"),
                };

            long expiresIn;
            if (long.TryParse(parser.Value<string>("expires_in"), out expiresIn))
                token.ExpiresIn = expiresIn;

            DateTime timestamp;
            token.Timestamp =
                DateTime.TryParse(parser.Value<string>("timestamp"), out timestamp)
                    ? timestamp
                    : DateTime.UtcNow;

            return token;
        }
Пример #2
0
        private void Copy(OAuth20Token oAuth20Token)
        {
            if (oAuth20Token == null) return;

            AccessToken = oAuth20Token.AccessToken;
            RefreshToken = oAuth20Token.RefreshToken;
            ExpiresIn = oAuth20Token.ExpiresIn;
            ClientID = oAuth20Token.ClientID;
            ClientSecret = oAuth20Token.ClientSecret;
            RedirectUri = oAuth20Token.RedirectUri;
            Timestamp = oAuth20Token.Timestamp;
        }
Пример #3
0
        public static OAuth20Token FromJson(string json)
        {
            if (string.IsNullOrEmpty(json))
            {
                return(null);
            }
            var parser = JObject.Parse(json);

            if (parser == null)
            {
                return(null);
            }

            var accessToken = parser.Value <string>("access_token");

            if (string.IsNullOrEmpty(accessToken))
            {
                return(null);
            }

            var token = new OAuth20Token
            {
                AccessToken  = accessToken,
                RefreshToken = parser.Value <string>("refresh_token"),
                ClientID     = parser.Value <string>("client_id"),
                ClientSecret = parser.Value <string>("client_secret"),
                RedirectUri  = parser.Value <string>("redirect_uri"),
                OriginJson   = json,
            };

            long expiresIn;

            if (long.TryParse(parser.Value <string>("expires_in"), out expiresIn))
            {
                token.ExpiresIn = expiresIn;
            }

            try
            {
                token.Timestamp =
                    !string.IsNullOrEmpty(parser.Value <string>("timestamp"))
                        ? parser.Value <DateTime>("timestamp")
                        : DateTime.UtcNow;
            }
            catch (Exception)
            {
                token.Timestamp = DateTime.MinValue;
            }

            return(token);
        }
        public GoogleDriveProviderInfo(int id, string providerKey, string customerTitle, string token, Guid owner, FolderType rootFolderType, DateTime createOn)
        {
            if (string.IsNullOrEmpty(providerKey)) throw new ArgumentNullException("providerKey");
            if (string.IsNullOrEmpty(token)) throw new ArgumentException("Token can't be null");

            ID = id;
            CustomerTitle = customerTitle;
            Owner = owner == Guid.Empty ? SecurityContext.CurrentAccount.ID : owner;

            ProviderKey = providerKey;
            _token = OAuth20Token.FromJson(token);
            _rootFolderType = rootFolderType;
            _createOn = createOn;
        }
Пример #5
0
        private void Copy(OAuth20Token oAuth20Token)
        {
            if (oAuth20Token == null)
            {
                return;
            }

            AccessToken  = oAuth20Token.AccessToken;
            RefreshToken = oAuth20Token.RefreshToken;
            ExpiresIn    = oAuth20Token.ExpiresIn;
            ClientID     = oAuth20Token.ClientID;
            ClientSecret = oAuth20Token.ClientSecret;
            RedirectUri  = oAuth20Token.RedirectUri;
            Timestamp    = oAuth20Token.Timestamp;
        }
Пример #6
0
        public static OAuth20Token FromJson(string json)
        {
            if (string.IsNullOrEmpty(json))
            {
                return(null);
            }
            var parser = JObject.Parse(json);

            if (parser == null)
            {
                return(null);
            }

            var accessToken = parser.Value <string>("access_token");

            if (string.IsNullOrEmpty(accessToken))
            {
                return(null);
            }

            var token = new OAuth20Token
            {
                AccessToken  = accessToken,
                RefreshToken = parser.Value <string>("refresh_token"),
                ClientID     = parser.Value <string>("client_id"),
                ClientSecret = parser.Value <string>("client_secret"),
                RedirectUri  = parser.Value <string>("redirect_uri"),
            };

            long expiresIn;

            if (long.TryParse(parser.Value <string>("expires_in"), out expiresIn))
            {
                token.ExpiresIn = expiresIn;
            }

            DateTime timestamp;

            token.Timestamp =
                DateTime.TryParse(parser.Value <string>("timestamp"), out timestamp)
                    ? timestamp
                    : DateTime.UtcNow;

            return(token);
        }
        private void ImportContacts(OAuth20Token token)
        {
            var doc = RequestContacts(token);

            //selecting from xdocument
            var contacts = from e in doc.Root.Elements("{http://www.w3.org/2005/Atom}entry")
                           select new
                               {
                                   Name = e.Element("{http://www.w3.org/2005/Atom}title").Value,
                                   Email = from a in e.Elements("{http://schemas.google.com/g/2005}email")
                                           where a.Attribute("address") != null
                                           select a.Attribute("address").Value
                               };
            foreach (var contact in contacts)
            {
                AddContactInfo(contact.Name, contact.Email);
            }
        }
Пример #8
0
 public OAuth20Token(OAuth20Token oAuth20Token)
 {
     Copy(oAuth20Token);
 }
Пример #9
0
        public void Open(OAuth20Token token)
        {
            if (IsOpened)
                return;

            if (token == null) throw new UnauthorizedAccessException("Cannot create GoogleDrive session with given token");
            if (token.IsExpired) token = OAuth20TokenHelper.RefreshToken(GoogleUrlToken, token);
            _token = token;

            var tokenResponse = new TokenResponse
                {
                    AccessToken = _token.AccessToken,
                    RefreshToken = _token.RefreshToken,
                    Issued = _token.Timestamp,
                    ExpiresInSeconds = _token.ExpiresIn,
                    TokenType = "Bearer"
                };

            var apiCodeFlow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
                {
                    ClientSecrets = new ClientSecrets
                        {
                            ClientId = _token.ClientID,
                            ClientSecret = _token.ClientSecret
                        },
                    Scopes = new[] { DriveService.Scope.Drive }
                });

            _driveService = new DriveService(new BaseClientService.Initializer
                {
                    HttpClientInitializer = new UserCredential(apiCodeFlow, string.Empty, tokenResponse)
                });

            IsOpened = true;
        }
Пример #10
0
 public OAuth20Token(OAuth20Token oAuth20Token)
 {
     Copy(oAuth20Token);
 }
Пример #11
0
 public Token(OAuth20Token oAuth20Token)
     : base(oAuth20Token)
 {
 }
Пример #12
0
 private static string EncryptToken(OAuth20Token token)
 {
     var t = token.ToJson();
     return string.IsNullOrEmpty(t) ? string.Empty : InstanceCrypto.Encrypt(t);
 }
        public XDocument RequestContacts(OAuth20Token token)
        {
            var response = RequestHelper.PerformRequest(GoogleContactsUrl, headers: new Dictionary<string, string> { { "Authorization", "Bearer " + token.AccessToken } });

            return XDocument.Parse(response);
        }