public ActionResult ApplicationError(string token)
        {
            var model = new TokenErrorModel
            {
                Token = token
            };

            return View(model);
        }
Beispiel #2
0
        /// <summary>
        /// Exchanges a temporary OAuth verifier code for a workspace token.
        /// <see href="https://api.slack.com/methods/oauth.token" />
        /// </summary>
        /// <param name='clientSecret'>
        /// Issued when you created your application.
        /// </param>
        /// <param name='code'>
        /// The `code` param returned via the OAuth callback.
        /// </param>
        /// <param name='singleChannel'>
        /// Request the user to add your app only to a single channel.
        /// </param>
        /// <param name='clientId'>
        /// Issued when you created your application.
        /// </param>
        /// <param name='redirectUri'>
        /// This must match the originally submitted URI (if one was sent).
        /// </param>
        /// <param name='customHeaders'>
        /// Headers that will be added to request.
        /// </param>
        /// <param name='cancellationToken'>
        /// The cancellation token.
        /// </param>
        /// <exception cref="TokenErrorModelException">
        /// Thrown when the operation returned an invalid status code
        /// </exception>
        /// <exception cref="SerializationException">
        /// Thrown when unable to deserialize the response
        /// </exception>
        /// <return>
        /// A response object containing the response body and response headers.
        /// </return>
        public async Task <HttpOperationResponse <TokenOKResponse> > TokenWithHttpMessagesAsync(string clientSecret = default(string), string code = default(string), bool?singleChannel = default(bool?), string clientId = default(string), string redirectUri = default(string), Dictionary <string, List <string> > customHeaders = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            // Tracing
            bool   _shouldTrace  = ServiceClientTracing.IsEnabled;
            string _invocationId = null;

            if (_shouldTrace)
            {
                _invocationId = ServiceClientTracing.NextInvocationId.ToString();
                Dictionary <string, object> tracingParameters = new Dictionary <string, object>();
                tracingParameters.Add("clientSecret", clientSecret);
                tracingParameters.Add("code", code);
                tracingParameters.Add("singleChannel", singleChannel);
                tracingParameters.Add("clientId", clientId);
                tracingParameters.Add("redirectUri", redirectUri);
                tracingParameters.Add("cancellationToken", cancellationToken);
                ServiceClientTracing.Enter(_invocationId, this, "Token", tracingParameters);
            }
            // Construct URL
            var           _baseUrl         = Client.BaseUri.AbsoluteUri;
            var           _url             = new System.Uri(new System.Uri(_baseUrl + (_baseUrl.EndsWith("/") ? "" : "/")), "oauth.token").ToString();
            List <string> _queryParameters = new List <string>();

            if (clientSecret != null)
            {
                _queryParameters.Add(string.Format("client_secret={0}", System.Uri.EscapeDataString(clientSecret)));
            }
            if (code != null)
            {
                _queryParameters.Add(string.Format("code={0}", System.Uri.EscapeDataString(code)));
            }
            if (singleChannel != null)
            {
                _queryParameters.Add(string.Format("single_channel={0}", System.Uri.EscapeDataString(Microsoft.Rest.Serialization.SafeJsonConvert.SerializeObject(singleChannel, Client.SerializationSettings).Trim('"'))));
            }
            if (clientId != null)
            {
                _queryParameters.Add(string.Format("client_id={0}", System.Uri.EscapeDataString(clientId)));
            }
            if (redirectUri != null)
            {
                _queryParameters.Add(string.Format("redirect_uri={0}", System.Uri.EscapeDataString(redirectUri)));
            }
            if (_queryParameters.Count > 0)
            {
                _url += "?" + string.Join("&", _queryParameters);
            }
            // Create HTTP transport objects
            var _httpRequest = new HttpRequestMessage();
            HttpResponseMessage _httpResponse = null;

            _httpRequest.Method     = new HttpMethod("GET");
            _httpRequest.RequestUri = new System.Uri(_url);
            // Set Headers


            if (customHeaders != null)
            {
                foreach (var _header in customHeaders)
                {
                    if (_httpRequest.Headers.Contains(_header.Key))
                    {
                        _httpRequest.Headers.Remove(_header.Key);
                    }
                    _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value);
                }
            }

            // Serialize Request
            string _requestContent = null;

            // Send Request
            if (_shouldTrace)
            {
                ServiceClientTracing.SendRequest(_invocationId, _httpRequest);
            }
            cancellationToken.ThrowIfCancellationRequested();
            _httpResponse = await Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false);

            if (_shouldTrace)
            {
                ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse);
            }
            HttpStatusCode _statusCode = _httpResponse.StatusCode;

            cancellationToken.ThrowIfCancellationRequested();
            string _responseContent = null;

            if ((int)_statusCode != 200)
            {
                var ex = new TokenErrorModelException(string.Format("Operation returned an invalid status code '{0}'", _statusCode));
                try
                {
                    _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

                    TokenErrorModel _errorBody = Microsoft.Rest.Serialization.SafeJsonConvert.DeserializeObject <TokenErrorModel>(_responseContent, Client.DeserializationSettings);
                    if (_errorBody != null)
                    {
                        ex.Body = _errorBody;
                    }
                }
                catch (JsonException)
                {
                    // Ignore the exception
                }
                ex.Request  = new HttpRequestMessageWrapper(_httpRequest, _requestContent);
                ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent);
                if (_shouldTrace)
                {
                    ServiceClientTracing.Error(_invocationId, ex);
                }
                _httpRequest.Dispose();
                if (_httpResponse != null)
                {
                    _httpResponse.Dispose();
                }
                throw ex;
            }
            // Create Result
            var _result = new HttpOperationResponse <TokenOKResponse>();

            _result.Request  = _httpRequest;
            _result.Response = _httpResponse;
            // Deserialize Response
            if ((int)_statusCode == 200)
            {
                _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

                try
                {
                    _result.Body = Microsoft.Rest.Serialization.SafeJsonConvert.DeserializeObject <TokenOKResponse>(_responseContent, Client.DeserializationSettings);
                }
                catch (JsonException ex)
                {
                    _httpRequest.Dispose();
                    if (_httpResponse != null)
                    {
                        _httpResponse.Dispose();
                    }
                    throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);
                }
            }
            if (_shouldTrace)
            {
                ServiceClientTracing.Exit(_invocationId, _result);
            }
            return(_result);
        }