Search in sources :

Example 11 with RestClient

use of com.salesforce.androidsdk.rest.RestClient in project SalesforceMobileSDK-Android by forcedotcom.

the class SalesforceReactActivity method authenticate.

/**
 * Method called from bridge to authenticate.
 *
 * @param successCallback Success callback.
 * @param errorCallback Error callback.
 */
public void authenticate(final Callback successCallback, final Callback errorCallback) {
    SalesforceReactLogger.i(TAG, "authenticate called");
    clientManager.getRestClient(this, new RestClientCallback() {

        @Override
        public void authenticatedRestClient(RestClient client) {
            SalesforceReactActivity.this.setRestClient(client);
            getAuthCredentials(successCallback, errorCallback);
        }
    });
}
Also used : RestClientCallback(com.salesforce.androidsdk.rest.ClientManager.RestClientCallback) RestClient(com.salesforce.androidsdk.rest.RestClient)

Example 12 with RestClient

use of com.salesforce.androidsdk.rest.RestClient in project SalesforceMobileSDK-Android by forcedotcom.

the class ManagerTestCase method initRestClient.

private RestClient initRestClient() throws Exception {
    httpAccess = new HttpAccess(null, "dummy-agent");
    final TokenEndpointResponse refreshResponse = OAuth2.refreshAuthToken(httpAccess, new URI(TestCredentials.LOGIN_URL), TestCredentials.CLIENT_ID, TestCredentials.REFRESH_TOKEN, null);
    final String authToken = refreshResponse.authToken;
    final ClientInfo clientInfo = new ClientInfo(new URI(TestCredentials.INSTANCE_URL), new URI(TestCredentials.LOGIN_URL), new URI(TestCredentials.IDENTITY_URL), TestCredentials.ACCOUNT_NAME, TestCredentials.USERNAME, TestCredentials.USER_ID, TestCredentials.ORG_ID, null, null, null, null, null, null, TestCredentials.PHOTO_URL, null, null, null, null, null, null, null, null, null);
    return new RestClient(clientInfo, authToken, httpAccess, null);
}
Also used : HttpAccess(com.salesforce.androidsdk.auth.HttpAccess) RestClient(com.salesforce.androidsdk.rest.RestClient) ClientInfo(com.salesforce.androidsdk.rest.RestClient.ClientInfo) TokenEndpointResponse(com.salesforce.androidsdk.auth.OAuth2.TokenEndpointResponse) URI(java.net.URI)

Example 13 with RestClient

use of com.salesforce.androidsdk.rest.RestClient in project SalesforceMobileSDK-Android by forcedotcom.

the class SalesforceDroidGapActivity method authenticate.

/**
 * Get a RestClient and refresh the auth token
 *
 * @param callbackContext when not null credentials/errors are sent through to callbackContext.success()/error()
 */
public void authenticate(final CallbackContext callbackContext) {
    SalesforceHybridLogger.i(TAG, "authenticate called");
    clientManager.getRestClient(this, new RestClientCallback() {

        @Override
        public void authenticatedRestClient(RestClient client) {
            if (client == null) {
                SalesforceHybridLogger.i(TAG, "authenticate callback triggered with null client");
                logout(null);
            } else {
                SalesforceHybridLogger.i(TAG, "authenticate callback triggered with actual client");
                SalesforceDroidGapActivity.this.client = client;
                /*
                     * Do a cheap REST call to refresh the access token if needed.
                     * If the login took place a while back (e.g. the already logged
                     * in application was restarted), then the returned session ID
                     * (access token) might be stale. This is not an issue if one
                     * uses exclusively RestClient for calling the server because
                     * it takes care of refreshing the access token when needed,
                     * but a stale session ID will cause the WebView to redirect
                     * to the web login.
                     */
                SalesforceDroidGapActivity.this.client.sendAsync(RestRequest.getRequestForUserInfo(), new AsyncRequestCallback() {

                    @Override
                    public void onSuccess(RestRequest request, RestResponse response) {
                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                /*
                                     * The client instance being used here needs to be
                                     * refreshed, to ensure we use the new access token.
                                     */
                                SalesforceDroidGapActivity.this.client = SalesforceDroidGapActivity.this.clientManager.peekRestClient();
                                getAuthCredentials(callbackContext);
                            }
                        });
                    }

                    @Override
                    public void onError(Exception exception) {
                        if (callbackContext != null) {
                            callbackContext.error(exception.getMessage());
                        }
                    }
                });
            }
        }
    });
}
Also used : AsyncRequestCallback(com.salesforce.androidsdk.rest.RestClient.AsyncRequestCallback) RestRequest(com.salesforce.androidsdk.rest.RestRequest) RestResponse(com.salesforce.androidsdk.rest.RestResponse) RestClientCallback(com.salesforce.androidsdk.rest.ClientManager.RestClientCallback) RestClient(com.salesforce.androidsdk.rest.RestClient) AccountInfoNotFoundException(com.salesforce.androidsdk.rest.ClientManager.AccountInfoNotFoundException) NoNetworkException(com.salesforce.androidsdk.auth.HttpAccess.NoNetworkException)

Aggregations

RestClient (com.salesforce.androidsdk.rest.RestClient)13 RestResponse (com.salesforce.androidsdk.rest.RestResponse)6 IOException (java.io.IOException)6 RestRequest (com.salesforce.androidsdk.rest.RestRequest)5 RestClientCallback (com.salesforce.androidsdk.rest.ClientManager.RestClientCallback)4 JSONObject (org.json.JSONObject)4 ClientManager (com.salesforce.androidsdk.rest.ClientManager)3 JSONException (org.json.JSONException)3 NoNetworkException (com.salesforce.androidsdk.auth.HttpAccess.NoNetworkException)2 AccountInfoNotFoundException (com.salesforce.androidsdk.rest.ClientManager.AccountInfoNotFoundException)2 AsyncRequestCallback (com.salesforce.androidsdk.rest.RestClient.AsyncRequestCallback)2 ClientInfo (com.salesforce.androidsdk.rest.RestClient.ClientInfo)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 HashMap (java.util.HashMap)2 ReactMethod (com.facebook.react.bridge.ReactMethod)1 HttpAccess (com.salesforce.androidsdk.auth.HttpAccess)1 TokenEndpointResponse (com.salesforce.androidsdk.auth.OAuth2.TokenEndpointResponse)1 AccMgrAuthTokenProvider (com.salesforce.androidsdk.rest.ClientManager.AccMgrAuthTokenProvider)1