PHP code example of alpa / oauth_manager

1. Go to this page and download the library: Download alpa/oauth_manager library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

alpa / oauth_manager example snippets



namespace Alpa\Icms\Oauth;
// Authorization
$providerData=new PoviderData([ // data from data base.
    'alias'=>'MyOauthServer',
    'class'=>'MyVendor\\MyOauthProvider',// Authorization provider class
    'clientId'=>'your client id ', 
    'clientSecret'=>'your secret token',
    'redirectUri'=>'redirect uri'
]);
$options=[
    'code'=>'code from your client' //authorization code. Should be sent by the client.
    ];
$pm=new ProviderManager($providerData,$options);
$provider=$pm->getProvider();// result object ProviderAbstract see https://oauth2-client.thephpleague.com/usage/
$token=$pm->getAccessToken();// // result object AccessTokenInterface see https://oauth2-client.thephpleague.com/usage/
$flip_fields=[
'name'=>'flip_name' // From the provider, the flip_name field data will come, and fit into the 'name' field.
];
$data=$pm->getResourceOwner($flip_fields)->toArray();//additional data that is passed in the access token during authorization will also be reflected.

$token_data=$token->jsonSerialize();  
// optional 
$additional_data=$token->getValues();
$token_data=array_merge($token_data,$additional_data);
// saving $token_data


namespace Alpa\Icms\Oauth;
// Authentication
$providerData=new PoviderData([ // settings data from data base.
    'alias'=>'MyOauthServer',
    'class'=>'MyVendor\\MyOauthProvider',// Authorization provider class
    'clientId'=>'your client id ', 
    'clientSecret'=>'your secret token',
    'redirectUri'=>'redirect uri'
]);
$pm=new ProviderManager($providerData);
$token=$pm->newAccessToken([ // data from data base.
    'access_token'=>' access token for your client',
    'resource_owner_id'=>'providers client id',
    'refresh_token'=>'token to refresh the access token',
    'expires'=>'(integer) the time at which the access token will expire',//
]);
$pm->setAccessToken($token);
$flip_fields=[
'name'=>'flip_name' // From the provider, the flip_name field data will come, and fit into the 'name' field.
];

$data=$pm->getResourceOwner($flip_fields)->toArray(); 
//saving data




$check=$pm->refreshAccessToken(); //replace token if time is up
//$check=$pm->refreshAccessToken(true); //force token refresh
if($check){
    $newToken=$pm->getAccessToken();
    $token_data=$newToken->jsonSerialize();
    
       // optional
    $additional_data=$newToken->getValues();
    $token_data=array_merge($token_data,$additional_data);
    
    // saving $token_data 
}


// For other code, see https://oauth2-client.thephpleague.com/usage/
//Example
  $authorizationUrl = $provider->getAuthorizationUrl();