Exemplo n.º 1
0
 //TODO: Create ZohoOAuthException class and change the throw exception class;
 public 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);
     }
 }
Exemplo n.º 2
0
 public ZohoOAuthTokens GetTokensFromJSON(JObject responseJSON)
 {
     try
     {
         ZohoOAuthTokens tokens    = new ZohoOAuthTokens();
         long            expiresIn = Convert.ToInt64(responseJSON[ZohoOAuthConstants.EXPIRES_IN]);
         tokens.ExpiryTime  = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() + expiresIn - 600000;
         tokens.AccessToken = (string)responseJSON[ZohoOAuthConstants.ACCESS_TOKEN];
         if (responseJSON.ContainsKey(ZohoOAuthConstants.REFRESH_TOKEN))
         {
             tokens.RefreshToken = (string)responseJSON[ZohoOAuthConstants.REFRESH_TOKEN];
         }
         //CRM.Library.Common.ZCRMConfigUtil.ConfigProperties["apiBaseUrl"] = (string)responseJSON["api_domain"];
         return(tokens);
     }
     catch (Exception) { throw; }
 }
Exemplo n.º 3
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)
            {
                MessageBox.Show("Grant Type is not provided");
                new ZohoOAuthException("Grant Type is not provided");
                return(null);
            }

            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);
                    //tokens.UserMaiilId = GetUserMailIdv2(tokens.AccessToken);
                    tokens.UserMaiilId = ZCRMConfigUtil.ConfigProperties["currentUserEmail"];
                    ZohoOAuth.GetPersistenceHandlerInstance().SaveOAuthTokens(tokens);
                    return(tokens);
                }

                MessageBox.Show("Exception while fetching Access Token from grant token" + response);
                new ZohoOAuthException("Exception while fetching Access Token from grant token" + response);
                return(null);
            }
            catch (WebException e)
            {
                ZCRMLogger.LogError(e);
                throw new ZohoOAuthException(e);
            }
        }