From f5768f2d58c8abea86abda11d74d8db2f31ade93 Mon Sep 17 00:00:00 2001 From: Kiddo <2868947306@qq.com> Date: Wed, 15 Dec 2021 11:55:02 +0800 Subject: [PATCH] Support Gaming Graph Domain --- src/Facebook/Facebook.php | 4 +++- src/Facebook/FacebookClient.php | 17 ++++++++++++++++- tests/FacebookTest.php | 20 ++++++++++++++++++++ 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/src/Facebook/Facebook.php b/src/Facebook/Facebook.php index 03a523480..e17f3f9fc 100644 --- a/src/Facebook/Facebook.php +++ b/src/Facebook/Facebook.php @@ -129,6 +129,7 @@ public function __construct(array $config = []) 'app_secret' => getenv(static::APP_SECRET_ENV_NAME), 'default_graph_version' => static::DEFAULT_GRAPH_VERSION, 'enable_beta_mode' => false, + 'use_gaming_graph' => false, 'http_client_handler' => null, 'persistent_data_handler' => null, 'pseudo_random_string_generator' => null, @@ -145,7 +146,8 @@ public function __construct(array $config = []) $this->app = new FacebookApp($config['app_id'], $config['app_secret']); $this->client = new FacebookClient( HttpClientsFactory::createHttpClient($config['http_client_handler']), - $config['enable_beta_mode'] + $config['enable_beta_mode'], + $config['use_gaming_graph'] ); $this->pseudoRandomStringGenerator = PseudoRandomStringGeneratorFactory::createPseudoRandomStringGenerator( $config['pseudo_random_string_generator'] diff --git a/src/Facebook/FacebookClient.php b/src/Facebook/FacebookClient.php index dbf759238..f7d1de8f3 100644 --- a/src/Facebook/FacebookClient.php +++ b/src/Facebook/FacebookClient.php @@ -55,6 +55,11 @@ class FacebookClient */ const BASE_GRAPH_VIDEO_URL_BETA = 'https://graph-video.beta.facebook.com'; + /** + * @const string Gaming Graph API URL. + */ + const GAMING_GRAPH_URL = 'https://graph.fb.gg'; + /** * @const int The timeout in seconds for a normal request. */ @@ -75,6 +80,11 @@ class FacebookClient */ protected $enableBetaMode = false; + /** + * @var bool Toggle to use Gaming Graph url. + */ + protected $useGamingGraph = false; + /** * @var FacebookHttpClientInterface HTTP client handler. */ @@ -91,10 +101,11 @@ class FacebookClient * @param FacebookHttpClientInterface|null $httpClientHandler * @param boolean $enableBeta */ - public function __construct(FacebookHttpClientInterface $httpClientHandler = null, $enableBeta = false) + public function __construct(FacebookHttpClientInterface $httpClientHandler = null, $enableBeta = false, $useGamingGraph = false) { $this->httpClientHandler = $httpClientHandler ?: $this->detectHttpClientHandler(); $this->enableBetaMode = $enableBeta; + $this->useGamingGraph = $useGamingGraph; } /** @@ -150,6 +161,10 @@ public function getBaseGraphUrl($postToVideoUrl = false) return $this->enableBetaMode ? static::BASE_GRAPH_VIDEO_URL_BETA : static::BASE_GRAPH_VIDEO_URL; } + if ($this->useGamingGraph) { + return static::GAMING_GRAPH_URL; + } + return $this->enableBetaMode ? static::BASE_GRAPH_URL_BETA : static::BASE_GRAPH_URL; } diff --git a/tests/FacebookTest.php b/tests/FacebookTest.php index 035e8d70f..efe373605 100644 --- a/tests/FacebookTest.php +++ b/tests/FacebookTest.php @@ -416,4 +416,24 @@ public function testMaxingOutRetriesWillThrow() $fb = new Facebook($config); $fb->uploadVideo('4', __DIR__.'/foo.txt', [], 'foo-token', 3); } + + public function testCreatingANewRequestWithGamingGraphDomain() + { + $config = array_merge($this->config, [ + 'app_id' => FacebookTestCredentials::$appId, + 'app_secret' => FacebookTestCredentials::$appSecret, + 'use_gaming_graph' => true, + ]); + $fb = new Facebook($config); + + $oauth = $fb->getOAuth2Client(); + $metadata = $oauth->debugToken('baz_token'); + + $this->assertInstanceOf('Facebook\Authentication\AccessTokenMetadata', $metadata); + + $request = $oauth->getLastRequest(); + $this->assertEquals('GET', $request->getMethod()); + $this->assertEquals('/debug_token', $request->getEndpoint()); + $this->assertEquals(Facebook::DEFAULT_GRAPH_VERSION, $request->getGraphVersion()); + } }