Example #1
0
        /// <summary>
        /// Gets the JSON Web Token using the specified URL and grant type.
        /// </summary>
        /// <param name="url">The URL.</param>
        /// <param name="grantType">The grant type.</param>
        /// <returns>The JSON Web Token provided by the URL.</returns>
        public string GetJsonWebToken(string url, string grantType = DefaultGrantType)
        {
            using (var client = new WebClient())
            {
                client.Proxy = Proxy;

                // Include Authorization header
                foreach (var header in BasicHeader)
                {
                    client.Headers[header.Key] = header.Value;
                }

                client.Headers[HttpRequestHeader.ContentType]  = UrlEncodedContentType;
                client.Headers[HttpRequestHeader.CacheControl] = NoCache;

                var    payload  = PreparePayload(BasicHeader, grantType);
                var    response = client.UploadString(url, payload);
                string token;

                try
                {
                    var json = JObject.Parse(response);
                    token = json["access_token"].Value <string>();
                }
                catch
                {
                    // If we can't parse the expected response format, use the raw response as the token
                    token = response;
                }

                BearerHeader = Authorization.Bearer(token);
                return(token);
            }
        }
Example #2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="JsonClient" /> class.
 /// </summary>
 /// <param name="token">The token.</param>
 /// <param name="proxy">The proxy.</param>
 public JsonClient(string token, IWebProxy proxy = null)
 {
     Authorization = Authorization.Bearer(token);
     Proxy         = proxy;
 }
Example #3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="JsonClient" /> class.
 /// </summary>
 /// <param name="username">The username.</param>
 /// <param name="password">The password.</param>
 /// <param name="proxy">The proxy.</param>
 public JsonClient(string username, string password, IWebProxy proxy = null)
 {
     Authorization = Authorization.Basic(username, password);
     Proxy         = proxy;
 }
Example #4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="JsonClient" /> class.
 /// </summary>
 /// <param name="token">The token.</param>
 /// <param name="proxy">The proxy.</param>
 public JsonClient(string token, IWebProxy proxy = null)
 {
     BasicHeader  = new Dictionary <string, string>();
     BearerHeader = Authorization.Bearer(token);
     Proxy        = proxy;
 }
Example #5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="JsonClient" /> class.
 /// </summary>
 /// <param name="username">The username.</param>
 /// <param name="password">The password.</param>
 /// <param name="proxy">The proxy.</param>
 public JsonClient(string username, string password, IWebProxy proxy = null)
 {
     BasicHeader  = Authorization.Basic(username, password);
     BearerHeader = new Dictionary <string, string>();
     Proxy        = proxy;
 }