Ejemplo n.º 1
0
        protected ResponseModel <T> CallApi <T>(string apiUrl, string value, Method method = Method.GET, string paramName = "application/json", ParameterType parameterType = ParameterType.RequestBody, string contentType = "application/json", string apiBaseUrl = null, bool isAuthenticationEnabled = false)
        {
            var restClient  = new RestClient(apiBaseUrl ?? ConfigKeys.OmnicxApiBaseUrl);
            var restRequest = new RestRequest(apiUrl, method);

            if (!string.IsNullOrEmpty(value))
            {
                var param = new Parameter()
                {
                    Name        = paramName,
                    Type        = parameterType,
                    ContentType = contentType,
                    Value       = value
                };
                restRequest.AddParameter(param);
            }

            //restRequest.AddHeader("token", Token().Token);
            restRequest.AddHeader("DeviceId", ReadCookie(Constants.COOKIE_DEVICEID));
            restRequest.AddHeader("SessionId", ReadCookie(Constants.COOKIE_SESSIONID));

            AddDefaultHeader(ref restRequest);
            //if (isAuthenticationEnabled)
            //{
            var token = OAuthHelper.GetAccessToken(ConfigKeys.OAuthUrl + "/OAuth/Token", ConfigKeys.OmnicxAppId, ConfigKeys.OmnicxSharedSecret);

            restRequest.AddParameter("Authorization", "Bearer " + token.Token, ParameterType.HttpHeader);
            // }

            var restResponse = restClient.Execute(restRequest);

            try
            {
                var result = JsonConvert.DeserializeObject <ResponseModel <T> >(restResponse.Content);
                SetContentSnippets(result);
                return(result);
            }
            catch (Exception ex)
            {
                return(new ResponseModel <T> {
                    Message = ex.Message
                });
                // return (T)Activator.CreateInstance(typeof(T));
            }
        }
Ejemplo n.º 2
0
        protected Task <ResponseModel <T> > CallApiAsync <T>(string apiUrl, string value, Method method = Method.GET, string paramName = "application/json", ParameterType parameterType = ParameterType.RequestBody, string contentType = "application/json")
        {
            var restClient  = new RestClient(ConfigKeys.OmnicxApiBaseUrl);
            var restRequest = new RestRequest(apiUrl, method);

            if (!string.IsNullOrEmpty(value))
            {
                var param = new Parameter()
                {
                    Name        = paramName,
                    Type        = parameterType,
                    ContentType = contentType,
                    Value       = value
                };
                restRequest.AddParameter(param);
            }
            //restRequest.AddHeader("token", Token().Token);
            var token = OAuthHelper.GetAccessToken(ConfigKeys.OAuthUrl + "/OAuth/Token", ConfigKeys.OmnicxAppId, ConfigKeys.OmnicxSharedSecret);

            restRequest.AddParameter("Authorization", "Bearer " + token.Token, ParameterType.HttpHeader);

            restRequest.AddHeader("DeviceId", ReadCookie(Constants.COOKIE_DEVICEID));
            restRequest.AddHeader("SessionId", ReadCookie(Constants.COOKIE_SESSIONID));
            AddDefaultHeader(ref restRequest);
            var cancellationTokenSource = new CancellationTokenSource();

            var tcs = new TaskCompletionSource <ResponseModel <T> >();

            restClient.ExecuteAsync(restRequest, response =>
            {
                try
                {
                    tcs.SetResult(JsonConvert.DeserializeObject <ResponseModel <T> >(response.Content));
                    //return tcs.Task;
                }
                catch (Exception ex)
                {
                    tcs.SetResult((ResponseModel <T>)Activator.CreateInstance(typeof(ResponseModel <T>)));
                    throw ex;
                }
            });
            SetContentSnippets(tcs.Task.Result);
            return(Task.FromResult(tcs.Task.Result));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// ユーザが入力したPINからアクセストークンを取得します
        /// </summary>
        /// <param name="pin">PIN</param>
        /// <param name="AccessToken">AccessToken</param>
        /// <param name="AccessTokenSecret">AccessTokenSecret</param>
        public void GetAccessToken(string pin, out string AccessToken, out string AccessTokenSecret)
        {
            var oah = new OAuthHelper();

            oah.GetAccessToken(pin, out AccessToken, out AccessTokenSecret);
        }
Ejemplo n.º 4
0
 static void Main(string[] args)
 {
     var result = OAuthHelper.GetAccessToken(Config.AppID, Config.AppSecret);
 }