PHP code example of laravel-socialite-providers / socialite-wechat-service-account

1. Go to this page and download the library: Download laravel-socialite-providers/socialite-wechat-service-account 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/ */

    

laravel-socialite-providers / socialite-wechat-service-account example snippets


/**
 * The event handler mappings for the application.
 *
 * @var array
 */
protected $listen = [
    \SocialiteProviders\Manager\SocialiteWasCalled::class => [
        // add your listeners (aka providers) here
        'LaravelSocialiteProviders\\WeChatServiceAccount\\WeChatServiceAccountExtendSocialite@handle',
    ],
];

'wechat_service_account' => [
    'client_id' => env('WECHAT_SERVICE_ACCOUNT_APP_ID'),
    'client_secret' => env('WECHAT_SERVICE_ACCOUNT_APP_SECRET'),
    'redirect' => env('WECHAT_SERVICE_ACCOUNT_CALLBACK_URL'),
    'scopes' => preg_split('/,/', env('WECHAT_SERVICE_ACCOUNT_SCOPES'), null, PREG_SPLIT_NO_EMPTY), // can not use explode, see vlucas/phpdotenv#175
    'union_id_with' => preg_split('/,/', env('WECHAT_SERVICE_ACCOUNT_UNION_ID_WITH'), null, PREG_SPLIT_NO_EMPTY),
],

// default need user grant, and then you can get user info(nickname, unionid, openid...)
return Socialite::with('wechat_service_account')->redirect();

// not need user grant, and then you can only get openid
return Socialite::with('wechat_service_account')->scopes('snsapi_base')->redirect();

// to turn off stateless
return Socialite::with('wechat_service_account')->stateless(false)->redirect();

// to use stateless
return Socialite::with('wechat_service_account')->stateless()->redirect();

$clientId = "secret";
$clientSecret = "secret";
$redirectUrl = "http://yourdomain.com/api/redirect";
$additionalProviderConfig = [
    // Add additional configuration values here.
];
$config = new \SocialiteProviders\Manager\Config(
    $clientId,
    $clientSecret,
    $redirectUrl,
    $additionalProviderConfig
);

return Socialite::with('wechat_service_account')->setConfig($config)->redirect();

// default use openid as $user->id
$user = Socialite::driver('wechat_service_account')->user();
$accessTokenResponseBody = $user->accessTokenResponseBody;

// use unionid as $user->id
$user = Socialite::driver('wechat_service_account')->scopes('unionid')->user();

// not need user grant, you can only get openid
$user = Socialite::driver('wechat_service_account')->scopes('snsapi_base')->user();

$user = Socialite::driver('wechat_service_account')->setOpenId($openId)->userFromToken($token);
 php
'providers' => [
    // a whole bunch of providers
    // remove 'Laravel\Socialite\SocialiteServiceProvider',
    \SocialiteProviders\Manager\ServiceProvider::class, // add
];