Esempio n. 1
0
        /// <summary>
        /// Gets the OAuth token for the user associated with the provided code.
        /// </summary>
        /// <param name="code">Sent by reddit as a parameter in the return uri.</param>
        /// <param name="isRefresh">Set to true for refresh requests.</param>
        /// <returns></returns>
        public async Task <string> GetOAuthTokenAsync(string code, bool isRefresh = false)
        {
            var json = await _webAgent.ExecuteRequestAsync(() =>
            {
                var request = _webAgent.CreateRequest(AccessUrl, "POST");

                request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.UTF8.GetBytes(_clientId + ":" + _clientSecret)));

                if (isRefresh)
                {
                    _webAgent.WritePostBody(request, new
                    {
                        grant_type    = "refresh_token",
                        refresh_token = code
                    });
                }
                else
                {
                    _webAgent.WritePostBody(request, new
                    {
                        grant_type = "authorization_code",
                        code,
                        redirect_uri = _redirectUri
                    });
                }

                return(request);
            }).ConfigureAwait(false);

            return(json["access_token"].ToString());

            throw new AuthenticationException(json.ToString());
        }
Esempio n. 2
0
        /// <summary>
        /// Gets the OAuth token for the user associated with the provided code.
        /// </summary>
        /// <param name="code">Sent by reddit as a parameter in the return uri.</param>
        /// <param name="isRefresh">Set to true for refresh requests.</param>
        /// <returns></returns>
        public async Task <string> GetOAuthTokenAsync(string code, bool isRefresh = false)
        {
            //TODO test mono and make sure this works without security issues. Shouldn't be handled by library, should require install of cert or at least explicit calls to ingore certs
            //if (Type.GetType("Mono.Runtime") != null)
            //    ServicePointManager.ServerCertificateValidationCallback = (s, c, ch, ssl) => true;

            var json = await _webAgent.ExecuteRequestAsync(() => {
                var request = _webAgent.CreateRequest(AccessUrl, "POST");
                request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.UTF8.GetBytes(_clientId + ":" + _clientSecret)));
                if (isRefresh)
                {
                    _webAgent.WritePostBody(request, new
                    {
                        grant_type    = "refresh_token",
                        refresh_token = code
                    });
                }
                else
                {
                    _webAgent.WritePostBody(request, new
                    {
                        grant_type = "authorization_code",
                        code,
                        redirect_uri = _redirectUri
                    });
                }
                return(request);
            }).ConfigureAwait(false);

            if (json["access_token"] != null)
            {
                return(json["access_token"].ToString());
            }
            throw new AuthenticationException("Could not log in.");
        }