//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); } }
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); }
/// <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); } }