L
L
LucasP2018-05-13 21:27:01
Yii
LucasP, 2018-05-13 21:27:01

Yii2 OAuth on Twitter?

Authorization with Twitter does not work, I created an application, connected everything, keys in main.php too, and the error "Request failed with code: 401, message: <?xml version="1.0" encoding="UTF-8"?>
This client application's callback url has been locked/oauth/request_token"
SiteController.php

public function actions()
    {
        return [
            'auth' => [
                'class' => 'yii\authclient\AuthAction',
                'successCallback' => [$this, 'onAuthSuccess'],
            ],
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
        ];
    }

public function onAuthSuccess($client)
    {
        $attributes = $client->getUserAttributes();

        /** @var Auth $auth */
        $auth = Auth::find()->where([
            'source' => $client->getId(),
            'id_source' => $attributes['id'],
        ])->one();

        if (Yii::$app->user->isGuest) {
            if ($auth) { // login
                $user = $auth->user;
                Yii::$app->user->login($user);
            } else { // signup
                if (User::find()->where(['email' => $attributes['email']])->exists()) {
                    Yii::$app->getSession()->setFlash('error', [
                        Yii::t('app', "User with the same email as in {client} account already exists but isn't linked to it. Login using email first to link it.", ['client' => $client->getTitle()]),
                    ]);
                } else {
                    $password = Yii::$app->security->generateRandomString(6);
                    $user = new User([
                        'username' => $attributes['login'],
                        'email' => $attributes['email'],
                        'password' => $password,
                    ]);
                    $user->generateAuthKey();
                    $user->generatePasswordResetToken();
                    $transaction = $user->getDb()->beginTransaction();
                    if ($user->save()) {
                        $auth = new Auth([
                            'id_user' => $user->id,
                            'source' => $client->getId(),
                            'id_source' => (string)$attributes['id'],
                        ]);
                        if ($auth->save()) {
                            $transaction->commit();
                            Yii::$app->user->login($user);
                        } else {
                            print_r($auth->getErrors());
                        }
                    } else {
                        print_r($user->getErrors());
                    }
                }
            }
        } else { // user already logged in
            if (!$auth) { // add auth provider
                $auth = new Auth([
                    'id_user' => Yii::$app->user->id,
                    'source' => $client->getId(),
                    'id_source' => $attributes['id'],
                ]);
                $auth->save();
            }
        }
    }

5af882c962aec208423385.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2018-05-13
@butteff

There is a ready-made extension that uses yii2 oauth for social networks. Those. you can use it instead of your own bike.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question