From 179415b8d4cfd74602c00facd24534ec917e92a8 Mon Sep 17 00:00:00 2001 From: Floris Derks Date: Mon, 26 Oct 2020 23:59:02 +0100 Subject: [PATCH 1/2] Fix Instagram/Facebook oEmbed calls --- src/Providers/OEmbed/Facebook.php | 29 +++++++++++++++++++++++++++-- src/Providers/OEmbed/Instagram.php | 27 ++++++++++++++++++++++++++- 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/src/Providers/OEmbed/Facebook.php b/src/Providers/OEmbed/Facebook.php index 6cc03ce6..1d5af120 100644 --- a/src/Providers/OEmbed/Facebook.php +++ b/src/Providers/OEmbed/Facebook.php @@ -2,11 +2,35 @@ namespace Embed\Providers\OEmbed; +use Embed\Adapters\Adapter; +use Embed\Http\Response; use Embed\Http\Url; class Facebook extends EndPoint implements EndPointInterface { protected static $pattern = 'www.facebook.com/*'; + protected $key; + + /** + * {@inheritdoc} + */ + public static function create(Adapter $adapter) + { + $key = $adapter->getConfig('facebook[key]'); + + if (!empty($key)) { + return new static($adapter->getResponse(), $key); + } + } + + /** + * {@inheritdoc} + */ + protected function __construct(Response $response, $key = null) + { + $this->response = $response; + $this->key = $key; + } /** * {@inheritdoc} @@ -14,14 +38,15 @@ class Facebook extends EndPoint implements EndPointInterface public function getEndPoint() { if ($this->getUrl()->match(['*/videos/*', '/video.php'])) { - $endPoint = Url::create('https://www.facebook.com/plugins/video/oembed.json'); + $endPoint = Url::create('https://graph.facebook.com/v8.0/oembed_video'); } else { - $endPoint = Url::create('https://www.facebook.com/plugins/post/oembed.json'); + $endPoint = Url::create('https://graph.facebook.com/v8.0/oembed_post'); } return $endPoint->withQueryParameters([ 'url' => (string) $this->getUrl(), 'format' => 'json', + 'access_token' => $this->key, ]); } } diff --git a/src/Providers/OEmbed/Instagram.php b/src/Providers/OEmbed/Instagram.php index 2ac552c8..f4d40850 100644 --- a/src/Providers/OEmbed/Instagram.php +++ b/src/Providers/OEmbed/Instagram.php @@ -2,6 +2,8 @@ namespace Embed\Providers\OEmbed; +use Embed\Adapters\Adapter; +use Embed\Http\Response; use Embed\Http\Url; class Instagram extends EndPoint implements EndPointInterface @@ -11,7 +13,29 @@ class Instagram extends EndPoint implements EndPointInterface 'www.instagram.com/p/*', 'instagr.am/p/*', ]; - protected static $endPoint = 'https://api.instagram.com/oembed'; + protected static $endPoint = 'https://graph.facebook.com/v8.0/instagram_oembed'; + protected $key; + + /** + * {@inheritdoc} + */ + public static function create(Adapter $adapter) + { + $key = $adapter->getConfig('facebook[key]'); + + if (!empty($key)) { + return new static($adapter->getResponse(), $key); + } + } + + /** + * {@inheritdoc} + */ + protected function __construct(Response $response, $key = null) + { + $this->response = $response; + $this->key = $key; + } /** * {@inheritdoc} @@ -24,6 +48,7 @@ public function getEndPoint() ->withQueryParameters([ 'url' => (string) $url, 'format' => 'json', + 'access_token' => $this->key, ]); } } From 231603b2a0fea3e49c0f7989fdc8dbfe80729e44 Mon Sep 17 00:00:00 2001 From: Floris Derks Date: Tue, 27 Oct 2020 16:00:50 +0100 Subject: [PATCH 2/2] Bugfix: Check FB/IG url with pattern before using, otherwise use starting URL --- src/Providers/OEmbed/Facebook.php | 16 ++++++++++++++-- src/Providers/OEmbed/Instagram.php | 16 ++++++++++++++-- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/Providers/OEmbed/Facebook.php b/src/Providers/OEmbed/Facebook.php index 1d5af120..2e4e0c4a 100644 --- a/src/Providers/OEmbed/Facebook.php +++ b/src/Providers/OEmbed/Facebook.php @@ -19,17 +19,29 @@ public static function create(Adapter $adapter) $key = $adapter->getConfig('facebook[key]'); if (!empty($key)) { - return new static($adapter->getResponse(), $key); + $response = $adapter->getResponse(); + + if ($response->getUrl()->match(static::$pattern)) { + return new static($response, null, $key); + } + + if ($response->getStartingUrl()->match(static::$pattern)) { + return new static($response, $response->getStartingUrl(), $key); + } } } /** * {@inheritdoc} */ - protected function __construct(Response $response, $key = null) + protected function __construct(Response $response, $url = null, $key = null) { $this->response = $response; $this->key = $key; + + if ($url) { + $this->url = $url; + } } /** diff --git a/src/Providers/OEmbed/Instagram.php b/src/Providers/OEmbed/Instagram.php index f4d40850..8f7f8874 100644 --- a/src/Providers/OEmbed/Instagram.php +++ b/src/Providers/OEmbed/Instagram.php @@ -24,17 +24,29 @@ public static function create(Adapter $adapter) $key = $adapter->getConfig('facebook[key]'); if (!empty($key)) { - return new static($adapter->getResponse(), $key); + $response = $adapter->getResponse(); + + if ($response->getUrl()->match(static::$pattern)) { + return new static($response, null, $key); + } + + if ($response->getStartingUrl()->match(static::$pattern)) { + return new static($response, $response->getStartingUrl(), $key); + } } } /** * {@inheritdoc} */ - protected function __construct(Response $response, $key = null) + protected function __construct(Response $response, $url = null, $key = null) { $this->response = $response; $this->key = $key; + + if ($url) { + $this->url = $url; + } } /**