我正在尝试在Yii2中使用Auth客户端扩展(http://www.yiiframework.com/doc-2.0/ext-authclient-index.html)。我复制了YiiFramework附带的Twitter Auth Client类,并制作了自己的汤博乐版本。Twitter运行良好,但当我使用我的汤博乐版本时,我在屏幕上看到一个错误,"这个应用程序可以访问你的一些数据并在你的帐户上发帖吗?你以*********登录。"(汤博乐oauth页面)
错误为:请求失败,代码:401,消息:oauth_signature与预期值不匹配
这是我的汤博乐认证客户端代码:
namespace yiiauthclientclients;
use yiiauthclientOAuth1;
/**
*
* Example application configuration:
*
* ~~~
* 'components' => [
* 'authClientCollection' => [
* 'class' => 'yiiauthclientCollection',
* 'clients' => [
* 'tumblr' => [
* 'class' => 'yiiauthclientclientsTumblr',
* 'consumerKey' => 'tumblr_consumer_key',
* 'consumerSecret' => 'tumblr_consumer_secret',
* ],
* ],
* ]
* ...
* ]
* ~~~
*
*/
class Tumblr extends OAuth1
{
/**
* @inheritdoc
*/
public $authUrl = 'https://www.tumblr.com/oauth/authorize';
/**
* @inheritdoc
*/
public $requestTokenUrl = 'https://www.tumblr.com/oauth/request_token';
/**
* @inheritdoc
*/
public $requestTokenMethod = 'POST';
/**
* @inheritdoc
*/
public $accessTokenUrl = 'https://www.tumblr.com/oauth/access_token';
/**
* @inheritdoc
*/
public $accessTokenMethod = 'GET';
/**
* @inheritdoc
*/
public $apiBaseUrl = 'http://api.tumblr.com/v2';
/**
* @inheritdoc
*/
protected function initUserAttributes()
{
return $this->api('/user/info', 'GET');
}
/**
* @inheritdoc
*/
protected function defaultName()
{
return 'tumblr';
}
/**
* @inheritdoc
*/
protected function defaultTitle()
{
return 'Tumblr';
}
}
您可能想要试用Hybridauth的版本。我在Yii2中使用过谷歌和脸书版本,效果很好。
对于当前版本的yii-您必须使用以下代码扩展yii-Oauth1类才能使用您的类。
<?php
namespace your_vendorauthclient;
use yiiauthclientOAuthToken;
use yiibaseException;
use Yii;
class OAuth1 extends yiiauthclientOAuth1
{
public function fetchAccessToken(OAuthToken $requestToken = null, $oauthVerifier = null, array $params = [])
{
if (!is_object($requestToken)) {
$requestToken = $this->getState('requestToken');
if (!is_object($requestToken)) {
throw new Exception('Request token is required to fetch access token!');
}
}
$defaultParams = [
'oauth_consumer_key' => $this->consumerKey,
'oauth_token' => $requestToken->getToken()
];
if ($oauthVerifier === null) {
if (isset($_REQUEST['oauth_verifier'])) {
$oauthVerifier = $_REQUEST['oauth_verifier'];
}
}
if (!empty($oauthVerifier)) {
$defaultParams['oauth_verifier'] = $oauthVerifier;
}
$response = $this->sendSignedRequest($this->accessTokenMethod, $this->accessTokenUrl, array_merge($defaultParams, $params));
$this->removeState('requestToken');
$token = $this->createToken([
'params' => $response
]);
$this->setAccessToken($token);
return $token;
}
protected function composeSignatureKey() {
$signatureKeyParts = [
$this->consumerSecret
];
if (is_null($accessToken = $this->getState('requestToken'))) {
$accessToken = $this->getAccessToken();
}
if (is_object($accessToken)) {
$signatureKeyParts[] = $accessToken->getTokenSecret();
} else {
$signatureKeyParts[] = '';
}
$signatureKeyParts = array_map('rawurlencode', $signatureKeyParts);
return implode('&', $signatureKeyParts);
}
}