Search in sources :

Example 1 with Token

use of org.talend.salesforce.oauth.Token in project tdi-studio-se by Talend.

the class SforceOAuthConnection method renewSession.

@Override
protected void renewSession() throws Exception {
    Token token = loginWithOAuth();
    String session_id = token.getAccess_token();
    String endpoint = OAuthClient.getSOAPEndpoint(token, apiVersion);
    this.serviceEndPoint = endpoint;
    SforceManagementUtil.setEndpoint(stub, endpoint);
    sh.setSessionId(session_id);
}
Also used : Token(org.talend.salesforce.oauth.Token)

Example 2 with Token

use of org.talend.salesforce.oauth.Token in project tdi-studio-se by Talend.

the class SforceOAuthBulkConnection method loginWithOAuth.

private Token loginWithOAuth() throws Exception {
    String session_id = null;
    OAuthClient oauthClient = new OAuthClient();
    oauthClient.setBaseOAuthURL(login_endpoint);
    oauthClient.setClientID(oauth_clientID);
    oauthClient.setClientSecret(oauth_clientSecret);
    Token token = null;
    String refreshToken = null;
    // 1. if tokenFile exist, try refresh token
    if (tokenFilePath != null) {
        Properties prop = new Properties();
        FileInputStream inputStream = new FileInputStream(tokenFilePath);
        prop.load(inputStream);
        inputStream.close();
        String storedRefreshToken = (String) prop.get(REFRESHTOKEN_KEY);
        if (storedRefreshToken != null) {
            token = oauthClient.refreshToken(storedRefreshToken);
            session_id = token.getAccess_token();
            refreshToken = token.getRefresh_token();
        }
    }
    // 2. try to auth if session_id can't be retrieved
    if (session_id == null) {
        oauthClient.setCallbackHost(callbackHost);
        oauthClient.setCallbackPort(callbackPort);
        token = oauthClient.getToken();
        refreshToken = token.getRefresh_token();
    }
    // 3.if refresh token & tokenFile exist, store
    if (refreshToken != null && tokenFilePath != null) {
        Properties prop = new Properties();
        prop.setProperty(REFRESHTOKEN_KEY, refreshToken);
        FileOutputStream outputStream = new FileOutputStream(tokenFilePath);
        prop.store(outputStream, null);
        if (outputStream != null) {
            outputStream.close();
        }
    }
    return token;
}
Also used : OAuthClient(org.talend.salesforce.oauth.OAuthClient) FileOutputStream(java.io.FileOutputStream) Token(org.talend.salesforce.oauth.Token) Properties(java.util.Properties) FileInputStream(java.io.FileInputStream)

Example 3 with Token

use of org.talend.salesforce.oauth.Token in project tdi-studio-se by Talend.

the class SforceOAuthBulkConnection method renewSession.

@Override
protected void renewSession() throws ConnectionException {
    Token token;
    try {
        token = loginWithOAuth();
    } catch (Exception e) {
        throw new ConnectionException(e.getMessage());
    }
    String session_id = token.getAccess_token();
    String endpoint = OAuthClient.getBulkEndpoint(token, apiVersion);
    config.setSessionId(session_id);
    config.setRestEndpoint(endpoint);
}
Also used : Token(org.talend.salesforce.oauth.Token) ConnectionException(com.sforce.ws.ConnectionException) ConnectionException(com.sforce.ws.ConnectionException)

Example 4 with Token

use of org.talend.salesforce.oauth.Token in project tdi-studio-se by Talend.

the class SforceOAuthConnection method loginWithOAuth.

private Token loginWithOAuth() throws Exception {
    String session_id = null;
    OAuthClient oauthClient = new OAuthClient();
    oauthClient.setBaseOAuthURL(login_endpoint);
    oauthClient.setClientID(oauth_clientID);
    oauthClient.setClientSecret(oauth_clientSecret);
    Token token = null;
    String refreshToken = null;
    // 1. if tokenFile exist, try refresh token
    if (tokenFilePath != null) {
        Properties prop = new Properties();
        FileInputStream inputStream = new FileInputStream(tokenFilePath);
        prop.load(inputStream);
        inputStream.close();
        String storedRefreshToken = (String) prop.get(REFRESHTOKEN_KEY);
        if (storedRefreshToken != null) {
            token = oauthClient.refreshToken(storedRefreshToken);
            session_id = token.getAccess_token();
            refreshToken = token.getRefresh_token();
        }
    }
    // 2. try to auth if session_id can't be retrieved
    if (session_id == null) {
        oauthClient.setCallbackHost(callbackHost);
        oauthClient.setCallbackPort(callbackPort);
        token = oauthClient.getToken();
        refreshToken = token.getRefresh_token();
    }
    // 3.if refresh token & tokenFile exist, store
    if (refreshToken != null && tokenFilePath != null) {
        Properties prop = new Properties();
        prop.setProperty(REFRESHTOKEN_KEY, refreshToken);
        FileOutputStream outputStream = new FileOutputStream(tokenFilePath);
        prop.store(outputStream, null);
        if (outputStream != null) {
            outputStream.close();
        }
    }
    return token;
}
Also used : OAuthClient(org.talend.salesforce.oauth.OAuthClient) FileOutputStream(java.io.FileOutputStream) Token(org.talend.salesforce.oauth.Token) Properties(java.util.Properties) FileInputStream(java.io.FileInputStream)

Aggregations

Token (org.talend.salesforce.oauth.Token)4 FileInputStream (java.io.FileInputStream)2 FileOutputStream (java.io.FileOutputStream)2 Properties (java.util.Properties)2 OAuthClient (org.talend.salesforce.oauth.OAuthClient)2 ConnectionException (com.sforce.ws.ConnectionException)1