Answers.Body: <p>Because you're using a manual way to bootstrap civi (as opposed to e.g. running your script with <code>cv scr --user=admin</code>), you need to either add code in there to also &quot;log in&quot; as a user, or you need to pass checkPermissions =&gt; false as a parameter to api4.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-05-01T04:25:02.447Z
Answers.Text: 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.
Answers.UserDisplayName: jsherk
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-05-01T00:19:02.347Z
Answers.LastActivityDate: 2022-05-01T00:19:02.347Z
Answers.UserDisplayName: Demerit
Body: <p>So I was happily programming and testing away, and everything was working and then suddenly I started getting this error:</p>
  <pre><code>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
  </code></pre>
  <p>I am writing code in an external php file and using this method to gain access:</p>
  <pre><code>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();
  </code></pre>
  <p>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.</p>
  <p>This is one of the calls I am making (which is giving me error):</p>
  <pre><code>$financial_type_name = &quot;Donations&quot;;
  $financialTypes = civicrm_api4('FinancialType', 'get', [
        'select' =&gt; [
          'name', 
          'id',
          'is_deductible',
        ],
        'where' =&gt; [
          ['name', '=', $financial_type_name],
        ],
      ]);
  print_r($financialTypes);
  </code></pre>
  <p>EDIT: Using @Demerit answer, this is what the proper working call looks like with checkPermissions=FALSE</p>
  <pre><code>$financial_type_name = &quot;Donations&quot;;
  $financialTypes = civicrm_api4('FinancialType', 'get', [
        'checkPermissions'=&gt;FALSE,
        'select' =&gt; [
          'name', 
          'id',
          'is_deductible',
        ],
        'where' =&gt; [
          ['name', '=', $financial_type_name],
        ],
      ]);
  print_r($financialTypes);
  </code></pre>
  
Tags: <wordpress><civicontribute><contacts><api><apiv4>
Title: API4 PHP Fatal error Uncaught API_Exception Authorization failed
ThreadText: # API4 PHP Fatal error Uncaught API_Exception Authorization failed
  
  (Asked by: username_0 on 2022-04-30)
  
  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 @username_1 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);
  
  **Tags:** wordpress, civicontribute, contacts, api, apiv4
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2022-05-01. Score: 3 | Accepted answer
  
  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.
  
  > **Comments:**
  > **username_0**: 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.  
  
  ---
  
  