Yii2 Auth Client Extension for Tumblr

I am trying to use the Auth client extension in Yii2 ( http://www.yiiframework.com/doc-2.0/ext-authclient-index.html ). I copied the Auth Client Twitter class that came with YiiFramework and made my own version of Tumblr. Twitter is working fine, but when I use my version of Tumblr, I get an error message on the screen after "Is it okay for this application to access some of your data and make messages in your account?" You are logged in as **** ***** ". (Tumblr oauth Page)

Error: Error with code: 401, message: oauth_signature does not match the expected value

Here is my Tumblr client code:

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'; } 

}

code>
+6
source share
2 answers

You might want to try the Hybridauth version . I used the version of Google and Facebook in Yii2 and it works great.

+1
source

For the current version of yii, you must extend the yii class Oauth1 using the following code to be able to use your class.

 <?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); } } 
0
source

Source: https://habr.com/ru/post/976463/


All Articles