examples
directory, service settings in hybridauth/config.php
, install.php
is there, which will help us create the settings file automatically, open it:test.php
file in the root of the site: <?php session_start(); $config = dirname(__FILE__) . '/hybridauth/config.php'; require_once( "hybridauth/Hybrid/Auth.php" ); if( isset( $_GET["login"] ) ) { try { $hybridauth = new Hybrid_Auth( $config ); $adapter = $hybridauth->authenticate( "twitter" ); $user_profile = $adapter->getUserProfile(); } catch( Exception $e ) { die( "<b>got an error!</b> " . $e->getMessage() ); } } if( ! isset( $user_profile ) ) { ?> <p> .</p> <h2><a href ="test.php?login=1"></a></h2> <?php } else { ?> <fieldset> <legend>twitter </legend> <b> <?php echo $user_profile->displayName; ?></b> <hr /> <b>Hybridauth access tokens for twitter:</b> <pre><?php print_r( $adapter->getAccessToken() ); ?></pre> </fieldset> <?php } ?>
getUserProfile
receives in the case of Twitter. To do this, open the directory hybridauth/Hybrid/Providers
, where the settings of the authorization services are stored as files, we need Twitter.php
. Find the getUserProfile
method and see the resulting data: $this->user->profile->identifier = (property_exists($response,'id'))?$response->id:""; $this->user->profile->displayName = (property_exists($response,'screen_name'))?$response->screen_name:""; $this->user->profile->description = (property_exists($response,'description'))?$response->description:""; $this->user->profile->firstName = (property_exists($response,'name'))?$response->name:""; $this->user->profile->photoURL = (property_exists($response,'profile_image_url'))?$response->profile_image_url:""; $this->user->profile->profileURL = (property_exists($response,'screen_name'))?("http://twitter.com/".$response->screen_name):""; $this->user->profile->webSiteURL = (property_exists($response,'url'))?$response->url:""; $this->user->profile->region = (property_exists($response,'location'))?$response->location:"";
hybridauth/Hybrid/Providers/Yandex.php
. Yandex uses OAuth2 for authorization, so our class will inherit it: class Hybrid_Providers_Yandex extends Hybrid_Provider_Model_OAuth2
Hybrid_Provider_Model_OAuth1
, and for OpenID, Hybrid_Provider_Model_OpenID
. Now we need to figure out what data Yandex needs to authorize the user and access his profile data. First, create a Yandex API application, to get ID and Secret fields - go here and register a new application. To get the profile data, we will use the API Login.Yandex, where we will mark the mailbox fields and the user's full name.hybridauth/config.php
and add to the end: "Yandex" => array ( "enabled" => true, "keys" => array ( "id" => "Id ", "secret" => " " ) ),
oauth.yandex.ru/authorize
oauth.yandex.ru/authorize
, and to get a token to oauth.yandex.ru/token
oauth.yandex.ru/token
. It remains to find out the address of the API Login. Yandex - for this we open the API documentation and find the Login through Yandex, and there OAuth + Yandex.Login . Here we find that to obtain information about the user need to contact login.yandex.ru/info
login.yandex.ru/info
. Great, we have everything we need, go back to Yandex.php, in the function initialize we specify links to the API: $this->api->api_base_url = "https://login.yandex.ru/info"; $this->api->authorize_url = "https://oauth.yandex.ru/authorize"; $this->api->token_url = "https://oauth.yandex.ru/token";
oauth_token
parameter: $this->api->sign_token_name = "oauth_token";
$response = $this->api->api( "?format=json" );
$this->user->profile->identifier = (property_exists($response,'id'))?$response->id:""; $this->user->profile->firstName = (property_exists($response,'real_name'))?$response->real_name:""; $this->user->profile->displayName = (property_exists($response,'display_name'))?$response->display_name:""; $this->user->profile->photoURL = 'http://upics.yandex.net/'. $this->user->profile->identifier .'/normal'; $this->user->profile->gender = (property_exists($response,'sex'))?$response->sex:""; $this->user->profile->email = (property_exists($response,'default_email'))?$response->default_email:""; $this->user->profile->emailVerified = (property_exists($response,'default_email'))?$response->default_email:"";
<?php class Hybrid_Providers_Yandex extends Hybrid_Provider_Model_OAuth2 { function initialize() { parent::initialize(); $this->api->api_base_url = "https://login.yandex.ru/info"; $this->api->authorize_url = "https://oauth.yandex.ru/authorize"; $this->api->token_url = "https://oauth.yandex.ru/token"; $this->api->sign_token_name = "oauth_token"; } function getUserProfile() { $response = $this->api->api( "?format=json" ); if ( !isset( $response->id ) ) { throw new Exception( "User profile request failed! {$this->providerId} returned an invalide response.", 6 ); } $this->user->profile->identifier = (property_exists($response,'id'))?$response->id:""; $this->user->profile->firstName = (property_exists($response,'real_name'))?$response->real_name:""; $this->user->profile->displayName = (property_exists($response,'display_name'))?$response->display_name:""; $this->user->profile->photoURL = 'http://upics.yandex.net/'. $this->user->profile->identifier .'/normal'; $this->user->profile->gender = (property_exists($response,'sex'))?$response->sex:""; $this->user->profile->email = (property_exists($response,'default_email'))?$response->default_email:""; $this->user->profile->emailVerified = (property_exists($response,'default_email'))?$response->default_email:""; return $this->user->profile; } } ?>
$adapter = $hybridauth->authenticate( "yandex" );
Source: https://habr.com/ru/post/149187/
All Articles