我试图在Yii2中使用Auth Client Extension(http://www.yiiframework.com/doc-2.0/ext-authclient-index.html)。我复制了YiiFramework附带的Twitter Auth Client类,并制作了自己的Tumblr版本。 Twitter工作正常,但是当我使用我的Tumblr版本时,我在屏幕上出现错误"此应用程序是否可以访问您的某些数据并向您的帐户发帖?您以*********登录。" (Tumblr oauth页面)
出现错误:请求失败,代码为:401,消息:oauth_signature与预期值不匹配
这是我的Tumblr auth客户端代码:
namespace yii\authclient\clients;
use yii\authclient\OAuth1;
/**
*
* Example application configuration:
*
* ~~~
* 'components' => [
* 'authClientCollection' => [
* 'class' => 'yii\authclient\Collection',
* 'clients' => [
* 'tumblr' => [
* 'class' => 'yii\authclient\clients\Tumblr',
* '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';
}
}
答案 0 :(得分:1)
您可能想要试用Hybridauth's version。我在Yii2中使用了谷歌和Facebook版本,它运行良好。
答案 1 :(得分:0)
对于当前版本的yii - 您必须使用以下代码扩展yii Oauth1类才能使用您的类。
<?php
namespace your_vendor\authclient;
use yii\authclient\OAuthToken;
use yii\base\Exception;
use Yii;
class OAuth1 extends \yii\authclient\OAuth1
{
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);
}
}