Comments (6)
Here works catch
namespace SocialiteProviders\VKontakte;
use Illuminate\Support\Arr;
use Laravel\Socialite\Two\ProviderInterface;
use SocialiteProviders\Manager\OAuth2\AbstractProvider;
use SocialiteProviders\Manager\OAuth2\User;
class Provider extends AbstractProvider implements ProviderInterface
{
protected $fields = ['uid', 'email', 'first_name', 'last_name', 'screen_name', 'photo'];
/**
* Unique Provider Identifier.
*/
const IDENTIFIER = 'VKONTAKTE';
/**
* {@inheritdoc}
*/
protected $scopes = ['email'];
/**
* {@inheritdoc}
*/
const API_VERSION = '5.74';
protected $parameters = [
'v' => self::API_VERSION,
];
/**
* {@inheritdoc}
*/
protected function getAuthUrl($state)
{
return $this->buildAuthUrlFromBase(
'https://oauth.vk.com/authorize', $state
);
}
/**
* {@inheritdoc}
*/
protected function getTokenUrl()
{
return 'https://oauth.vk.com/access_token';
}
/**
* {@inheritdoc}
*/
protected function getUserByToken($token)
{
$from_token = [];
if(is_array($token)) {
$from_token["email"] = $token["email"];
$token = $token["access_token"];
}
$lang = $this->getConfig('lang');
$response = $this->getHttpClient()->get(
'https://api.vk.com/method/users.get?' . http_build_query([
"access_token" => $token,
"fields" => implode(",", $this->fields),
"lang" => $lang ?: "ru",
'v' => self::API_VERSION,
])
);
$response = json_decode($response->getBody()->getContents(), true);
if(!isset($response["response"][0])) {
throw new \RuntimeException("Result of VK response is bad: " . sprintf($response, true));
}
$result = $response["response"][0];
return array_merge($result, $from_token);
}
/**
* {@inheritdoc}
*/
protected function mapUserToObject(array $user)
{
return (new User())->setRaw($user)->map([
'id' => Arr::get($user, 'id'),
'nickname' => Arr::get($user, 'screen_name'),
'name' => trim(Arr::get($user, 'first_name').' '.Arr::get($user, 'last_name')),
'email' => Arr::get($user, 'email'),
'avatar' => Arr::get($user, 'photo'),
]);
}
protected function parseAccessToken($body) {
return json_decode($body, true);
}
public function user() {
if ($this->hasInvalidState()) {
throw new \RuntimeException();
}
$user = $this->mapUserToObject($this->getUserByToken(
$token = $this->getAccessTokenResponse($this->getCode())
));
return $user->setToken(array_get($token, 'access_token'));
}
/**
* {@inheritdoc}
*/
protected function getTokenFields($code)
{
return array_merge(parent::getTokenFields($code), [
'grant_type' => 'authorization_code',
]);
}
/**
* Set the user fields to request from Vkontakte.
*
* @param array $fields
*
* @return $this
*/
public function fields(array $fields)
{
$this->fields = $fields;
return $this;
}
/**
* {@inheritdoc}
*/
public static function additionalConfigKeys()
{
return ['lang'];
}
}
from providers.
@aleksandrguru Could you make a pull request?
from providers.
I found email here: $socialiteUser->accessTokenResponseBody['email']
. I was confused too when did not found email at $socialiteUser>getEmail()
or $socialiteUser->user['email']
.
from providers.
@frutality And How do you use this method? Because, if I try to do it I see the error: Call to undefined method SocialiteProviders\Manager\OAuth2\User::accessTokenResponseBody()
.
$user = Socialite::driver($provider)->user(); dd($user->accessTokenResponseBody('email'));
from providers.
It's not a method.
if ($socialiteUser->accessTokenResponseBody) {
$email = $socialiteUser->accessTokenResponseBody['email'];
}
from providers.
Can anyone PR this?
from providers.
Related Issues (20)
- Stateless discord oauth gives 401 unauthorized error when trying to collect user HOT 2
- OIDC token request results in failure multiple_client_credentials HOT 1
- Azure B2C provider failing with not supported? HOT 6
- Upgrade monorepo-builder to v11
- SAML2 provider - can't configure repeated_id_ttl and clock_skew HOT 1
- PKCE crashes when using stateless
- Azure B2C provider puts scope incorrectly HOT 1
- AzureADB2C has error in emails mapping HOT 3
- SAML2 Driver HOT 2
- Azure: Add support for optional post_logout_redirect_uri HOT 2
- Docs on how to use `SocialiteWasCalled[]` in Laravel 11 HOT 3
- Microsoft not returning `id_token` when using `refresh_token` HOT 1
- Apple Provider Regenerates the Session HOT 8
- Microsoft-azure laravel 11 support HOT 5
- HubSpot redirect without specifying scope throws an error
- composer require socialiteproviders/twitter SocialiteProviders\Twitter\Provider does not extend Laravel\Socialite\Two\AbstractProvider HOT 5
- Azure AD B2C mapping fails after upgrade from 4.1.1 to 4.3.0 HOT 1
- 500 Server Error with TikTok HOT 9
- Could not find a matching version of package socialiteproviders/claveunica HOT 1
- ClaveUnica Provider: new release HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from providers.