Esempio n. 1
0
        /// <summary>
        /// To get access token from grantToken.
        /// </summary>
        /// <returns>ZohoOAuthTokens class instance.</returns>
        /// <param name="grantToken">Grant token (String) of the oauth client</param>
        public ZohoOAuthTokens GenerateAccessToken(string grantToken)
        {
            if (grantToken == null || grantToken.Length == 0)
            {
                throw new ZohoOAuthException("Grant Type is not provided");
            }

            try
            {
                var conn = GetZohoConnector(ZohoOAuth.GetTokenURL());
                conn.AddParam(ZohoOAuthConstants.GRANT_TYPE, ZohoOAuthConstants.GRANT_TYPE_AUTH_CODE);
                conn.AddParam(ZohoOAuthConstants.CODE, grantToken);
                string response = conn.Post();

                JObject responseJSON = JObject.Parse(response);

                if (responseJSON.ContainsKey(ZohoOAuthConstants.ACCESS_TOKEN))
                {
                    ZohoOAuthTokens tokens = GetTokensFromJSON(responseJSON);
                    tokens.UserMaiilId = GetUserMailId(tokens.AccessToken);
                    ZohoOAuth.GetPersistenceHandlerInstance().SaveOAuthTokens(tokens);
                    return(tokens);
                }
                throw new ZohoOAuthException("Exception while fetching Access Token from grant token" + response);
            }
            catch (WebException e)
            {
                ZCRMLogger.LogError(e);
                throw new ZohoOAuthException(e);
            }
        }
Esempio n. 2
0
        public string GetAccessToken(string userMailId)
        {
            IZohoPersistenceHandler persistenceHandler = ZohoOAuth.GetPersistenceHandlerInstance();

            ZohoOAuthTokens tokens;

            try
            {
                ZCRMLogger.LogInfo("Retreiving access token..");
                tokens = persistenceHandler.GetOAuthTokens(userMailId);
            }
            catch (Exception e) when(!(e is ZohoOAuthException))
            {
                ZCRMLogger.LogError("Exception while fetching tokens from persistence" + e);
                throw new ZohoOAuthException(e);
            }
            try
            {
                return(tokens.AccessToken);
            }
            catch (ZohoOAuthException)
            {
                ZCRMLogger.LogInfo("Access Token expired, Refreshing Access token");
                tokens = RefreshAccessToken(tokens.RefreshToken, userMailId);
            }
            return(tokens.AccessToken);
        }
Esempio n. 3
0
 //TODO: Create ZohoOAuthException class and change the throw exception class;
 private ZohoOAuthTokens RefreshAccessToken(string refreshToken, string userMailId)
 {
     if (refreshToken == null)
     {
         throw new ZohoOAuthException("Refresh token is not provided");
     }
     try{
         ZohoHTTPConnector conn = GetZohoConnector(ZohoOAuth.GetRefreshTokenURL());
         conn.AddParam(ZohoOAuthConstants.GRANT_TYPE, ZohoOAuthConstants.REFRESH_TOKEN);
         conn.AddParam(ZohoOAuthConstants.REFRESH_TOKEN, refreshToken);
         string  response     = conn.Post();
         JObject responseJSON = JObject.Parse(response);
         if (responseJSON.ContainsKey(ZohoOAuthConstants.ACCESS_TOKEN))
         {
             ZohoOAuthTokens tokens = GetTokensFromJSON(responseJSON);
             tokens.RefreshToken = refreshToken;
             tokens.UserMaiilId  = userMailId;
             ZohoOAuth.GetPersistenceHandlerInstance().SaveOAuthTokens(tokens);
             return(tokens);
         }
         throw new ZohoOAuthException("Exception while fetching access tokens from Refresh Token" + response);
     }catch (WebException e) {
         ZCRMLogger.LogError(e);
         throw new ZohoOAuthException(e);
     }
 }
Esempio n. 4
0
        //TODO: the method throws three exceptions and check for null exception on access_token.
        private string GetUserMailId(string accessToken)
        {
            try
            {
                ZohoHTTPConnector conn = new ZohoHTTPConnector()
                {
                    Url = ZohoOAuth.GetUserInfoURL()
                };
                conn.AddHeader("Authorization", ZohoOAuthConstants.AuthHeaderPrefix + accessToken);
                string  response     = conn.Get();
                JObject responseJSON = JObject.Parse(response);

                return(responseJSON["Email"].ToString());
            }
            catch (WebException) { throw; }
        }
Esempio n. 5
0
        //TODO: the method throws three exceptions and check for null exception on access_token.
        private string GetUserMailId(string accessToken)
        {
            try
            {
                ZohoHTTPConnector conn = new ZohoHTTPConnector()
                {
                    Url = ZohoOAuth.GetUserInfoURL()
                };

                conn.AddHeader("Authorization", ZohoOAuthConstants.AuthHeaderPrefix + accessToken);

                string response = conn.Get();

                JObject responseJSON = JObject.Parse(response);

                return(responseJSON["Email"].ToString());
            }
            catch (Exception ex)
            {
                ZCRMLogger.LogError(ex);

                throw new ZohoOAuthException("Exception while fetching User email from access token, Make sure AAAserver.profile.Read scope is included while generating the Grant token.");
            }
        }