/// <summary>
        /// Extracts the access token from the specified <see cref="Uri"/>.
        /// </summary>
        /// <param name="redirectUri">The redirect <see cref="Uri"/> that contains an access token.</param>
        /// <returns>
        /// The <see cref="OAuthTokens"/> object which contains both the <see cref="OAuthTokens.AccessToken"/> and 
        /// <see cref="OAuthTokens.AccessTokenExpiresInSeconds"/> properties.
        /// </returns>
        public OAuthTokens ExtractAccessTokenFromUri(Uri redirectUri)
        {            
            var fragmentParts = redirectUri.ParseFragment();

            if (!fragmentParts.ContainsKey("access_token"))
            {
                throw new InvalidOperationException(ErrorMessages.UriDoesntContainAccessToken);
            }

            if (!fragmentParts.ContainsKey("expires_in"))
            {
                throw new InvalidOperationException(ErrorMessages.UriDoesntContainAccessToken);
            }

            return OAuthTokens = new OAuthTokens(fragmentParts["access_token"], int.Parse(fragmentParts["expires_in"]), null);            
        }
        /// <summary>
        /// Initializes a new instance of the OAuthDesktopMobileAuthCodeGrant class with the specified ClientId.
        /// </summary>
        /// <param name="clientId">
        /// The client identifier corresponding to your registered application.  
        /// </param>
        /// <param name="oauthTokens">
        /// Contains information about OAuth access tokens received from the Microsoft Account authorization service.
        /// </param>
        /// <remarks>
        /// For more information about using a client identifier for authentication, see <see href="http://tools.ietf.org/html/draft-ietf-oauth-v2-15#section-3.1">Client Password Authentication section of the OAuth 2.0 spec</see>.
        /// </remarks>
        public OAuthDesktopMobileAuthCodeGrant(string clientId, OAuthTokens oauthTokens)
            : base(clientId, null, LiveComOAuthService.DesktopRedirectUri, oauthTokens)
        {

        }
 /// <summary>
 /// Initializes a new instance of the OAuthWebAuthCodeGrant class.
 /// </summary>
 /// <param name="clientId">
 /// The client identifier corresponding to your registered application.
 /// </param>
 /// <param name="clientSecret">
 /// The client secret corresponding to your registered application, or null if your app is a desktop or mobile app.
 /// </param>
 /// <param name="redirectionUri">
 /// The URI to which the user of the app will be redirected after receiving user consent.
 /// </param>
 /// <param name="oAuthTokens">
 /// Contains information about OAuth access tokens received from the Microsoft Account authorization service.
 /// </param>
 /// <remarks>
 /// <para>
 /// For more information about using a client identifier for authentication, see
 /// <see href="http://tools.ietf.org/html/draft-ietf-oauth-v2-15#section-3.1">Client Password Authentication section of the OAuth 2.0 spec</see>.
 /// </para>
 /// <para>
 /// For web applications, redirectionUri must be within the same domain of your registered application.
 /// For more information, see <see href="http://tools.ietf.org/html/draft-ietf-oauth-v2-15#section-2.1.1">Redirection Uri section of the OAuth 2.0 spec</see>.
 /// </para>
 /// </remarks>
 public OAuthWebAuthCodeGrant(string clientId, string clientSecret, Uri redirectionUri, OAuthTokens oAuthTokens)
     : base(clientId, clientSecret, redirectionUri, oAuthTokens)
 {
     if (clientSecret == null)
     {
         throw new ArgumentNullException("clientSecret");
     }
 }
 /// <summary>
 /// Initializes a new instance of the OAuthDesktopMobileImplicitGrant class.
 /// </summary>
 /// <param name="clientId">
 /// The client identifier corresponding to your registered application.
 /// </param>
 /// <param name="oAuthTokens">
 /// Contains information about OAuth access tokens received from the Microsoft Account authorization service.
 /// </param>
 /// <param name="environment">Bing Ads API environment</param>
 /// /// <remarks>
 /// For more information about using a client identifier for authentication, see <see href="https://tools.ietf.org/html/rfc6749#section-3.1">Client Password Authentication section of the OAuth 2.0 spec</see>.
 /// </remarks>
 public OAuthDesktopMobileImplicitGrant(string clientId, OAuthTokens oAuthTokens, ApiEnvironment?environment = ApiEnvironment.Production)
     : base(clientId, environment)
 {
     OAuthTokens = oAuthTokens;
 }
 /// <summary>
 /// Initializes a new instance of the OAuthWebAuthCodeGrant class.
 /// </summary>
 /// <param name="clientId">
 /// The client identifier corresponding to your registered application. 
 /// </param>
 /// <param name="clientSecret">
 /// The client secret corresponding to your registered application, or null if your app is a desktop or mobile app.
 /// </param>
 /// <param name="redirectionUri">
 /// The URI to which the user of the app will be redirected after receiving user consent.
 /// </param>
 /// <param name="oAuthTokens">
 /// Contains information about OAuth access tokens received from the Microsoft Account authorization service.
 /// </param>
 /// <remarks>
 /// <para>
 /// For more information about using a client identifier for authentication, see 
 /// <see href="http://tools.ietf.org/html/draft-ietf-oauth-v2-15#section-3.1">Client Password Authentication section of the OAuth 2.0 spec</see>.
 /// </para>
 /// <para>
 /// For web applications, redirectionUri must be within the same domain of your registered application.  
 /// For more information, see <see href="http://tools.ietf.org/html/draft-ietf-oauth-v2-15#section-2.1.1">Redirection Uri section of the OAuth 2.0 spec</see>.
 /// </para>
 /// </remarks>
 public OAuthWebAuthCodeGrant(string clientId, string clientSecret, Uri redirectionUri, OAuthTokens oAuthTokens)
     : base(clientId, clientSecret, redirectionUri, oAuthTokens)
 {
     if (clientSecret == null)
     {
         throw new ArgumentNullException("clientSecret");
     }
 }
 /// <summary>
 /// Initializes a new instance of the OAuthDesktopMobileImplicitGrant class.
 /// </summary>
 /// <param name="clientId">
 /// The client identifier corresponding to your registered application.
 /// </param>
 /// <param name="oAuthTokens">
 /// Contains information about OAuth access tokens received from the Microsoft Account authorization service.
 /// </param>
 /// /// <remarks>
 /// For more information about using a client identifier for authentication, see <see href="http://tools.ietf.org/html/draft-ietf-oauth-v2-15#section-3.1">Client Password Authentication section of the OAuth 2.0 spec</see>.
 /// </remarks>
 public OAuthDesktopMobileImplicitGrant(string clientId, OAuthTokens oAuthTokens)
     : base(clientId)
 {
     OAuthTokens = oAuthTokens;
 }
 /// <summary>
 /// Initializes a new instance of the OAuthWebAuthCodeGrant class.
 /// </summary>
 /// <param name="clientId">
 /// The client identifier corresponding to your registered application.
 /// </param>
 /// <param name="clientSecret">
 /// The client secret corresponding to your registered application, or null if your app is a desktop or mobile app.
 /// </param>
 /// <param name="redirectionUri">
 /// The URI to which the user of the app will be redirected after receiving user consent.
 /// </param>
 /// <param name="oAuthTokens">
 /// Contains information about OAuth access tokens received from the Microsoft Account authorization service.
 /// </param>
 /// <param name="environment">
 /// The environment the application runs in. Value should be either <see cref="ApiEnvironment.Production"/> or <see cref="ApiEnvironment.Sandbox"/>.
 /// If null is given, application will detect the application environment from configuration.
 /// </param>
 /// <remarks>
 /// <para>
 /// For more information about using a client identifier for authentication, see
 /// <see href="https://tools.ietf.org/html/rfc6749#section-3.1">Client Password Authentication section of the OAuth 2.0 spec</see>.
 /// </para>
 /// <para>
 /// For web applications, redirectionUri must be within the same domain of your registered application.
 /// For more information, see <see href="https://tools.ietf.org/html/rfc6749#section-2.1.1">Redirection Uri section of the OAuth 2.0 spec</see>.
 /// </para>
 /// </remarks>
 public OAuthWebAuthCodeGrant(string clientId, string clientSecret, Uri redirectionUri, OAuthTokens oAuthTokens, ApiEnvironment?environment = ApiEnvironment.Production)
     : base(clientId, clientSecret, redirectionUri, oAuthTokens, environment)
 {
     if (clientSecret == null)
     {
         throw new ArgumentNullException("clientSecret");
     }
 }
 /// <summary>
 /// Initializes a new instance of the OAuthDesktopMobileAuthCodeGrant class with the specified ClientId.
 /// </summary>
 /// <param name="clientId">
 /// The client identifier corresponding to your registered application.
 /// </param>
 /// <param name="oauthTokens">
 /// Contains information about OAuth access tokens received from the Microsoft Account authorization service.
 /// </param>
 /// <remarks>
 /// For more information about using a client identifier for authentication, see <see href="http://tools.ietf.org/html/draft-ietf-oauth-v2-15#section-3.1">Client Password Authentication section of the OAuth 2.0 spec</see>.
 /// </remarks>
 public OAuthDesktopMobileAuthCodeGrant(string clientId, OAuthTokens oauthTokens)
     : base(clientId, null, LiveComOAuthService.DesktopRedirectUri, oauthTokens)
 {
 }
        private static void WriteToFile(OAuthTokens token)
        {
            string res = "#Generation Time\r\n#AccessToken\r\n#AccessTokenExpiresInSeconds\r\n#RefreshToken\r\n";
            res += string.Format("{0}\r\n", CommonHelper.GetDateTimeInMillisecond());
            res += string.Format("{0}\r\n", token.AccessToken);
            res += string.Format("{0}\r\n", token.AccessTokenExpiresInSeconds);
            res += string.Format("{0}\r\n", token.RefreshToken);

            DateTime currentTime = DateTime.Now;
            string fileName = string.Format("MyCredential_{0}.ini", currentTime.ToString("yyyy_MM_dd_hh_mm_ss"));
            StreamWriter writer = new StreamWriter(fileName);
            writer.Write(res);
            writer.Close();
            fileName = string.Format("MyCredential.ini");
            writer = new StreamWriter(fileName);
            writer.Write(res);
            writer.Close();
        }