diff --git a/README.md b/README.md index e1255d3..d592658 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,7 @@ The implementation of the authorization on your own server has several advantage * VKontake (ru) * Mail.ru (ru) * Odnoklassniki (ru) + * Weibo (cn) ### Resources @@ -217,6 +218,12 @@ Add the following in your config: 'clientPublic' => '...', 'title' => 'Odnoklas.', ], + 'weibo' => [ + // register your app here: http://open.weibo.com/webmaster/add + 'class' => 'nodge\eauth\services\WeiboOAuth2Service', + 'clientId' => '...', + 'clientSecret' => '...', + ], ], ], diff --git a/src/assets/css/eauth.css b/src/assets/css/eauth.css index fb5cb52..a2a9fd9 100644 --- a/src/assets/css/eauth.css +++ b/src/assets/css/eauth.css @@ -100,4 +100,8 @@ .eauth-service-id-instagram .eauth-service-link:before { background-position: 0 -474px; +} + +.eauth-service-id-weibo .eauth-service-link:before { + background-position: 0 -508px; } \ No newline at end of file diff --git a/src/assets/images/auth-src.png b/src/assets/images/auth-src.png index 9c91275..a0c94de 100644 Binary files a/src/assets/images/auth-src.png and b/src/assets/images/auth-src.png differ diff --git a/src/assets/images/auth.png b/src/assets/images/auth.png index 818f3aa..1c68f2b 100644 Binary files a/src/assets/images/auth.png and b/src/assets/images/auth.png differ diff --git a/src/services/WeiboOauth2Service.php b/src/services/WeiboOauth2Service.php new file mode 100644 index 0000000..20ea03e --- /dev/null +++ b/src/services/WeiboOauth2Service.php @@ -0,0 +1,86 @@ + + * @link http://github.com/Nodge/yii2-eauth/ + * @license http://www.opensource.org/licenses/bsd-license.php + */ + +namespace nodge\eauth\services; + + +use nodge\eauth\ErrorException; +use nodge\eauth\oauth2\Service; +use OAuth\OAuth2\Service\ServiceInterface; +use Yii; + +class WeiboOauth2Service extends Service +{ + /** + * Scopes @link http://open.weibo.com/wiki/Scope + */ + const SCOPE_ALL = 'all'; + + protected $name = 'weibo'; + protected $title = 'Weibo'; + protected $type = 'OAuth2'; + protected $jsArguments = ['popup' => ['width' => 585, 'height' => 290]]; + + protected $scopes = []; + protected $providerOptions = [ + 'authorize' => 'https://api.weibo.com/oauth2/authorize', + 'access_token' => 'https://api.weibo.com/oauth2/access_token', + ]; + protected $baseApiUrl = 'https://api.weibo.com/2/'; + + /** + * {@inheritdoc} + */ + protected function fetchAttributes() + { + $accessTokenData = $this->getAccessTokenData(); + if (isset($accessTokenData['params']['uid'])) { + $this->attributes['id'] = $accessTokenData['params']['uid']; + } else { + throw new ErrorException(Yii::t('eauth', 'Unable to make user info request without uid.')); + } + $info = $this->makeSignedRequest('users/show.json', [ + 'query' => [ + 'uid' => $this->attributes['id'] + ] + ]); + + foreach(['name', 'gender'] as $attr) { + $this->attributes[$attr] = $info[$attr]; + } + $this->attributes['url'] = 'http://www.weibo.com/' . $info['profile_url']; + + return true; + } + + /** + * {@inheritdoc} + */ + public function getAuthorizationMethod() + { + return ServiceInterface::AUTHORIZATION_METHOD_QUERY_STRING; + } + + /** + * {@inheritdoc} + */ + protected function fetchResponseError($response) + { + if (isset($response['error'])) { + return [ + 'code' => $response['error_code'], + 'message' => $response['error'], + ]; + } + return null; + } +} \ No newline at end of file