コード例 #1
0
ファイル: TelekomOAuth2Auth.cs プロジェクト: palortoff/hacs
        private TelekomJsonWebRequest <AccessTokenResponse> CreateRequestAccessTokenParams()
        {
            string uri = BaseUrl + "/oauth";

            TelekomJsonWebRequest <AccessTokenResponse> request = new TelekomJsonWebRequest <AccessTokenResponse>(uri, HttpMethod.POST);

            string credentials = String.Format("{0}:{1}", ClientId, ClientSecret);

            byte[] bytes         = Encoding.UTF8.GetBytes(credentials);
            string base64        = Convert.ToBase64String(bytes);
            string authorization = String.Concat("Basic ", base64);

            request.AuthHeader = authorization;

            System.Net.NetworkCredential nc = new System.Net.NetworkCredential(ClientId, ClientSecret);
            request.Credentials = nc;

            // convert string to stream
            byte[] byteArray = Encoding.UTF8.GetBytes("grant_type=client_credentials&scope=" + Scope);
            //byte[] byteArray = Encoding.ASCII.GetBytes(contents);
            System.IO.MemoryStream streamData = new System.IO.MemoryStream(byteArray);

            request.SetRawContent(streamData, "application/x-www-form-urlencoded");

            return(request);
        }
コード例 #2
0
ファイル: Speech2TextClient.cs プロジェクト: palortoff/hacs
        /// <summary>
        /// Create a web request with access token and serialized parameters
        /// </summary>
        /// <typeparam name="ResponseType">Type which the response will be deserialized into</typeparam>
        /// <param name="uri">Target URI of the request</param>
        /// <param name="method">HTTP method of the request</param>
        /// <param name="requestParams">Object containing request parameters</param>
        /// <returns>configured web request with added header fields</returns>
        private TelekomJsonWebRequest <ResponseType> createAuthenticatedTranscriptionRequest <ResponseType>(string uri, Speech2TextRequest requestParams)
        {
            TelekomJsonWebRequest <ResponseType> webRequest = CreateAuthenticatedRequest <ResponseType>(uri, HttpMethod.POST, null);

            webRequest.AddParameter("file", requestParams.FileData);

            return(webRequest);
        }
コード例 #3
0
        private TelekomJsonWebRequest <SmsResponse> CreateAuthenticatedJsonRequest <ResponseType>(string uri, NotificationUnsubscribeRequest request)
        {
            TelekomJsonWebRequest <SmsResponse> webRequest = CreateAuthenticatedRequest <SmsResponse>(uri, HttpMethod.DELETE);

            webRequest.SetRawContent(null, "application/json");

            return(webRequest);
        }
コード例 #4
0
        private TelekomJsonWebRequest <SmsResponse> CreateAuthenticatedJsonRequest <ResponseType>(string uri, ReceiveNotificationSubscribeRequest request)
        {
            TelekomJsonWebRequest <SmsResponse> webRequest = CreateAuthenticatedRequest <SmsResponse>(uri, HttpMethod.POST);

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

            //---------------------------------------------------------------------------------------------------------------
            Dictionary <string, object> val = new Dictionary <string, object>();

            Dictionary <string, string> helper = new Dictionary <string, string>();

            helper.Add("notifyURL", request.notifyURL);
            if (request.callbackData != null)
            {
                helper.Add("callbackData", request.callbackData);
            }
            val.Add("callbackReference", helper);

            if (request.criteria != null)
            {
                val.Add("criteria", request.criteria);
            }

            if (request.clientCorrelator != null)
            {
                val.Add("clientCorrelator", request.clientCorrelator);
            }

            if (request.account != null)
            {
                val.Add("account", request.account);
            }

            if (request.notificationFormat != null)
            {
                val.Add("notificationFormat", request.notificationFormat);
            }

            val.Add("destinationAddress", request.destinationAddress);

            jsonRequest.Add("subscription", val);

            JsonSerializer serializer = new JsonSerializer();

            MemoryStream ms     = new MemoryStream();
            StreamWriter sw     = new StreamWriter(ms);
            JsonWriter   writer = new JsonTextWriter(sw);

            serializer.Serialize(writer, jsonRequest);
            writer.Flush();
            sw.Flush();
            ms.Position = 0;
            ms.Flush();

            webRequest.SetRawContent(ms, "application/json");

            return(webRequest);
        }
