From 1f46681c3fc6b85877754a32f374161e11cb8261 Mon Sep 17 00:00:00 2001 From: dpi Date: Mon, 19 Oct 2020 23:30:09 +0800 Subject: [PATCH] Updated API to work with Helix --- src/Entity/TwitchUser.php | 15 ++++++++------ src/Provider/Twitch.php | 43 +++++++++++++++++++++++++++++---------- 2 files changed, 41 insertions(+), 17 deletions(-) diff --git a/src/Entity/TwitchUser.php b/src/Entity/TwitchUser.php index 1064279..df04220 100644 --- a/src/Entity/TwitchUser.php +++ b/src/Entity/TwitchUser.php @@ -1,10 +1,12 @@ id = $attributes['_id']; + // See https://dev.twitch.tv/docs/api/reference#get-users. + $this->id = (int) $attributes['id']; $this->display_name = $attributes['display_name']; $this->type = $attributes['type']; - $this->bio = $attributes['bio']; + $this->bio = $attributes['description']; $this->email = $attributes['email']; - $this->partnered = $attributes['partnered']; - $this->name = $this->username = $attributes['name']; - $this->logo = $attributes['logo']; + $this->partnered = $this->type === 'partnered'; + $this->name = $this->username = $attributes['login']; + $this->logo = $attributes['profile_image_url']; } /** diff --git a/src/Provider/Twitch.php b/src/Provider/Twitch.php index 8d00efd..3796a78 100644 --- a/src/Provider/Twitch.php +++ b/src/Provider/Twitch.php @@ -12,11 +12,32 @@ class Twitch extends AbstractProvider { /** - * Api domain + * The domain for authorization. * * @var string */ - public $apiDomain = 'https://api.twitch.tv'; + public $authorizationDomain = 'https://id.twitch.tv'; + + /** + * The path for authorization. + * + * @var string + */ + public $authorizationPath = '/oauth2'; + + /** + * The domain with resources. + * + * @var string + */ + public $resourceDomain = 'https://api.twitch.tv'; + + /** + * The path with resources. + * + * @var string + */ + public $resourcePath = '/helix'; public $scopes = [ 'user_read' ]; @@ -27,7 +48,7 @@ class Twitch extends AbstractProvider */ public function getBaseAuthorizationUrl() { - return $this->apiDomain.'/kraken/oauth2/authorize'; + return $this->authorizationDomain.$this->authorizationPath.'/authorize'; } /** @@ -39,7 +60,7 @@ public function getBaseAuthorizationUrl() */ public function getBaseAccessTokenUrl(array $params) { - return $this->apiDomain.'/kraken/oauth2/token'; + return $this->authorizationDomain.$this->authorizationPath.'/token'; } /** @@ -51,7 +72,7 @@ public function getBaseAccessTokenUrl(array $params) */ public function getResourceOwnerDetailsUrl(AccessToken $token) { - return $this->getAuthenticatedUrlForEndpoint('/kraken/user', $token); + return $this->resourceDomain.$this->resourcePath.'/users?oauth_token='.$token->getToken(); } /** @@ -63,7 +84,7 @@ public function getResourceOwnerDetailsUrl(AccessToken $token) */ public function getAuthenticatedUrlForEndpoint($endpoint, AccessToken $token) { - return $this->apiDomain.$endpoint.'?oauth_token='.$token->getToken(); + return $this->authorizationDomain.$endpoint.'?oauth_token='.$token->getToken(); } /** @@ -74,7 +95,7 @@ public function getAuthenticatedUrlForEndpoint($endpoint, AccessToken $token) */ public function getUrlForEndpoint($endpoint) { - return $this->apiDomain.$endpoint; + return $this->authorizationDomain.$endpoint; } /** @@ -139,7 +160,7 @@ protected function createResourceOwner(array $response, AccessToken $token) */ protected function getDefaultHeaders() { - return ['Client-ID' => $this->clientId, 'Accept' => 'application/vnd.twitchtv.v5+json']; + return ['Client-ID' => $this->clientId]; } /** @@ -149,9 +170,9 @@ protected function getDefaultHeaders() * @return array */ protected function getAuthorizationHeaders($token = null) { - if(isset($token)) - return ['Authorization' => 'OAuth '.$token->getToken()]; - return []; + return $token ? [ + 'Authorization' => 'Bearer ' . $token->getToken(), + ] : []; }