/// <summary>
 /// Populates the token
 /// </summary>
 /// <param name="clientSecrets"></param>
 /// <param name="apiTokens"> </param>
 internal void Populate(ClientSecrets clientSecrets, Tokens apiTokens)
 {
     this.ClientSecrets = clientSecrets;
     this.ApiTokens = apiTokens;
 }
        /// <summary>
        /// Authenticates with OAuth Authorization Server token end point using OAuth2 Client Credentials Grant Type
        /// </summary>
        /// <param name="oauthTokenEndPoint">OAuth2 Authorization Server Access/Refresh Token URL</param>
        /// <param name="apiKey">Issued by VisualVault, used to identify the client application/developer</param>
        /// <param name="apiSecret">Issued by VisualVault, used to authenticate the client application/developer</param>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public static Tokens GetAccessToken(string oauthTokenEndPoint, string apiKey, string apiSecret, string userName, string password)
        {
            Tokens apiTokens = new Tokens();

            var post = new Dictionary<string, string>
                           {
                               {"client_id", apiKey},
                               {"client_secret", apiSecret},
                               {"username", userName},
                               {"password", password},
                               {"grant_type", "client_credentials"}
                           };

            string formData = "";

            foreach (string key in post.Keys)
            {
                formData += string.Format("&{0}={1}", key, post[key]);
            }

            WebRequest httpWebRequest = WebRequest.Create(oauthTokenEndPoint);
            httpWebRequest.Method = "Post";
            httpWebRequest.ContentType = "application/x-www-form-urlencoded";

            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {
                streamWriter.Write(formData);
                streamWriter.Flush();
                streamWriter.Close();
            }

            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();

            var stream = httpResponse.GetResponseStream();

            if (stream != null)
            {
                string content;

                using (var streamReader = new StreamReader(stream))
                {
                    content = streamReader.ReadToEnd();
                }

                var json = JObject.Parse(content);

                apiTokens = new Tokens
                {
                    AccessToken = json["access_token"].ToString(),
                    RefreshToken = json["refresh_token"].ToString()
                };

                if (!string.IsNullOrEmpty(json["expires_in"].ToString()))
                {
                    double expiration;
                    if (double.TryParse(json["expires_in"].ToString(), out expiration))
                    {
                        apiTokens.AccessTokenExpiration = DateTime.UtcNow.AddSeconds(expiration);
                    }
                }
            }

            return apiTokens;
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Authenticates with OAuth Authorization Server token end point using OAuth2 Client Credentials Grant Type
        /// </summary>
        /// <param name="oauthTokenEndPoint">OAuth2 Authorization Server Access/Refresh Token URL</param>
        /// <param name="apiKey">Issued by VisualVault, used to identify the client application/developer</param>
        /// <param name="apiSecret">Issued by VisualVault, used to authenticate the client application/developer</param>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public static Tokens GetAccessToken(string oauthTokenEndPoint, string apiKey, string apiSecret, string userName, string password)
        {
            Tokens apiTokens = new Tokens();

            var post = new Dictionary <string, string>
            {
                { "client_id", apiKey },
                { "client_secret", apiSecret },
                { "username", userName },
                { "password", password },
                { "grant_type", "client_credentials" }
            };

            string formData = "";

            foreach (string key in post.Keys)
            {
                formData += string.Format("&{0}={1}", key, post[key]);
            }

            WebRequest httpWebRequest = WebRequest.Create(oauthTokenEndPoint);

            httpWebRequest.Method      = "Post";
            httpWebRequest.ContentType = "application/x-www-form-urlencoded";

            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {
                streamWriter.Write(formData);
                streamWriter.Flush();
                streamWriter.Close();
            }

            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();

            var stream = httpResponse.GetResponseStream();

            if (stream != null)
            {
                string content;

                using (var streamReader = new StreamReader(stream))
                {
                    content = streamReader.ReadToEnd();
                }

                var json = JObject.Parse(content);

                apiTokens = new Tokens
                {
                    AccessToken  = json["access_token"].ToString(),
                    RefreshToken = json["refresh_token"].ToString()
                };

                if (!string.IsNullOrEmpty(json["expires_in"].ToString()))
                {
                    double expiration;
                    if (double.TryParse(json["expires_in"].ToString(), out expiration))
                    {
                        apiTokens.AccessTokenExpiration = DateTime.UtcNow.AddSeconds(expiration);
                    }
                }
            }

            return(apiTokens);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="virtualPath"></param>
        /// <param name="queryString"></param>
        /// <param name="options"></param>
        /// <param name="urlParts"></param>
        /// <param name="apiTokens"></param>
        /// <param name="clientSecrets"></param>
        /// <param name="virtualPathArgs"></param>
        /// <returns></returns>
        public static Stream GetStream(string virtualPath, string queryString, RequestOptions options, UrlParts urlParts, Tokens apiTokens, ClientSecrets clientSecrets, params object[] virtualPathArgs)
        {
            if (options == null)
            {
                options = new RequestOptions();
            }

            options.PrepForRequest();

            CleanupVirtualPathArgs(virtualPathArgs);

            string url = CreateUrl(urlParts, string.Format(virtualPath, virtualPathArgs), options.GetQueryString(queryString), options.Fields, options.Expand);

            WebRequest httpWebRequest = WebRequest.Create(url);
            httpWebRequest.Method = "Get";

            httpWebRequest.Headers.Add("Authorization", "Bearer " + apiTokens.AccessToken);

            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();

            Stream stream = httpResponse.GetResponseStream();

            return stream;
        }
Ejemplo n.º 5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="virtualPath"></param>
        /// <param name="queryString"></param>
        /// <param name="options"></param>
        /// <param name="urlParts"></param>
        /// <param name="apiTokens"></param>
        /// <param name="clientSecrets"></param>
        /// <param name="virtualPathArgs"></param>
        /// <returns></returns>
        public static Stream GetStream(string virtualPath, string queryString, RequestOptions options, UrlParts urlParts, Tokens apiTokens, ClientSecrets clientSecrets, params object[] virtualPathArgs)
        {
            if (options == null)
            {
                options = new RequestOptions();
            }

            options.PrepForRequest();

            CleanupVirtualPathArgs(virtualPathArgs);

            string url = CreateUrl(urlParts, string.Format(virtualPath, virtualPathArgs), options.GetQueryString(queryString), options.Fields, options.Expand);

            WebRequest httpWebRequest = WebRequest.Create(url);

            httpWebRequest.Method = "Get";

            httpWebRequest.Headers.Add("Authorization", "Bearer " + apiTokens.AccessToken);

            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();

            Stream stream = httpResponse.GetResponseStream();

            return(stream);
        }