Exemple #1
0
        internal void RefreshAccessToken()
        {
            TeaRequest request = new TeaRequest();

            request.Protocol = "http";
            request.Method   = "POST";
            request.Pathname = "/v2/oauth/token";
            request.Headers  = new Dictionary <string, string>();
            request.Headers.Add("host", GetHost(this.endpoint, this.domainId + ".api.alicloudccp.com"));
            request.Headers.Add("content-type", "application/x-www-form-urlencoded; charset=utf-8");
            request.Headers.Add("date", TimeUtils.GetGMTDate());
            request.Headers.Add("accept", "application/json");
            request.Headers.Add("x-acs-signature-method", "HMAC-SHA1");
            request.Headers.Add("x-acs-signature-version", "1.0");
            string bodyStr = string.Format("grant_type=refresh_token&refresh_token={0}&client_id={1}&client_secret={2}",
                                           this.refreshToken, this.clientId, this.clientSecret);
            MemoryStream stream = new MemoryStream();

            byte[] bytes = Encoding.UTF8.GetBytes(bodyStr);
            stream.Write(bytes, 0, bytes.Length);
            request.Body = stream;
            TeaResponse response = TeaCore.DoAction(request, new Dictionary <string, object>());
            string      body     = TeaCore.GetResponseBody(response);
            Dictionary <string, object> bodyDict = JsonConvert.DeserializeObject <Dictionary <string, object> >(body);

            if (response.StatusCode == 200)
            {
                expireTime   = DictUtils.GetDicValue(bodyDict, "expire_time").ToSafeString();
                accessToken  = DictUtils.GetDicValue(bodyDict, "access_token").ToSafeString();
                refreshToken = DictUtils.GetDicValue(bodyDict, "refresh_token").ToSafeString();
            }
        }
Exemple #2
0
        /// <summary>
        /// 将网关响应发序列化成Map,同时将API的接口名称和响应原文插入到响应Map的method和body字段中
        /// </summary>
        /// <param name="response">HTTP响应</param>
        /// <param name="method">调用的OpenAPI的接口名称</param>
        /// <returns>响应反序列化的Map</returns>
        protected Dictionary <string, object> _readAsJson(TeaResponse response, string method)
        {
            string responseBody = TeaCore.GetResponseBody(response);
            Dictionary <string, object> dictionary = JsonConvert.DeserializeObject <Dictionary <string, object> >(responseBody);

            dictionary.Add(AlipayConstants.BODY_FIELD, responseBody);
            dictionary.Add(AlipayConstants.METHOD_FIELD, method);
            return(DictionaryUtil.ObjToDictionary(dictionary));
        }
Exemple #3
0
        protected Dictionary <string, object> _json(TeaResponse response)
        {
            string body = TeaCore.GetResponseBody(response);
            Dictionary <string, object> dic     = new Dictionary <string, object>();
            Dictionary <string, object> dicBody = JsonConvert.DeserializeObject <Dictionary <string, object> >(body);

            dic = ObjToDictionary(dicBody);
            return(dic);
        }
Exemple #4
0
        protected Dictionary <string, object> _readAsJSON(TeaResponse response)
        {
            string body = TeaCore.GetResponseBody(response);
            Dictionary <string, object> dic = new Dictionary <string, object>();
            JObject jObj = JObject.Parse(body);

            dic = (Dictionary <string, object>)ReadJsonUtil.DeserializeToDic(jObj);
            return(dic);
        }
