N
N
Nikita Danilov2015-10-16 06:12:59
Yii
Nikita Danilov, 2015-10-16 06:12:59

Yii2 EAuth extension. How to fix the VK authorization link from the social button?

Good afternoon!
I became interested in authorization through social networks in Yii2, came across this project: nodge.ru/yii-eauth/demo2 This link is great for logging in via vKontakte)
I downloaded it to my computer from the repository https://github.com/Nodge/yii2 -eauth-demo/ and started experimenting on a local XAMPP server. Having struggled with configuring Apache (I made a virtual host for an address like yii2vk.com) and some other problems, I ran into the fact that it doesn’t authorize from the localhost from the VK button from the login page due to the fact that authorization links have a perverted look like yii2vk.com/ login?service=vkontakte - while on the original demo site they had a normal view like nodge.ru/yii-eauth/demo2/login/vkontakte
And in general, links from all social buttons on the localhost look like login?service=...
Moreover, if you directly drive in yii2vk.com/login/vkontakte in the address bar , then everything is perfectly authorized, even though the localhost))) i.e. . The connection with VKontakte itself works. It's the buttons of the EAuth extension widget that don't work because of strange links from them.
With the CNC, in general, everything seems to be fine, otherwise yii2vk.com/login would issue a 404 (as it did at the very beginning, until I configured Apache correctly so that I didn’t have to drive in index.php? r = site/login). And the prettyUrl themselves in the application were included by the authors who posted them on Github. The urlManager rules say 'login/' => 'site/login' as advised in the instructions for the extension.
What can be wrong? Why are the links from the social buttons on the login page correct on the demo site, but everything is crooked on mine?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Klyuev, 2015-10-16
@BBird

In my project, I did the following:
1. Filed my own class "OAuth2_VKontakteService.php"
2. Inherited it from "nodge\eauth\services\VKontakteOAuth2Service"
3. Redefined parameters and methods in it.

<?php
namespace frontend\models;

use common\models\User;
use nodge\eauth\services\VKontakteOAuth2Service;

/**
 * VKontakte provider class.
 *
 * @package application.extensions.eauth.services
 */
class OAuth2_VKontakteService extends VKontakteOAuth2Service
{

    const SCOPE_FRIENDS = 'friends';
    const SCOPE_EMAIL = 'email';

    protected $name = 'vkontakte';
    protected $title = 'VK.com';
    protected $type = 'OAuth2';
    protected $jsArguments = array('popup' => array('width' => 500, 'height' => 450));

    protected $scopes = [self::SCOPE_EMAIL];
    protected $providerOptions = [
        'authorize' => 'http://api.vk.com/oauth/authorize',
        'access_token' => 'https://api.vk.com/oauth/access_token'
    ];
    protected $baseApiUrl = 'https://api.vk.com/method/';

    protected function fetchAttributes()
    {
        $tokenData = $this->getAccessTokenData();
        $info = $this->makeSignedRequest('users.get.json', array(
            'query' => array(
                'uids' => $tokenData['params']['user_id'],
                'fields' => 'email, sex, bdate, photo_big'
            )
        ));

        $info = $info['response'][0];

        $this->attributes['service'] = $this->getServiceName();

        $this->attributes['id'] = $info['uid'];
        $this->attributes['url'] = 'http://vk.com/id' . $info['uid'];
        $this->attributes['name'] = $info['first_name'] . ' ' . $info['last_name'];
        $this->attributes['firstname'] = $info['first_name'];
        $this->attributes['lastname'] = $info['last_name'];
        $this->attributes['gender'] = ($info['sex'] === 2) ? User::SEX_MALE : User::SEX_FEMALE;;
        $b = explode('.', $info['bdate']);
        $this->attributes['birthday'] = $b[2] . '-' . $b[1] .'-' . $b[0];
        $this->attributes['photo'] = $info['photo_big'];
        $this->attributes['email'] = $tokenData['params']['email'];

        $this->attributes['info'] = $info;

        return true;
    }
}

PS: I did the same for twitter, facebook, odnoklasniki, etc. Why did I do it? And in order for all the OAuth services used to return the received data in the same format, because after authorization they are transferred to the model and validated.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question