/// <summary>
        /// Initializes a new instance of the <see cref="WeiboDataProvider"/> class.
        /// Constructor.
        /// </summary>
        /// <param name="tokens">OAuth tokens for request.</param>
        /// <param name="authenticationBroker">Authentication result interface.</param>
        /// <param name="passwordManager">Platform password manager</param>
        /// <param name="storageManager">Platform storage provider</param>
        public WeiboDataProvider(WeiboOAuthTokens tokens, IAuthenticationBroker authenticationBroker, IPasswordManager passwordManager, IStorageManager storageManager)
        {
            if (string.IsNullOrEmpty(tokens.AppSecret))
            {
                throw new ArgumentException("Missing app secret");
            }

            if (string.IsNullOrEmpty(tokens.AppKey))
            {
                throw new ArgumentException("Missing app key");
            }

            if (string.IsNullOrEmpty(tokens.RedirectUri))
            {
                throw new ArgumentException("Missing redirect uri");
            }

            _tokens = tokens;
            _authenticationBroker = authenticationBroker ?? throw new ArgumentException("Invalid AuthenticationBroker");
            _passwordManager      = passwordManager ?? throw new ArgumentException("Invalid PasswordManager");
            _storageManager       = storageManager ?? throw new ArgumentException("Invalid StorageManager");
            if (_client == null)
            {
                HttpClientHandler handler = new HttpClientHandler {
                    AutomaticDecompression = DecompressionMethods.GZip
                };
                _client = new HttpClient(handler);
            }
        }
        /// <summary>
        /// HTTP Post request to specified Uri.
        /// </summary>
        /// <param name="requestUri">Uri to make OAuth request.</param>
        /// <param name="tokens">Tokens to pass in request.</param>
        /// <param name="status">Status text.</param>
        /// <param name="content">Data to post to server.</param>
        /// <returns>String result.</returns>
        public async Task <WeiboStatus> ExecutePostMultipartAsync(Uri requestUri, WeiboOAuthTokens tokens, string status, byte[] content)
        {
            try
            {
                using (var multipartFormDataContent = new MultipartFormDataContent())
                {
                    using (var stringContent = new StringContent(status))
                    {
                        multipartFormDataContent.Add(stringContent, "status");
                        using (var byteContent = new ByteArrayContent(content))
                        {
                            // Somehow Weibo's backend requires a Filename field to work
                            byteContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
                            {
                                FileName = "attachment", Name = "pic"
                            };
                            multipartFormDataContent.Add(byteContent, "pic");

                            using (var request = new HttpRequestMessage(HttpMethod.Post, requestUri))
                            {
                                UriBuilder requestUriBuilder = new UriBuilder(request.RequestUri);
                                if (requestUriBuilder.Query.StartsWith("?"))
                                {
                                    requestUriBuilder.Query = requestUriBuilder.Query.Substring(1) + "&access_token=" + OAuthEncoder.UrlEncode(tokens.AccessToken);
                                }
                                else
                                {
                                    requestUriBuilder.Query = requestUriBuilder.Query + "access_token=" + OAuthEncoder.UrlEncode(tokens.AccessToken);
                                }

                                request.RequestUri = requestUriBuilder.Uri;

                                request.Content = multipartFormDataContent;

                                using (var response = await _client.SendAsync(request).ConfigureAwait(false))
                                {
                                    if (response.StatusCode == HttpStatusCode.OK)
                                    {
                                        return(JsonConvert.DeserializeObject <WeiboStatus>(await response.Content.ReadAsStringAsync().ConfigureAwait(false)));
                                    }
                                    else
                                    {
                                        response.ThrowIfNotValid();
                                        ProcessError(await response.Content.ReadAsStringAsync().ConfigureAwait(false));
                                        return(null);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (ObjectDisposedException)
            {
                // known issue
                // http://stackoverflow.com/questions/39109060/httpmultipartformdatacontent-dispose-throws-objectdisposedexception
            }

            return(null);
        }
Example #3
0
        /// <summary>
        /// Initialize underlying provider with relevant token information.
        /// </summary>
        /// <param name="oAuthTokens">Token instance.</param>
        /// <param name="authenticationBroker">Authentication result interface.</param>
        /// <param name="passwordManager">Password Manager interface, store the password.</param>
        /// <param name="storageManager">Storage Manager interface</param>
        /// <returns>Success or failure.</returns>
        public bool Initialize(WeiboOAuthTokens oAuthTokens, IAuthenticationBroker authenticationBroker, IPasswordManager passwordManager, IStorageManager storageManager)
        {
            _tokens = oAuthTokens ?? throw new ArgumentNullException(nameof(oAuthTokens));
            this._authenticationBroker = authenticationBroker ?? throw new ArgumentNullException(nameof(authenticationBroker));
            this._passwordManager      = passwordManager ?? throw new ArgumentNullException(nameof(passwordManager));
            this._storageManager       = storageManager ?? throw new ArgumentNullException(nameof(storageManager));

            _isInitialized = true;

            _weiboDataProvider = null;

            return(true);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="WeiboDataProvider"/> class.
 /// Constructor.
 /// </summary>
 /// <param name="tokens">OAuth tokens for request.</param>
 /// <param name="authenticationBroker">Authentication result interface.</param>
 /// <param name="passwordManager">Platform password manager</param>
 /// <param name="storageManager">Platform storage provider</param>
 public WeiboDataProvider(WeiboOAuthTokens tokens, IAuthenticationBroker authenticationBroker, IPasswordManager passwordManager, IStorageManager storageManager)
 {
     _tokens = tokens;
     _authenticationBroker = authenticationBroker;
     _passwordManager      = passwordManager;
     _storageManager       = storageManager;
     if (_client == null)
     {
         HttpClientHandler handler = new HttpClientHandler {
             AutomaticDecompression = DecompressionMethods.GZip
         };
         _client = new HttpClient(handler);
     }
 }
Example #5
0
        /// <summary>
        /// Initialize underlying provider with relevant token information.
        /// </summary>
        /// <param name="appKey">App key.</param>
        /// <param name="appSecret">App secret.</param>
        /// <param name="redirectUri">Redirect URI. Has to match redirect URI defined at open.weibo.com/apps (can be arbitrary).</param>
        /// <param name="authenticationBroker">Authentication result interface.</param>
        /// <param name="passwordManager">Password Manager interface, store the password.</param>
        /// <param name="storageManager">Storage Manager interface</param>
        /// <returns>Success or failure.</returns>
        public bool Initialize(string appKey, string appSecret, string redirectUri, IAuthenticationBroker authenticationBroker, IPasswordManager passwordManager, IStorageManager storageManager)
        {
            if (string.IsNullOrEmpty(appKey))
            {
                throw new ArgumentNullException(nameof(appKey));
            }

            if (string.IsNullOrEmpty(appSecret))
            {
                throw new ArgumentNullException(nameof(appSecret));
            }

            if (string.IsNullOrEmpty(redirectUri))
            {
                throw new ArgumentNullException(nameof(redirectUri));
            }

            if (authenticationBroker == null)
            {
                throw new ArgumentException(nameof(authenticationBroker));
            }

            if (passwordManager == null)
            {
                throw new ArgumentException(nameof(passwordManager));
            }

            if (storageManager == null)
            {
                throw new ArgumentException(nameof(storageManager));
            }

            var oAuthTokens = new WeiboOAuthTokens
            {
                AppKey      = appKey,
                AppSecret   = appSecret,
                RedirectUri = redirectUri
            };

            return(Initialize(oAuthTokens, authenticationBroker, passwordManager, storageManager));
        }
        /// <summary>
        /// HTTP Get request to specified Uri.
        /// </summary>
        /// <param name="requestUri">Uri to make OAuth request.</param>
        /// <param name="tokens">Tokens to pass in request.</param>
        /// <returns>String result.</returns>
        public async Task <string> ExecuteGetAsync(Uri requestUri, WeiboOAuthTokens tokens)
        {
            using HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, requestUri);
            UriBuilder requestUriBuilder = new UriBuilder(request.RequestUri);

            if (requestUriBuilder.Query.StartsWith("?"))
            {
                requestUriBuilder.Query = requestUriBuilder.Query.Substring(1) + "&access_token=" + OAuthEncoder.UrlEncode(tokens.AccessToken);
            }
            else
            {
                requestUriBuilder.Query = requestUriBuilder.Query + "?access_token=" + OAuthEncoder.UrlEncode(tokens.AccessToken);
            }

            request.RequestUri = requestUriBuilder.Uri;

            using HttpResponseMessage response = await _client.SendAsync(request).ConfigureAwait(false);

            response.ThrowIfNotValid();
            return(ProcessError(await response.Content.ReadAsStringAsync().ConfigureAwait(false)));
        }
        /// <summary>
        /// HTTP Post request to specified Uri.
        /// </summary>
        /// <param name="requestUri">Uri to make OAuth request.</param>
        /// <param name="tokens">Tokens to pass in request.</param>
        /// <param name="status">Status text.</param>
        /// <returns>String result.</returns>
        public async Task <WeiboStatus> ExecutePostAsync(Uri requestUri, WeiboOAuthTokens tokens, string status)
        {
            var contentDict = new Dictionary <string, string>();

            contentDict.Add("status", status);

            using (var formUrlEncodedContent = new FormUrlEncodedContent(contentDict))
            {
                using (var request = new HttpRequestMessage(HttpMethod.Post, requestUri))
                {
                    UriBuilder requestUriBuilder = new UriBuilder(request.RequestUri);
                    if (requestUriBuilder.Query.StartsWith("?"))
                    {
                        requestUriBuilder.Query = requestUriBuilder.Query.Substring(1) + "&access_token=" + OAuthEncoder.UrlEncode(tokens.AccessToken);
                    }
                    else
                    {
                        requestUriBuilder.Query = requestUriBuilder.Query + "access_token=" + OAuthEncoder.UrlEncode(tokens.AccessToken);
                    }

                    request.RequestUri = requestUriBuilder.Uri;

                    request.Content = formUrlEncodedContent;

                    using (var response = await _client.SendAsync(request).ConfigureAwait(false))
                    {
                        if (response.StatusCode == HttpStatusCode.OK)
                        {
                            return(JsonConvert.DeserializeObject <WeiboStatus>(await response.Content.ReadAsStringAsync().ConfigureAwait(false)));
                        }
                        else
                        {
                            response.ThrowIfNotValid();
                            ProcessError(await response.Content.ReadAsStringAsync().ConfigureAwait(false));
                            return(null);
                        }
                    }
                }
            }
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="WeiboDataProvider"/> class.
 /// Constructor.
 /// </summary>
 /// <param name="tokens">OAuth tokens for request.</param>
 public WeiboDataProvider(WeiboOAuthTokens tokens)
     : this(tokens, new NetFrameworkAuthenticationBroker(), new NetFrameworkPasswordManager(), new NetFrameworkStorageManager())
 {
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="WeiboDataProvider"/> class.
 /// Constructor.
 /// </summary>
 /// <param name="tokens">OAuth tokens for request.</param>
 public WeiboDataProvider(WeiboOAuthTokens tokens)
     : this(tokens, new UwpAuthenticationBroker(), new UwpPasswordManager(), new UwpStorageManager())
 {
 }
Example #10
0
 /// <summary>
 /// Initialize underlying provider with relevant token information.
 /// </summary>
 /// <param name="oAuthTokens">Token instance.</param>
 /// <returns>Success or failure.</returns>
 public bool Initialize(WeiboOAuthTokens oAuthTokens)
 {
     return(Initialize(oAuthTokens, new UwpAuthenticationBroker(), new UwpPasswordManager(), new UwpStorageManager()));
 }