Exemple #5
0
        public void TestDoAction()
        {
            TeaRequest teaRequest = new TeaRequest();

            teaRequest.Protocol        = "http";
            teaRequest.Method          = "GET";
            teaRequest.Headers         = new Dictionary <string, string>();
            teaRequest.Headers["host"] = "www.alibabacloud.com";
            teaRequest.Pathname        = "/s/zh";
            teaRequest.Query           = new Dictionary <string, string>();
            teaRequest.Query.Add("k", "ecs");
            Dictionary <string, object> runtime = new Dictionary <string, object>();

            runtime.Add("readTimeout", 7000);
            runtime.Add("connectTimeout", 7000);
            runtime.Add("httpsProxy", "http://www.alibabacloud.com/s/zh?k=ecs");
            runtime.Add("ignoreSSL", true);

            TeaResponse teaResponse = TeaCore.DoAction(teaRequest, runtime);

            Assert.NotNull(teaResponse);

            teaRequest.Protocol = "https";
            teaResponse         = TeaCore.DoAction(teaRequest);
            Assert.NotNull(teaResponse);


            string bodyStr = TeaCore.GetResponseBody(teaResponse);

            Assert.NotNull(bodyStr);

            teaRequest.Method = "POST";
            teaRequest.Body   = new MemoryStream(Encoding.UTF8.GetBytes("test"));
            teaRequest.Headers["content-test"] = "test";
            teaResponse = TeaCore.DoAction(teaRequest, runtime);
            Assert.NotNull(teaResponse);

            TeaRequest teaRequest404 = new TeaRequest();

            teaRequest404.Protocol        = "https";
            teaRequest404.Method          = "GET";
            teaRequest404.Headers         = new Dictionary <string, string>();
            teaRequest404.Headers["host"] = "www.alibabacloud404.com";
            teaRequest404.Pathname        = "/s/zh";
            teaRequest404.Query           = new Dictionary <string, string>();
            teaRequest404.Query.Add("k", "ecs");
            Dictionary <string, object> runtime404 = new Dictionary <string, object>();

            runtime404.Add("readTimeout", 7000);
            runtime404.Add("connectTimeout", 7000);
            Assert.Throws <AggregateException>(() => { TeaCore.DoAction(teaRequest404, runtime404); });

            TeaRequest teaRequestProxy = new TeaRequest();


            TeaRequest requestException = new TeaRequest
            {
                Protocol = "http",
                Method   = "GET",
                Pathname = "/test"
            };
            Dictionary <string, object> runtimeException = new Dictionary <string, object>();

            requestException.Headers["host"] = "www.aliyun.com";
            TeaResponse responseException = TeaCore.DoAction(requestException, runtimeException);

            Assert.NotNull(responseException);
        }
Exemple #6
0
        public async Task TestDoActionAsync()
        {
            TeaRequest teaRequest = new TeaRequest();

            teaRequest.Protocol        = "https";
            teaRequest.Method          = "GET";
            teaRequest.Headers         = new Dictionary <string, string>();
            teaRequest.Headers["host"] = "www.alibabacloud.com";
            teaRequest.Pathname        = "/s/zh";
            teaRequest.Query           = new Dictionary <string, string>();
            teaRequest.Query.Add("k", "ecs");

            TeaResponse teaResponse = await TeaCore.DoActionAsync(teaRequest);

            Assert.NotNull(teaResponse);

            Dictionary <string, object> runtime = new Dictionary <string, object>();

            runtime.Add("readTimeout", 4000);
            runtime.Add("connectTimeout", 0);

            teaResponse = await TeaCore.DoActionAsync(teaRequest, runtime);

            Assert.NotNull(teaResponse);

            string bodyStr = TeaCore.GetResponseBody(teaResponse);

            Assert.NotNull(bodyStr);

            teaRequest.Method = "POST";
            teaRequest.Body   = new MemoryStream(Encoding.UTF8.GetBytes("test"));
            teaResponse       = await TeaCore.DoActionAsync(teaRequest, runtime);

            Assert.NotNull(teaResponse);

            TeaRequest teaRequest404 = new TeaRequest();

            teaRequest404.Protocol        = "https";
            teaRequest404.Method          = "GET";
            teaRequest404.Headers         = new Dictionary <string, string>();
            teaRequest404.Headers["host"] = "www.alibabacloud404.com";
            teaRequest404.Pathname        = "/s/zh";
            teaRequest404.Query           = new Dictionary <string, string>();
            teaRequest404.Query.Add("k", "ecs");
            Dictionary <string, object> runtime404 = new Dictionary <string, object>();

            runtime404.Add("readTimeout", 7000);
            runtime404.Add("connectTimeout", 7000);
            await Assert.ThrowsAsync <HttpRequestException>(async() => { await TeaCore.DoActionAsync(teaRequest404, runtime); });

            TeaRequest requestException = new TeaRequest
            {
                Protocol = "http",
                Method   = "GET",
                Pathname = "/test"
            };
            Dictionary <string, object> runtimeException = new Dictionary <string, object>();

            requestException.Headers["host"] = "www.aliyun.com";
            TeaResponse responseException = await TeaCore.DoActionAsync(requestException, runtimeException);
        }
Exemple #7
0
        protected string _readAsString(TeaResponse teaResponse)
        {
            string bodyStr = TeaCore.GetResponseBody(teaResponse);

            return(bodyStr);
        }