コード例 #5
0
ファイル: TelekomClient.cs プロジェクト: palortoff/hacs
        /// <summary>
        /// Create a web request with access token and serialized parameters
        /// </summary>
        /// <typeparam name="ResponseType">Type which the response will be deserialized into</typeparam>
        /// <param name="uri">Target URI of the request</param>
        /// <param name="method">HTTP method of the request</param>
        /// <param name="requestParams">Object containing request parameters</param>
        /// <returns>configured web request with added header fields</returns>
        internal virtual TelekomJsonWebRequest <ResponseType> CreateAuthenticatedRequest <ResponseType>(string uri, HttpMethod method, TelekomRequest requestParams)
        {
            if (!authentication.HasToken())
            {
                throw new InvalidOperationException("No access token fetched.");
            }

            var request = new TelekomJsonWebRequest <ResponseType>(uri, method);

            request.AuthHeader = "OAuth realm=\"developergarden.com\", oauth_token=\"" + authentication.AccessToken + "\"";
            if (requestParams != null)
            {
                requestParams.BuildRequestParameters(request);
            }
            return(request);
        }
コード例 #6
0
ファイル: TelekomOAuth2Auth.cs プロジェクト: palortoff/hacs
        private TelekomJsonWebRequest <AccessTokenResponse> CreateRefreshAccessTokenWebRequest()
        {
            if (!HasRefreshToken())
            {
                throw new InvalidOperationException("Cannot refresh without a refresh token");
            }

            string uri = BaseUrl + "/tokens";

            var request = new RefreshAccessTokenRequest()
            {
                ClientId     = ClientId,
                ClientSecret = ClientSecret,
                GrantType    = "refresh_token",
                RefreshToken = RefreshToken
            };

            var webRequest = new TelekomJsonWebRequest <AccessTokenResponse>(uri, HttpMethod.POST);

            request.BuildRequestParameters(webRequest);
            return(webRequest);
        }
コード例 #7
0
ファイル: TelekomOAuth2Auth.cs プロジェクト: pereritob/hacs
        private TelekomJsonWebRequest<AccessTokenResponse> CreateRequestAccessTokenParams()
        {
            string uri = BaseUrl + "/oauth";

            TelekomJsonWebRequest<AccessTokenResponse> request = new TelekomJsonWebRequest<AccessTokenResponse>(uri, HttpMethod.POST);

            string credentials = String.Format("{0}:{1}", ClientId, ClientSecret);
            byte[] bytes = Encoding.UTF8.GetBytes(credentials);
            string base64 = Convert.ToBase64String(bytes);
            string authorization = String.Concat("Basic ", base64);
            request.AuthHeader = authorization;

            System.Net.NetworkCredential nc = new System.Net.NetworkCredential(ClientId, ClientSecret);
            request.Credentials = nc;
            
            // convert string to stream
            byte[] byteArray = Encoding.UTF8.GetBytes("grant_type=client_credentials&scope="+ Scope);
            //byte[] byteArray = Encoding.ASCII.GetBytes(contents);
            System.IO.MemoryStream streamData = new System.IO.MemoryStream(byteArray);

            request.SetRawContent(streamData, "application/x-www-form-urlencoded");

            return request;
        }
