API Connection Error (GravityForms)

Hey all I managed to get SuiteCRM API version 8 working with Gravity Forms webhooks! I’m able to retrieve the auth key and then refresh it when expired and connect and post new leads to SuiteCRM using the key. Haven’t tried it with other modules. I’ll be adding to my blog soon (when I get a minute) and post link. If anyone needs in the meantime reach out to me.

1 Like

What PHP version are you using with API V8?

I’m struggling to make get the access token. I’m using SuiteCRM 8.6, API V8 and have tried with PHP 8.1 and 8.2 with no success. Any tips will be welcome.

I’ve been using it on all versions of PHP since 7.1 up to 8.1 without any issues. Here are some key tips:

Turn on logging in Gravity Forms, I’ve setup a number of Webhooks with different systems with it and this info is super helpful for troubleshooting.

Here’s from my personal notes, it’s been a while since I set this up, so my memory is a little fuzzy:

Getting and updating the access token

Gravity forms needs to get the access token from SuiteCRM first, then use the access token to connect to SuiteCRM to update, get, and add records.

You need to add a function to WordPress’s functions.php to be able to do this:

You need to enter the appropriate client_id, client_secret. The grant type is ā€œclient_credentialsā€ make sure you create this grant type in SuiteCRM Oath2

Note the _1 means this applies to form id 1

/*****add gf forms webhooks header for suitecrm**********/
add_filter( 'gform_webhooks_request_headers_1', function ( $request_headers ) {
    $token = GFCache::get( 'suitecrm_token' );
 
    if ( ! $token ) {
        $post_url = 'https://xxxxxxxxxxxxxxxxxxxxxxxxx/Api/access_token';
        $body     = json_encode(array(
            'client_id'     => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
            'client_secret' => 'GravityForms',
            'grant_type'    => 'client_credentials',
			
        ));
        $headers  = array( 'Content-Type' => 'application/vnd.api+json',
							'Accept' => 'application/vnd.api+json',
							);
 
        $response = wp_remote_post( $post_url, array( 'body' => $body, 'headers' => $headers ) );
        gf_webhooks()->log_debug( 'gform_webhooks_request_headers: response => ' . print_r( $response, true ) );
 
        if ( ! is_wp_error( $response ) && wp_remote_retrieve_response_code( $response ) == 200 ) {
            $response_body_json  = wp_remote_retrieve_body( $response );
            $response_body_array = json_decode( $response_body_json, true );
 
            if ( isset( $response_body_array['access_token'] ) ) {
                $token = $response_body_array['access_token'];
                GFCache::set( 'suitecrm_token', $token, true, $response_body_array['expires_in'] - 30 );
            }
        }
    }
 
    if ( $token ) {
        gf_webhooks()->log_debug( 'gform_webhooks_request_headers: token added.' );
        $request_headers['Authorization'] = 'Bearer ' . $token;
		$request_headers['grant_type'] = 'client_credentials';

    }
 
    return $request_headers;
} );

Modify the Output

Now that you have the access credentials in the header, you have to modify the output to be in a format accepted by SuiteCRM. You can add a function where the _1 is the form it applies to.

/****GF Webhook for SuiteCRM****/
add_filter( 'gform_webhooks_request_data_1', 'modify_suite_data', 50, 4 );
function modify_suite_data( $request_data, $feed, $entry, $form ){
$request_data = array(
'data' => array(
'type' => 'Leads',
'attributes' => array(
'name' => 'new lead',
'first_name' => rgar( $entry, '1.3' ),
'last_name' => rgar( $entry, '1.6' ),
'email1' => rgar( $entry, '2' ),
'account_name' => rgar( $entry, '4' ),
)
)
);
gf_webhooks()->log_debug( 'request modified' );
return $request_data;
}

Thanks @pstevens.

I think my issue is my server. I’m using Nginx. That one comes with it’s own issues.

This post has been helpful to me in case somebody uses he same server: