Answers.Body: <p>You can handle it with something like this in a custom module.  This assumes that your API base URL, username and password are stored in your .env file.</p>
  <p>Obviously you'll need to customize the Guzzle request to suit your needs and edit the header that gets added to the feed settings.</p>
  <p>This stores the token for 900 seconds (15 minutes).</p>
  <pre><code>use Craft;
  use GuzzleHttp\Client;
  use craft\feedme\Plugin as FeedMe;
  use craft\feedme\events\FeedDataEvent;
  use craft\feedme\services\DataTypes;
  use craft\helpers\App;
  
  Event::on(DataTypes::class, DataTypes::EVENT_BEFORE_FETCH_FEED, function(FeedDataEvent $event) {
  
      $token = Craft::$app-&gt;getCache()-&gt;getOrSet('api-token', function () {
          // Create Guzzle client
          $client = new Client([
              'base_uri' =&gt; App::env('API_URL') . '/'
          ]);
  
          // Request token
          $response = $client-&gt;request('POST', 'generate-token', [
              'form_params' =&gt; [
                  'username' =&gt; App::env('API_USERNAME'),
                  'password' =&gt; App::env('API_PASSWORD')
              ]
          ]);
  
          $status = $response-&gt;getStatusCode();
  
          if ($status === 200) {
              $body = $response-&gt;getBody();
              $data = json_decode($body-&gt;getContents());
              return $data-&gt;token;
          } else {
              return false;
          }
      }, 900);
  
      // Get the Feed Me plugin's settings
      $settings = FeedMe::getInstance()-&gt;getSettings();
  
      // Add the access token to the settings
      $settings-&gt;feedOptions[$event-&gt;feedId]['requestOptions']['headers']['Authorization'] = 'Bearer '.$token;
  
      // Feed back to the plugin
      FeedMe::getInstance()-&gt;setSettings((array) $settings);
  });
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-11-24T00:54:25.080Z
Answers.Text: Thank you for this.  For anyone else, I setup a plugin and added this code to the init() function of the Plugin.php file.
Answers.UserDisplayName: Stu00
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-10-26T13:31:58.320Z
Answers.LastActivityDate: 2023-10-26T13:31:58.320Z
Answers.UserDisplayName: Pete H
Answers.Body: <p>I needed something very similar so I ended up calling a function from a custom module which rotates my access tokens. The problem with the <code>feed-me.php</code> config file is it gets called on every single request on the frontend.</p>
  <p>I've looked through the events in the FeedMe docs and there doesn't appear to be a way to modify the Guzzle request headers before FeedMe fetches the feed. I've added a <a href="https://github.com/craftcms/feed-me/discussions/1369" rel="nofollow noreferrer">discussion</a> for this on GitHub to see if there's a way to do this.</p>
  <p>The only other solution I can think of is to trigger my token refresh via a cron every 10 mins and then store the token in a config file or in the database.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-10-11T15:29:14.007Z
Answers.LastActivityDate: 2023-10-11T15:29:14.007Z
Answers.UserDisplayName: Andy
Body: <p>I have setup a feed using Feed Me and have added the Authorization header token but the API changes the Auth token every 15min. Which I need to copy paste into the config file.</p>
  <p>Is there an easy setup using Guzzle that can check the timestamp of the last request and request a Auth token if needed?</p>
  
Tags: <plugin-feedme><api>
Title: Setting up Feed Me with and API Auth Token that changes every 15mins
ThreadText: # Setting up Feed Me with and API Auth Token that changes every 15mins
  
  (Asked by: username_0 on 2023-10-09)
  
  I have setup a feed using Feed Me and have added the Authorization header token but the API changes the Auth token every 15min. Which I need to copy paste into the config file.
  
  Is there an easy setup using Guzzle that can check the timestamp of the last request and request a Auth token if needed?
  
  **Tags:** plugin-feedme, api
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2023-10-26. Score: 1 | Accepted answer
  
  You can handle it with something like this in a custom module. This assumes that your API base URL, username and password are stored in your .env file.
  
  Obviously you'll need to customize the Guzzle request to suit your needs and edit the header that gets added to the feed settings.
  
  This stores the token for 900 seconds (15 minutes).
  
  ```
  use Craft;
  use GuzzleHttp\Client;
  use craft\feedme\Plugin as FeedMe;
  use craft\feedme\events\FeedDataEvent;
  use craft\feedme\services\DataTypes;
  use craft\helpers\App;
  
  Event::on(DataTypes::class, DataTypes::EVENT_BEFORE_FETCH_FEED, function(FeedDataEvent $event) {
  
      $token = Craft::$app->getCache()->getOrSet('api-token', function () {
          // Create Guzzle client
          $client = new Client([
              'base_uri' => App::env('API_URL') . '/'
          ]);
  
          // Request token
          $response = $client->request('POST', 'generate-token', [
              'form_params' => [
                  'username' => App::env('API_USERNAME'),
                  'password' => App::env('API_PASSWORD')
              ]
          ]);
  
          $status = $response->getStatusCode();
  
          if ($status === 200) {
              $body = $response->getBody();
              $data = json_decode($body->getContents());
              return $data->token;
          } else {
              return false;
          }
      }, 900);
  
      // Get the Feed Me plugin's settings
      $settings = FeedMe::getInstance()->getSettings();
  
      // Add the access token to the settings
      $settings->feedOptions[$event->feedId]['requestOptions']['headers']['Authorization'] = 'Bearer '.$token;
  
      // Feed back to the plugin
      FeedMe::getInstance()->setSettings((array) $settings);
  });
  
  ```
  
  > **Comments:**
  > **username_0**: Thank you for this.  For anyone else, I setup a plugin and added this code to the init() function of the Plugin.php file.  
  
  ---
  
  ### Answer by username_2 on 2023-10-11. Score: 1
  
  I needed something very similar so I ended up calling a function from a custom module which rotates my access tokens. The problem with the `feed-me.php` config file is it gets called on every single request on the frontend.
  
  I've looked through the events in the FeedMe docs and there doesn't appear to be a way to modify the Guzzle request headers before FeedMe fetches the feed. I've added a [discussion](https://github.com/craftcms/feed-me/discussions/1369) for this on GitHub to see if there's a way to do this.
  
  The only other solution I can think of is to trigger my token refresh via a cron every 10 mins and then store the token in a config file or in the database.
  
  
  ---
  
  