-
Notifications
You must be signed in to change notification settings - Fork 7
/
GetRefreshToken.php
135 lines (114 loc) · 4.33 KB
/
GetRefreshToken.php
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
/**
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
require 'vendor/autoload.php';
use Google\Auth\CredentialsLoader;
use Google\Auth\OAuth2;
/**
* Command line example that prompts you for the required OAuth2 credentials
* to generate an offline refresh token for installed application flows.
*
* <p>You can then use this refresh token to generate access tokens to
* authenticate against the ads API(s) you're using.
*/
class GetRefreshToken {
/**
* @var string the OAuth2 scope for the AdWords API
* @see https://developers.google.com/adwords/api/docs/guides/authentication#scope
*/
const ADWORDS_API_SCOPE = 'https://www.googleapis.com/auth/adwords';
/**
* @var string the OAuth2 scope for the DFP API
* @see https://developers.google.com/doubleclick-publishers/docs/authentication#scope
*/
const DFP_API_SCOPE = 'https://www.googleapis.com/auth/dfp';
/**
* @var string the Google OAuth2 authorization URI for OAuth2 requests
* @see https://developers.google.com/identity/protocols/OAuth2InstalledApp#formingtheurl
*/
const AUTHORIZATION_URI = 'https://accounts.google.com/o/oauth2/v2/auth';
/**
* @var string the redirect URI for OAuth2 installed application flows
* @see https://developers.google.com/identity/protocols/OAuth2InstalledApp#formingtheurl
*/
const REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob';
public static function main() {
$PRODUCTS = [
['AdWords', self::ADWORDS_API_SCOPE],
['DFP', self::DFP_API_SCOPE],
['AdWords and DFP', self::ADWORDS_API_SCOPE . ' ' . self::DFP_API_SCOPE]
];
$stdin = fopen('php://stdin', 'r');
print 'Enter your OAuth2 client ID here: ';
$clientId = trim(fgets($stdin));
print 'Enter your OAuth2 client secret here: ';
$clientSecret = trim(fgets($stdin));
print "Select the ads API you're using: [0] AdWords [1] DFP [2] Both\n";
$api = trim(fgets($stdin));
while (!is_numeric($api)
|| !(strval(intval($api)) === $api)
|| !(intval($api) >= 0 && intval($api) <= 2)) {
print "Please enter a valid number for the ads API you're using: [0] "
. "AdWords [1] DFP [2] Both\n";
$api = trim(fgets($stdin));
}
$api = intval($api);
if ($api === 2) {
print '[OPTIONAL] enter any additional OAuth2 scopes as a space '
. 'delimited string here (the AdWords and DFP scopes are already '
. 'included): ';
} else {
printf(
'[OPTIONAL] enter any additional OAuth2 scopes as a space '
. 'delimited string here (the %s scope is already included): ',
$PRODUCTS[$api][0]
);
}
$scopes = $PRODUCTS[$api][1] . ' ' . trim(fgets($stdin));
$oauth2 = new OAuth2([
'authorizationUri' => self::AUTHORIZATION_URI,
'redirectUri' => self::REDIRECT_URI,
'tokenCredentialUri' => CredentialsLoader::TOKEN_CREDENTIAL_URI,
'clientId' => $clientId,
'clientSecret' => $clientSecret,
'scope' => $scopes
]);
printf(
"Log into the Google account you use for %s and visit the following "
. "URL:\n%s\n\n",
$PRODUCTS[$api][0],
$oauth2->buildFullAuthorizationUri()
);
print
'After approving the application, enter the authorization code here: ';
$code = trim(fgets($stdin));
fclose($stdin);
print "\n";
$oauth2->setCode($code);
$authToken = $oauth2->fetchAuthToken();
printf("Your refresh token is: %s\n\n", $authToken['refresh_token']);
printf(
"Copy the following lines to your 'adsapi_php.ini' file:\n"
. "clientId = \"%s\"\n"
. "clientSecret = \"%s\"\n"
. "refreshToken = \"%s\"\n",
$clientId,
$clientSecret,
$authToken['refresh_token']
);
}
}
GetRefreshToken::main();