Beispiel #1
0
        /*
         * Use this operation to get the refresh and access tokens.
         */
        public OAuthResponse ExchangeCodeForAccessToken(OAuthEnvironment environment, String code)
        {
            //Validate request
            ValidateInput("Environment", environment);
            ValidateInput("Code", code);

            //Get credentials
            CredentialUtil.Credentials credentials = GetCredentials(environment);

            // Create request payload
            Dictionary <String, String> payloadParams = new Dictionary <string, string>
            {
                { Constants.PAYLOAD_GRANT_TYPE, Constants.PAYLOAD_VALUE_AUTHORIZATION_CODE },
                { Constants.PAYLOAD_REDIRECT_URI, credentials.Get(CredentialType.REDIRECT_URI) },
                { Constants.PAYLOAD_CODE, code }
            };
            String requestPayload = OAuth2Util.CreateRequestPayload(payloadParams);

            OAuthResponse oAuthResponse = FetchToken(environment, requestPayload, TokenType.USER);

            return(oAuthResponse);
        }
        /*
         * Use this operation to get an OAuth access token using a client credentials grant.
         * The access token retrieved from this process is called an Application access token.
         */
        public OAuthResponse GetApplicationToken(OAuthEnvironment environment, IList <String> scopes)
        {
            //Validate request
            ValidateEnvironmentAndScopes(environment, scopes);
            OAuthResponse oAuthResponse = null;

            //Check for token in cache
            if (appTokenCache != null)
            {
                oAuthResponse = appTokenCache.GetValue(environment);
                if (oAuthResponse != null && oAuthResponse.AccessToken != null && oAuthResponse.AccessToken.Token != null)
                {
                    log.Info("Returning token from cache for " + environment.ConfigIdentifier());
                    return(oAuthResponse);
                }
            }

            //App token not in cache, fetch it and set into cache
            String formattedScopes = OAuth2Util.FormatScopesForRequest(scopes);

            //Prepare request payload
            Dictionary <String, String> payloadParams = new Dictionary <string, string>
            {
                { Constants.PAYLOAD_GRANT_TYPE, Constants.PAYLOAD_VALUE_CLIENT_CREDENTIALS },
                { Constants.PAYLOAD_SCOPE, formattedScopes }
            };
            String requestPayload = OAuth2Util.CreateRequestPayload(payloadParams);

            oAuthResponse = FetchToken(environment, requestPayload, TokenType.APPLICATION);
            //Update value in cache
            if (oAuthResponse != null && oAuthResponse.AccessToken != null)
            {
                appTokenCache.UpdateValue(environment, oAuthResponse, oAuthResponse.AccessToken.ExpiresOn);
            }

            return(oAuthResponse);
        }