Yii2 Auth Client Extension for Tumblr
Asked Answered
P

2

6

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

There error is: Request failed with code: 401, message: oauth_signature does not match expected value

Here is my Tumblr auth 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';
}

}

Propagandize answered 9/10, 2014 at 2:20 Comment(3)
did you try testing your credentials manually here? api.tumblr.com/console/calls/user/infoAbampere
I had not tried testing them, but I have just now and they appear to work fine.Propagandize
It would then appear that the problem is with the OAuth protocol? I never used Tumblr, but LinkedIn for instance isn't 100% compliant with OAuth 2. Last time I wrote something for it, it required a few workarounds.Flay
T
1

You might want to try out Hybridauth's version. I have used the Google and Facebook version in Yii2 and it works fine.

Tiffanytiffi answered 8/7, 2015 at 19:14 Comment(0)
T
0

For the current version of yii - you have to extend yii Oauth1 class 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);
    }
}
Teens answered 11/8, 2015 at 9:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.