YandexDirect.php
1.62 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
namespace App\Service\API;
use Illuminate\Http\Client\Response;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
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 = [
'grant_type' => 'authorization_code',
'code' => $code,
'client_id' => config('api.yandex.id'),
'client_secret' => config('api.yandex.password'),
];
$data = Http::asForm()->post($this->getTokenUrl(), $data);
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;
}
public function execute(): Response
{
// Log::debug($this->request->getBody());
$result = Http::withBody($this->request->getBody(), 'none')
->withHeaders($this->request->getHeaders())
->withToken($this->request->getToken()->token)
->post($this->request->getUrl());
// Log::debug($result->json());
return $result;
}
}