YandexDirect.php
1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php
namespace App\Service\API;
use Illuminate\Support\Facades\Http;
class YandexDirect extends API{
private $url = 'https://oauth.yandex.ru/token';
function getAuthLink()
{
return 'https://oauth.yandex.ru/authorize?response_type=code&client_id=' . config('api.yandex.id');
}
function getToken($code) {
$data = Http::post($this->getTokenUrl(), [
'grant_type' => 'authorization_code',
'code' => $code,
'client_id' => config('api.yandex.id'),
'client_secret' => config('api.yandex.password'),
]);
return $this->extractToken($data);
}
protected function getTokenUrl() {
return $this->url;
}
function extractToken($data){
$token = $data->json()->access_token;
$login = $this->getLoginByToken($token);
return ['token' => $token, 'login' => $login];
}
public function getLoginByToken($token) {
$url = "https://login.yandex.ru/info?format=json&oauth_token={$token}";
$data = Http::get($url)->json();
return isset($data->login) ? $data->login : false;
}
}