Example #1
0
        /// <summary>
        /// Authenticates and returns user info.
        /// </summary>
        /// <returns>
        /// The user info, including username and token.
        /// </returns>
        public override async Task <UserInfo> Authenticate()
        {
            var uriBuilder = new UriBuilder("https://www.douban.com/service/auth2/auth");

            uriBuilder.AppendAuthenticationCommonFields(ServerConnection);
            uriBuilder.AppendQuery(StringTable.ResponseType, StringTable.Code);
            var redirectedUri = await GetRedirectedUri(uriBuilder.Uri);

            if (redirectedUri != null)
            {
                var    queries = redirectedUri.GetQueries();
                string code;
                if (queries.TryGetValue(StringTable.Code, out code) && !string.IsNullOrEmpty(code))
                {
                    var tokenUrl = new UriBuilder("https://www.douban.com/service/auth2/token");
                    tokenUrl.AppendAuthenticationCommonFields(ServerConnection);
                    tokenUrl.AppendQuery(StringTable.GrantType, StringTable.AuthorizationCode);
                    tokenUrl.AppendQuery(StringTable.Code, code);
                    var jsonContent = await ServerConnection.Post(tokenUrl.Uri, null);

                    return(ParseLogOnResult(jsonContent));
                }
                string error;
                if (queries.TryGetValue(StringTable.Error, out error))
                {
                    throw new ServerException(-1, error);
                }
            }
            throw new OperationCanceledException("User cancelled OAuth.");
        }
Example #2
0
        /// <summary>
        /// Authenticates and returns user info.
        /// </summary>
        /// <returns>
        /// The user info, including username and token.
        /// </returns>
        /// <exception cref="System.InvalidOperationException">
        /// Username is empty
        /// or
        /// Password is empty
        /// </exception>
        public override async Task <UserInfo> Authenticate()
        {
            if (string.IsNullOrWhiteSpace(Username))
            {
                throw new InvalidOperationException("Username is empty");
            }
            if (string.IsNullOrEmpty(Password))
            {
                throw new InvalidOperationException("Password is empty");
            }

            var uriBuilder = new UriBuilder("https://www.douban.com/service/auth2/token");

            uriBuilder.AppendAuthenticationCommonFields(ServerConnection);
            uriBuilder.AppendQuery(StringTable.GrantType, StringTable.Password);
            uriBuilder.AppendQuery(StringTable.Username, Username);
            uriBuilder.AppendQuery(StringTable.Password, Password);
            var data        = uriBuilder.RemoveQuery();
            var jsonContent = await ServerConnection.Post(uriBuilder.Uri, data);

            return(ParseLogOnResult(jsonContent));
        }