|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SchulzeFelix\AdWords\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use Google\Auth\CredentialsLoader; |
|
7
|
|
|
use Google\Auth\OAuth2; |
|
8
|
|
|
use Illuminate\Console\Command; |
|
9
|
|
|
|
|
10
|
|
|
class GenerateRefreshTokenCommand extends Command |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var string the OAuth2 scope for the AdWords API |
|
14
|
|
|
* @see https://developers.google.com/adwords/api/docs/guides/authentication#scope |
|
15
|
|
|
*/ |
|
16
|
|
|
const ADWORDS_API_SCOPE = 'https://www.googleapis.com/auth/adwords'; |
|
17
|
|
|
/** |
|
18
|
|
|
* @var string the Google OAuth2 authorization URI for OAuth2 requests |
|
19
|
|
|
* @see https://developers.google.com/identity/protocols/OAuth2InstalledApp#formingtheurl |
|
20
|
|
|
*/ |
|
21
|
|
|
const AUTHORIZATION_URI = 'https://accounts.google.com/o/oauth2/v2/auth'; |
|
22
|
|
|
/** |
|
23
|
|
|
* @var string the redirect URI for OAuth2 installed application flows |
|
24
|
|
|
* @see https://developers.google.com/identity/protocols/OAuth2InstalledApp#formingtheurl |
|
25
|
|
|
*/ |
|
26
|
|
|
const REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob'; |
|
27
|
|
|
/** |
|
28
|
|
|
* Console command signature. |
|
29
|
|
|
* |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $signature = 'adwords:token'; |
|
33
|
|
|
/** |
|
34
|
|
|
* Description. |
|
35
|
|
|
* |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $description = 'Generate a new refresh token for Google Ads API'; |
|
39
|
|
|
|
|
40
|
|
|
public function handle() |
|
41
|
|
|
{ |
|
42
|
|
|
if (! $config = $this->config()) { |
|
43
|
|
|
$this->error('Please provide a valid configuration.'); |
|
44
|
|
|
|
|
45
|
|
|
return false; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
$clientId = $config['client_id']; |
|
49
|
|
|
$clientSecret = $config['client_secret']; |
|
50
|
|
|
$scopes = self::ADWORDS_API_SCOPE; |
|
51
|
|
|
|
|
52
|
|
|
$oauth2 = new OAuth2([ |
|
53
|
|
|
'authorizationUri' => self::AUTHORIZATION_URI, |
|
54
|
|
|
'redirectUri' => self::REDIRECT_URI, |
|
55
|
|
|
'tokenCredentialUri' => CredentialsLoader::TOKEN_CREDENTIAL_URI, |
|
56
|
|
|
'clientId' => $clientId, |
|
57
|
|
|
'clientSecret' => $clientSecret, |
|
58
|
|
|
'scope' => $scopes, |
|
59
|
|
|
]); |
|
60
|
|
|
|
|
61
|
|
|
$this->info("Please sign in to your AdWords account, and open following url:\n"); |
|
62
|
|
|
$this->line(sprintf( |
|
63
|
|
|
'%s', |
|
64
|
|
|
$oauth2->buildFullAuthorizationUri([ |
|
65
|
|
|
'access_type' => 'offline', |
|
66
|
|
|
]) |
|
67
|
|
|
)); |
|
68
|
|
|
|
|
69
|
|
|
$accessToken = $this->ask('Insert your access token'); |
|
70
|
|
|
|
|
71
|
|
|
try { |
|
72
|
|
|
$oauth2->setCode($accessToken); |
|
73
|
|
|
$authToken = $oauth2->fetchAuthToken(); |
|
74
|
|
|
} catch (Exception $exception) { |
|
75
|
|
|
$this->error($exception->getMessage()); |
|
76
|
|
|
|
|
77
|
|
|
return false; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
if (! isset($authToken)) { |
|
81
|
|
|
$this->error('Error fetching the refresh token'); |
|
82
|
|
|
|
|
83
|
|
|
return false; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$this->comment('Insert the refresh token in your adwords configuration file (config/adwords-targeting-idea-service.php)'); |
|
87
|
|
|
|
|
88
|
|
|
$this->info(sprintf( |
|
89
|
|
|
'Refresh token: "%s"', |
|
90
|
|
|
$authToken['refresh_token'] |
|
91
|
|
|
)); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Configuration. |
|
96
|
|
|
* |
|
97
|
|
|
* @return bool|array |
|
98
|
|
|
*/ |
|
99
|
|
|
private function config() |
|
100
|
|
|
{ |
|
101
|
|
|
/** @var null|array $config */ |
|
102
|
|
|
$config = config('adwords-targeting-idea-service'); |
|
103
|
|
|
if (is_null($config) || ! count($config)) { |
|
104
|
|
|
return false; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return $config; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|