1

So I was happily programming and testing away, and everything was working and then suddenly I started getting this error:

PHP Fatal error:  Uncaught API_Exception: [0]: Authorization failed
  thrown in /home/my_public_html/wp-content/plugins/civicrm/civicrm/Civi/API/Kernel.php on line 223

I am writing code in an external php file and using this method to gain access:

require_once '/home/my_public_html/wp-content/plugins/civicrm/civicrm/civicrm.config.php';
require_once '/home/my_public_html/wp-content/plugins/civicrm/civicrm/CRM/Core/Config.php';
$civi_config = CRM_Core_Config::singleton();

I was able to get and create Financial Types as well as get and create Contacts without any issues. But then suddenly started getting the error.

This is one of the calls I am making (which is giving me error):

$financial_type_name = "Donations";
$financialTypes = civicrm_api4('FinancialType', 'get', [
      'select' => [
        'name', 
        'id',
        'is_deductible',
      ],
      'where' => [
        ['name', '=', $financial_type_name],
      ],
    ]);
print_r($financialTypes);

EDIT: Using @Demerit answer, this is what the proper working call looks like with checkPermissions=FALSE

$financial_type_name = "Donations";
$financialTypes = civicrm_api4('FinancialType', 'get', [
      'checkPermissions'=>FALSE,
      'select' => [
        'name', 
        'id',
        'is_deductible',
      ],
      'where' => [
        ['name', '=', $financial_type_name],
      ],
    ]);
print_r($financialTypes);

1 Answer 1

3

Because you're using a manual way to bootstrap civi (as opposed to e.g. running your script with cv scr --user=admin), you need to either add code in there to also "log in" as a user, or you need to pass checkPermissions => false as a parameter to api4.

1
  • 1
    So I do not understand why it was working fine for several days, and just started throwing the error all of sudden. But anyways I tried adding the checkPermissions=FALSE before the other calls but get same error. Do I need to include it with every GET or CREATE call that I make? EDIT: I answered my own question... yes, it needs to be included as part of every call that I make.
    – jsherk
    Commented May 1, 2022 at 4:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.