Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add weibo support #84

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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' => '...',
],
],
],

Expand Down
4 changes: 4 additions & 0 deletions src/assets/css/eauth.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Binary file modified src/assets/images/auth-src.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src/assets/images/auth.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
86 changes: 86 additions & 0 deletions src/services/WeiboOauth2Service.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php
/**
* WeiboOAuth2Service class file.
* API : @link http://open.weibo.com/wiki/%E5%BE%AE%E5%8D%9AAPI
* Register application: @link http://open.weibo.com/webmaster/add
* Oauth2 Weibo desc: @link http://open.weibo.com/wiki/%E6%8E%88%E6%9D%83%E6%9C%BA%E5%88%B6%E8%AF%B4%E6%98%8E
*
* @author Maxim Zemskov <[email protected]>
* @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;
}
}