//TODO: Generate ZohoOAuthTokens Class for GenerateAccessTokens Method(); //TODO: This method throws an exception; public ZohoOAuthTokens GenerateAccessToken(string grantToken) { if (grantToken == null || grantToken.Length == 0) { //TODO: Throw an ZohoOAuthException; Console.WriteLine("Null Grant Token"); } //TODO: Inspect the usage of Contract; Console.WriteLine("access token ok"); var conn = GetZohoConnector(ZohoOAuth.GetTokenURL()); conn.AddParam("grant_type", "authorization_code"); conn.AddParam("code", grantToken); string response = conn.Post(); Console.WriteLine("Request posted"); Dictionary <string, string> responseJSON = JsonConvert.DeserializeObject <Dictionary <string, string> >(response); if (responseJSON.ContainsKey("access_token")) { Console.WriteLine("Response contains Access Token"); ZohoOAuthTokens tokens = GetTokensFromJSON(responseJSON); tokens.UserMaiilId = GetUserMailId(tokens.AccessToken); ZohoOAuth.GetPersistenceHandlerInstance().SaveOAuthData(tokens); //Console.WriteLine(CRM.Library.Common.ZCRMConfigUtil.GetAuthenticationClass()); return(tokens); } //TODO: Throw exceptions and remove the return statement and console statement; Console.WriteLine("Exception fetching access tokens"); return(null); }
//TODO: Create ZohoOAuthException class and change the throw exception class; private ZohoOAuthTokens RefreshAccessToken(string refreshToken, string userMailId) { if (refreshToken == null) { throw new Exception("Refresh token is not provided"); } try{ ZohoHTTPConnector conn = GetZohoConnector(ZohoOAuth.GetRefreshTokenURL()); conn.AddParam("grant_type", "refresh_token"); conn.AddParam("refresh_token", refreshToken); string response = conn.Post(); Dictionary <string, string> responseJson = JsonConvert.DeserializeObject <Dictionary <string, string> >(response); if (responseJson.ContainsKey("access_token")) { ZohoOAuthTokens tokens = GetTokensFromJSON(responseJson); tokens.RefreshToken = refreshToken; tokens.UserMaiilId = userMailId; ZohoOAuth.GetPersistenceHandlerInstance().SaveOAuthData(tokens); return(tokens); } throw new Exception("Exception while fetching access tokens from site"); }catch (Exception e) { Console.WriteLine(e.Message); throw; } }
//TODO: Throw ZohoOAuthException ; public string GetAccessToken(string userMailId) { IZohoPersistenceHandler persistenceHandler = ZohoOAuth.GetPersistenceHandlerInstance(); ZohoOAuthTokens tokens; try{ Console.WriteLine("Getting tokens"); tokens = persistenceHandler.GetOAuthTokens(userMailId); }catch (Exception e) { Console.WriteLine("Exception while retrieving tokens from persistence " + e.Message); throw; } try { return(tokens.AccessToken); }catch (Exception e) { Console.WriteLine(e.Message); ZCRMLogger.LogInfo("Access Token expired, Refreshing Access token"); tokens = RefreshAccessToken(tokens.RefreshToken, userMailId); } return(tokens.AccessToken); }