コード例 #8
0
ファイル: TelekomOAuth2Auth.cs プロジェクト: pereritob/hacs
        private TelekomJsonWebRequest<AccessTokenResponse> CreateRefreshAccessTokenWebRequest()
        {
            if (!HasRefreshToken())
                throw new InvalidOperationException("Cannot refresh without a refresh token");

            string uri = BaseUrl + "/tokens";

            var request = new RefreshAccessTokenRequest()
            {
                ClientId = ClientId,
                ClientSecret = ClientSecret,
                GrantType = "refresh_token",
                RefreshToken = RefreshToken
            };

            var webRequest = new TelekomJsonWebRequest<AccessTokenResponse>(uri, HttpMethod.POST);
            request.BuildRequestParameters(webRequest);
            return webRequest;
        }
コード例 #9
0
        private TelekomJsonWebRequest <SmsResponse> CreateAuthenticatedJsonRequest <ResponseType>(string uri, SendSmsReceiveRequest request)
        {
            TelekomJsonWebRequest <SmsResponse> webRequest = CreateAuthenticatedRequest <SmsResponse>(uri, HttpMethod.GET, request);

            return(webRequest);
        }
コード例 #10
0
ファイル: SendSmsClient.cs プロジェクト: palortoff/hacs
        private TelekomJsonWebRequest <SmsResponse> CreateAuthenticatedJsonRequest <ResponseType>(string uri, SendSmsRequest request)
        {
            TelekomJsonWebRequest <SmsResponse> webRequest = CreateAuthenticatedRequest <SmsResponse>(uri, HttpMethod.POST);

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

            //---------------------------------------------------------------------------------------------------------------
            Dictionary <string, object> val = new Dictionary <string, object>();

            val.Add("address", request.Address);
            Dictionary <string, object> helper = new Dictionary <string, object>();

            String key  = "";
            String key2 = "";

            if (request.SMSType.Equals(OutboundSMSType.TEXT))
            {
                key  = "outboundSMSTextMessage";
                key2 = "message";
            }
            else if (request.SMSType.Equals(OutboundSMSType.BINARY))
            {
                key  = "outboundSMSBinaryMessage";
                key2 = "message";
            }
            else if (request.SMSType.Equals(OutboundSMSType.FLASH))
            {
                key  = "outboundSMSFlashMessage";
                key2 = "flashMessage";
            }
            helper.Add(key2, request.Message);
            val.Add(key, helper);

            val.Add("senderAddress", request.SenderAddress);

            if (request.SenderName != null)
            {
                val.Add("senderName", request.SenderName);
            }
            if (request.Account != null)
            {
                val.Add("account", request.Account);
            }
            helper = new Dictionary <string, object>();
            if (request.CallbackData != null || request.NotifyURL != null)
            {
                if (request.CallbackData != null)
                {
                    helper.Add("callbackData", request.CallbackData);
                }
                if (request.NotifyURL != null)
                {
                    helper.Add("notifyURL", request.NotifyURL);
                }
                val.Add("receiptRequest", helper);
            }

            if (request.Encoding != null)
            {
                if (request.Encoding.Equals(OutboundEncoding.GSM))
                {
                    val.Add("outboundEncoding", "7bitGSM");
                }
                else if (request.Encoding.Equals(OutboundEncoding.UCS))
                {
                    val.Add("outboundEncoding", "16bitUCS2");
                }
            }

            if (request.ClientCorrelator != null)
            {
                val.Add("clientCorrelator", request.ClientCorrelator);
            }

            jsonRequest.Add("outboundSMSMessageRequest", val);

            JsonSerializer serializer = new JsonSerializer();

            MemoryStream ms     = new MemoryStream();
            StreamWriter sw     = new StreamWriter(ms);
            JsonWriter   writer = new JsonTextWriter(sw);

            serializer.Serialize(writer, jsonRequest);
            writer.Flush();
            sw.Flush();
            ms.Position = 0;
            ms.Flush();

            webRequest.SetRawContent(ms, "application/json");

            //var sr = new StreamReader(ms);
            //var myStr = sr.ReadToEnd();
            //Console.WriteLine(myStr);

            return(webRequest);
        }