PayPal.OAuthTokenCredential.GetAccessToken C# (CSharp) Method

GetAccessToken() public method

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.
Thrown if clientId or clientSecret are null or empty. Thrown if there is an issue converting the credentials to a formatted authorization string. Thrown if authorization fails as a result of providing invalid credentials. Thrown if authorization fails and an HTTP error response is received. Thrown if there is an issue attempting to connect to PayPal's services. Thrown if there is an error with any informaiton provided by the . Thrown for any other general exception. See inner exception for further details.
public GetAccessToken ( ) : string
return string
        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;
        }

Usage Example

 public void GetAccessTokenTest()
 {
     OAuthTokenCredential target = new OAuthTokenCredential(ClientID, ClientSecret); // TODO: Initialize to an appropriate value
     string expected = target.GetAccessToken();
     string actual = target.GetAccessToken();
     Assert.IsTrue(actual.ToLower().Contains("bearer"));
 }
All Usage Examples Of PayPal.OAuthTokenCredential::GetAccessToken