From 4c94d6cbf5e9a0963c597ad7d0b998f147acd9c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E8=B1=AA?= Date: Mon, 28 Dec 2020 15:45:18 +0800 Subject: [PATCH] add Tapd channel (#206) --- README.md | 1 + src/Exceptions/BadRequestException.php | 10 ++ src/Providers/Tapd.php | 149 +++++++++++++++++++++++++ 3 files changed, 160 insertions(+) create mode 100644 src/Exceptions/BadRequestException.php create mode 100644 src/Providers/Tapd.php diff --git a/README.md b/README.md index 085ad8a..1f1373e 100644 --- a/README.md +++ b/README.md @@ -446,6 +446,7 @@ Enjoy it! :heart: - [豆瓣 - OAuth 2.0 授权机制说明](http://developers.douban.com/wiki/?title=oauth2) - [抖音 - 网站应用开发指南](http://open.douyin.com/platform/doc) - [飞书 - 授权说明](https://open.feishu.cn/document/ukTMukTMukTM/uMTNz4yM1MjLzUzM) +- [Tapd - 用户授权说明](https://www.tapd.cn/help/show#1120003271001000093) ## PHP 扩展包开发 diff --git a/src/Exceptions/BadRequestException.php b/src/Exceptions/BadRequestException.php new file mode 100644 index 0000000..05baa7e --- /dev/null +++ b/src/Exceptions/BadRequestException.php @@ -0,0 +1,10 @@ +buildAuthUrlFromBase($this->baseUrl . '/quickstart/testauth'); + } + + /** + * @return string + */ + protected function getTokenUrl(): string + { + return $this->baseUrl . '/tokens/request_token'; + } + + /** + * @return string + */ + protected function getRefreshTokenUrl(): string + { + return $this->baseUrl . '/tokens/refresh_token'; + } + + /** + * @param string $code + * + * @return array + * @throws \Overtrue\Socialite\Exceptions\AuthorizeFailedException + * @throws \GuzzleHttp\Exception\GuzzleException + */ + public function tokenFromCode($code): array + { + $response = $this->getHttpClient()->post($this->getTokenUrl(), [ + 'headers' => [ + 'Accept' => 'application/json', + 'Authorization' => 'Basic ' . \base64_encode(\sprintf('%s:%s', $this->getClientId(), $this->getClientSecret())) + ], + 'form_params' => $this->getTokenFields($code), + ]); + + return $this->normalizeAccessTokenResponse($response->getBody()->getContents()); + } + + /** + * @param string $code + * + * @return array + */ + protected function getTokenFields(string $code): array + { + return [ + 'grant_type' => 'authorization_code', + 'redirect_uri' => $this->redirectUrl, + 'code' => $code, + ]; + } + + /** + * @param $refreshToken + * + * @return array + */ + protected function getRefreshTokenFields($refreshToken): array + { + return [ + 'grant_type' => 'grant_type', + 'redirect_uri' => $this->redirectUrl, + 'refresh_token' => $refreshToken, + ]; + } + + /** + * @param string $refreshToken + * + * @return array + * + * @throws \GuzzleHttp\Exception\GuzzleException + * @throws \Overtrue\Socialite\Exceptions\AuthorizeFailedException + */ + public function tokenFromRefreshToken(string $refreshToken): array + { + $response = $this->getHttpClient()->post($this->getRefreshTokenUrl(), [ + 'headers' => [ + 'Accept' => 'application/json', + 'Authorization' => 'Basic ' . \base64_encode(\sprintf('%s:%s', $this->getClientId(), $this->getClientSecret())) + ], + 'form_params' => $this->getRefreshTokenFields($refreshToken), + ]); + + return $this->normalizeAccessTokenResponse($response->getBody()->getContents()); + } + + /** + * @param string $token + * + * @return array + * + * @throws \GuzzleHttp\Exception\GuzzleException + */ + protected function getUserByToken(string $token): array + { + $response = $this->getHttpClient()->get($this->baseUrl . '/users/info', [ + 'headers' => [ + 'Accept' => 'application/json', + 'Authorization' => 'Bearer ' . $token, + ], + ]); + + return \json_decode($response->getBody(), true) ?? []; + } + + /** + * @param array $user + * + * @return \Overtrue\Socialite\User + * + * @throws \Overtrue\Socialite\Exceptions\BadRequestException + */ + protected function mapUserToObject(array $user): User + { + if (!isset($user['status']) && $user['status'] != 1) { + throw new BadRequestException("用户信息获取失败"); + } + + return new User([ + 'id' => $user['data']['id'], + 'nickname' => $user['data']['nick'], + 'name' => $user['data']['name'], + 'email' => '', + 'avatar' => $user['data']['avatar'], + ]); + } +}