コード例 #1
0
        /// <summary>
        /// Returns an OAuth access token.
        /// The endpoint supports distinct methods of obtaining OAuth access tokens.
        /// Applications specify a method by adding the `grant_type` parameter
        /// in the request and also provide relevant information.
        /// For more information, see [OAuth access token management](https://developer.squareup.com/docs/authz/oauth/how-it-works#oauth-access-token-management).
        /// __Note:__ Regardless of the method application specified,
        /// the endpoint always returns two items; an OAuth access token and
        /// a refresh token in the response.
        /// __OAuth tokens should only live on secure servers. Application clients
        /// should never interact directly with OAuth tokens__.
        /// </summary>
        /// <param name="body">Required parameter: An object containing the fields to POST for the request.  See the corresponding object definition for field details.</param>
        /// <return>Returns the Models.ObtainTokenResponse response from the API call</return>
        public Models.ObtainTokenResponse ObtainToken(Models.ObtainTokenRequest body)
        {
            Task <Models.ObtainTokenResponse> t = ObtainTokenAsync(body);

            ApiHelper.RunTaskSynchronously(t);
            return(t.Result);
        }
コード例 #2
0
        /// <summary>
        /// Returns an OAuth access token.
        /// The endpoint supports distinct methods of obtaining OAuth access tokens.
        /// Applications specify a method by adding the `grant_type` parameter
        /// in the request and also provide relevant information.
        /// For more information, see [OAuth access token management](https://developer.squareup.com/docs/authz/oauth/how-it-works#oauth-access-token-management).
        /// __Note:__ Regardless of the method application specified,
        /// the endpoint always returns two items; an OAuth access token and
        /// a refresh token in the response.
        /// __OAuth tokens should only live on secure servers. Application clients
        /// should never interact directly with OAuth tokens__.
        /// </summary>
        /// <param name="body">Required parameter: An object containing the fields to POST for the request.  See the corresponding object definition for field details.</param>
        /// <return>Returns the Models.ObtainTokenResponse response from the API call</return>
        public async Task <Models.ObtainTokenResponse> ObtainTokenAsync(Models.ObtainTokenRequest body, CancellationToken cancellationToken = default)
        {
            //the base uri for api requests
            string _baseUri = config.GetBaseUri();

            //prepare query string for API call
            StringBuilder _queryBuilder = new StringBuilder(_baseUri);

            _queryBuilder.Append("/oauth2/token");

            //validate and preprocess url
            string _queryUrl = ApiHelper.CleanUrl(_queryBuilder);

            //append request with appropriate headers and parameters
            var _headers = new Dictionary <string, string>()
            {
                { "user-agent", userAgent },
                { "accept", "application/json" },
                { "content-type", "application/json; charset=utf-8" },
                { "Square-Version", "2020-05-28" }
            };

            //append body params
            var _body = ApiHelper.JsonSerialize(body);

            //prepare the API call request to fetch the response
            HttpRequest _request = GetClientInstance().PostBody(_queryUrl, _headers, _body);

            if (HttpCallBack != null)
            {
                HttpCallBack.OnBeforeHttpRequestEventHandler(GetClientInstance(), _request);
            }

            //invoke request and get response
            HttpStringResponse _response = await GetClientInstance().ExecuteAsStringAsync(_request, cancellationToken).ConfigureAwait(false);

            HttpContext _context = new HttpContext(_request, _response);

            if (HttpCallBack != null)
            {
                HttpCallBack.OnAfterHttpResponseEventHandler(GetClientInstance(), _response);
            }

            //handle errors defined at the API level
            base.ValidateResponse(_response, _context);

            var _responseModel = ApiHelper.JsonDeserialize <Models.ObtainTokenResponse>(_response.Body);

            _responseModel.Context = _context;
            return(_responseModel);
        }
コード例 #3
0
        /// <summary>
        /// Returns an OAuth access token..
        /// The endpoint supports distinct methods of obtaining OAuth access tokens..
        /// Applications specify a method by adding the `grant_type` parameter.
        /// in the request and also provide relevant information..
        /// __Note:__ Regardless of the method application specified,.
        /// the endpoint always returns two items; an OAuth access token and.
        /// a refresh token in the response..
        /// __OAuth tokens should only live on secure servers. Application clients.
        /// should never interact directly with OAuth tokens__..
        /// </summary>
        /// <param name="body">Required parameter: An object containing the fields to POST for the request.  See the corresponding object definition for field details..</param>
        /// <param name="cancellationToken"> cancellationToken. </param>
        /// <returns>Returns the Models.ObtainTokenResponse response from the API call.</returns>
        public async Task <Models.ObtainTokenResponse> ObtainTokenAsync(
            Models.ObtainTokenRequest body,
            CancellationToken cancellationToken = default)
        {
            // the base uri for api requests.
            string baseUri = this.Config.GetBaseUri();

            // prepare query string for API call.
            StringBuilder queryBuilder = new StringBuilder(baseUri);

            queryBuilder.Append("/oauth2/token");

            // append request with appropriate headers and parameters
            var headers = new Dictionary <string, string>()
            {
                { "user-agent", this.UserAgent },
                { "accept", "application/json" },
                { "content-type", "application/json; charset=utf-8" },
                { "Square-Version", this.Config.SquareVersion },
            };

            // append body params.
            var bodyText = ApiHelper.JsonSerialize(body);

            // prepare the API call request to fetch the response.
            HttpRequest httpRequest = this.GetClientInstance().PostBody(queryBuilder.ToString(), headers, bodyText);

            if (this.HttpCallBack != null)
            {
                this.HttpCallBack.OnBeforeHttpRequestEventHandler(this.GetClientInstance(), httpRequest);
            }

            // invoke request and get response.
            HttpStringResponse response = await this.GetClientInstance().ExecuteAsStringAsync(httpRequest, cancellationToken).ConfigureAwait(false);

            HttpContext context = new HttpContext(httpRequest, response);

            if (this.HttpCallBack != null)
            {
                this.HttpCallBack.OnAfterHttpResponseEventHandler(this.GetClientInstance(), response);
            }

            // handle errors defined at the API level.
            this.ValidateResponse(response, context);

            var responseModel = ApiHelper.JsonDeserialize <Models.ObtainTokenResponse>(response.Body);

            responseModel.Context = context;
            return(responseModel);
        }