'Basic '.$authKey, 'Cache-Control' => 'no-cache', 'Content-Length' => 0, 'Postman-Token' => 'XX264e32-2de0-f1e3-f3f8-eab014bbXX76' ])->post('https://api.srgssr.ch/oauth/v1/accesstoken?grant_type=client_credentials'); if($response->ok()){ return $response->json(); } return null; } /** * Oauth renewal. * Retrieve new token and save to database. * @param string $endpoint * @return string */ public static function renewal(string $endpoint) : string { $settings = Endpoint::firstWhere('uri', $endpoint)->settings; $token = self::accessToken($settings['key'], $settings['secret']); Endpoint::updateOrCreate( ['uri' => $endpoint.'#oauth'], ['settings' => $token] ); return $token['access_token']; } /** * Retrive Token. * Retrive token from database or renew if is expired. * @return string * @param string $endpoint */ public static function token($endpoint) : string { $oauth = Endpoint::firstWhere('uri', $endpoint.'#oauth'); $settings = $oauth->settings; $expiryDate = strtotime($oauth->updated_at) + $settings['expires_in']; if($expiryDate - time() <= 0){ $settings['access_token'] = self::renewal($endpoint); } return $settings['access_token']; } }