1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SchulzeFelix\AdWords\Commands; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Google\Auth\OAuth2; |
7
|
|
|
use Illuminate\Console\Command; |
8
|
|
|
use Google\Auth\CredentialsLoader; |
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
|
|
|
exit; |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$clientId = $config['client_id']; |
48
|
|
|
$clientSecret = $config['client_secret']; |
49
|
|
|
$scopes = self::ADWORDS_API_SCOPE; |
50
|
|
|
|
51
|
|
|
$oauth2 = new OAuth2([ |
52
|
|
|
'authorizationUri' => self::AUTHORIZATION_URI, |
53
|
|
|
'redirectUri' => self::REDIRECT_URI, |
54
|
|
|
'tokenCredentialUri' => CredentialsLoader::TOKEN_CREDENTIAL_URI, |
55
|
|
|
'clientId' => $clientId, |
56
|
|
|
'clientSecret' => $clientSecret, |
57
|
|
|
'scope' => $scopes |
58
|
|
|
]); |
59
|
|
|
$this->info("Please sign in to your AdWords account, and open following url:\n"); |
60
|
|
|
$this->line(sprintf( |
61
|
|
|
"%s", |
62
|
|
|
$oauth2->buildFullAuthorizationUri([ |
63
|
|
|
'access_type' => 'offline', |
64
|
|
|
]) |
65
|
|
|
)); |
66
|
|
|
// Retrieve token |
67
|
|
|
$accessToken = $this->ask('Insert your access token'); |
68
|
|
|
// Fetch auth token |
69
|
|
|
try { |
70
|
|
|
$oauth2->setCode($accessToken); |
71
|
|
|
$authToken = $oauth2->fetchAuthToken(); |
72
|
|
|
} catch (Exception $exception) { |
73
|
|
|
$this->error($exception->getMessage()); |
74
|
|
|
exit; |
|
|
|
|
75
|
|
|
} |
76
|
|
|
if (!isset($authToken)) { |
77
|
|
|
$this->error('Error fetching the refresh token'); |
78
|
|
|
exit; |
|
|
|
|
79
|
|
|
} |
80
|
|
|
$this->comment('Insert the refresh token in your adwords configuration file (config/adwords-targeting-idea-service.php)'); |
81
|
|
|
// Print refresh token |
82
|
|
|
$this->info(sprintf( |
83
|
|
|
'Refresh token: "%s"', |
84
|
|
|
$authToken['refresh_token'] |
85
|
|
|
)); |
86
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Configuration |
92
|
|
|
* |
93
|
|
|
* @return bool|array |
94
|
|
|
*/ |
95
|
|
|
private function config() |
96
|
|
|
{ |
97
|
|
|
/** @var null|array $config */ |
98
|
|
|
$config = config('adwords-targeting-idea-service'); |
99
|
|
|
if (is_null($config) || !count($config)) { |
100
|
|
|
return false; |
101
|
|
|
} |
102
|
|
|
return $config; |
103
|
|
|
} |
104
|
|
|
} |
An exit expression should only be used in rare cases. For example, if you write a short command line script.
In most cases however, using an
exit
expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.