PayPal.OAuthTokenCredential.ConvertClientCredentialsToBase64String C# (CSharp) Method

ConvertClientCredentialsToBase64String() private static method

Covnerts the specified client credentials to a base-64 string for authorization purposes.
Thrown if clientId or clientSecret are null or empty. Thrown if there is an issue converting the credentials to a formatted authorization string. Thrown for any other issue encountered. See inner exception for further details.
private static ConvertClientCredentialsToBase64String ( string clientId, string clientSecret ) : string
clientId string The client ID to be used in generating the base-64 client identifier.
clientSecret string The client secret to be used in generating the base-64 client identifier.
return string
        private static string ConvertClientCredentialsToBase64String(string clientId, string clientSecret)
        {
            // Validate the provided credentials. If either value is null or empty, then throw.
            if (string.IsNullOrEmpty(clientId))
            {
                throw new MissingCredentialException("clientId is missing.");
            }
            else if (string.IsNullOrEmpty(clientSecret))
            {
                throw new MissingCredentialException("clientSecret is missing.");
            }

            try
            {
                byte[] bytes = Encoding.UTF8.GetBytes(string.Format("{0}:{1}", clientId, clientSecret));
                return Convert.ToBase64String(bytes);
            }
            catch (System.Exception ex)
            {
                if (ex is FormatException || ex is ArgumentNullException)
                {
                    throw new InvalidCredentialException("Unable to convert client credentials to base-64 string.\n" +
                                                         "  clientId: \"" + clientId + "\"\n" +
                                                         "  clientSecret: \"" + clientSecret + "\"\n" +
                                                         "  Error: " + ex.Message);
                }

                throw new PayPalException(ex.Message, ex);
            }
        }

Usage Example

Exemplo n.º 1
0
        /// <summary>
        /// Returns the currently cached access token. If no access token was
        /// previously cached, or if the current access token is expired, then
        /// a new one is generated and returned.
        /// </summary>
        /// <returns>The OAuth access token to use for making PayPal requests.</returns>
        /// <exception cref="PayPal.Exception.MissingCredentialException">Thrown if clientId or clientSecret are null or empty.</exception>
        /// <exception cref="PayPal.Exception.InvalidCredentialException">Thrown if there is an issue converting the credentials to a formatted authorization string.</exception>
        /// <exception cref="PayPal.Exception.IdentityException">Thrown if authorization fails as a result of providing invalid credentials.</exception>
        /// <exception cref="PayPal.Exception.HttpException">Thrown if authorization fails and an HTTP error response is received.</exception>
        /// <exception cref="PayPal.Exception.ConnectionException">Thrown if there is an issue attempting to connect to PayPal's services.</exception>
        /// <exception cref="PayPal.Exception.ConfigException">Thrown if there is an error with any informaiton provided by the <see cref="PayPal.Manager.ConfigManager"/>.</exception>
        /// <exception cref="PayPal.Exception.PayPalException">Thrown for any other general exception. See inner exception for further details.</exception>
        public string GetAccessToken()
        {
            // If the cached access token value is valid, then check to see if
            // it has expired.
            if (!string.IsNullOrEmpty(this.accessToken))
            {
                // If the time since the access token was created is greater
                // than the access token's specified expiration time less the
                // safety gap, then regenerate the token.
                double elapsedSeconds = (DateTime.Now - this.AccessTokenLastCreationDate).TotalSeconds;
                if (elapsedSeconds > this.AccessTokenExpirationInSeconds - this.AccessTokenExpirationSafetyGapInSeconds)
                {
                    this.accessToken = null;
                }
            }

            // If the cached access token is empty or null, then generate a new token.
            if (string.IsNullOrEmpty(this.accessToken))
            {
                // Write Logic for passing in Detail to Identity Api Serv and
                // computing the token
                // Set the Value inside the accessToken and result
                string base64ClientId = OAuthTokenCredential.ConvertClientCredentialsToBase64String(this.ClientId, this.ClientSecret);
                this.accessToken = this.GenerateOAuthToken(base64ClientId);
            }
            return(this.accessToken);
        }