auth
Helper methods for client authorisation.
You will only need to use these if you're planning to subclass the base client.
See also
This follows the OpenID Connect specification as described in the Google Identity documentation.
Scopes
Bases: Flag
An enum for API scopes.
Possible values are:
READONLY
— Don't include revenue data from reportsMONETARY_READONLY
— Only include revenue data from reportsALL
— Include all data in reports (this does not enable JWT scopes)OPENID
— Enable the OpenID scopePROFILE
— Include profile information in JWTsEMAIL
— Include email information in JWTsALL_JWT
— Include all available information in JWTs
Changed in version 5.1
- Added the
OPENID
,PROFILE
,EMAIL
, andALL_JWT
scopes - This now works like a flag enum rather than a normal one; this
doesn't introduce any breaking changes (unless you're using
analytix in a particularly unconventional way), but does mean
you can now use a
|
to concatenate scopes
Source code in analytix/auth.py
Secrets
dataclass
A set of API secrets.
This should always be created using the load_from
classmethod.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
type |
Literal['installed', 'web']
|
The application type. This will always be either "installed" or "web". |
required |
client_id |
str
|
The client ID. |
required |
project_id |
str
|
The name of the project. |
required |
auth_uri |
str
|
The authorisation server endpoint URI. |
required |
token_uri |
str
|
The token server endpoint URI. |
required |
auth_provider_x509_cert_url |
str
|
The URL of the public x509 certificate, used to verify the signature on JWTs, such as ID tokens, signed by the authentication provider. |
required |
client_secret |
str
|
The client secret. |
required |
redirect_uris |
List[str]
|
A list of valid redirection endpoint URIs. This list should match the list entered for the client ID on the API Access pane of the Google APIs Console. |
required |
Source code in analytix/auth.py
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
|
load_from
classmethod
Load secrets from a JSON file.
Changed in version 5.0
This used to be from_file
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
PathLike
|
The path to your secrets file. |
required |
Returns:
Type | Description |
---|---|
Secrets
|
Your secrets. |
Raises:
Type | Description |
---|---|
FileNotFoundError
|
No secrets file exists at the given path. |
JSONDecodeError
|
The given file is not a valid JSON file. |
Examples:
Source code in analytix/auth.py
Tokens
dataclass
OAuth tokens.
This should always be created using one of the available classmethods.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
access_token |
str
|
A token that can be sent to a Google API. |
required |
expires_in |
int
|
The remaining lifetime of the access token in seconds. |
required |
scope |
str
|
The scopes of access granted by the access_token expressed as a list of space-delimited, case-sensitive strings. |
required |
token_type |
Literal['Bearer']
|
Identifies the type of token returned. This will always be "Bearer". |
required |
refresh_token |
str
|
A token that can be used to refresh your access token. |
required |
id_token |
Optional[str]
|
A JWT that contains identity information about the user that is
digitally signed by Google. This will be |
None
|
Warnings
The expires_in
field is never updated by analytix, and as such
will always be 3599
unless you update it yourself.
Source code in analytix/auth.py
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 |
|
from_json
classmethod
Load tokens from raw JSON data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
Union[str, bytes]
|
Your tokens in JSON form. |
required |
Returns:
Type | Description |
---|---|
Tokens
|
Your tokens. |
Raises:
Type | Description |
---|---|
JSONDecodeError
|
The given file is not a valid JSON file. |
Examples:
Source code in analytix/auth.py
load_from
classmethod
Load tokens from a JSON file.
Changed in version 5.0
This used to be from_file
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
PathLike
|
The path to your tokens file. |
required |
Returns:
Type | Description |
---|---|
Tokens
|
Your tokens. |
Raises:
Type | Description |
---|---|
FileNotFoundError
|
No tokens file exists at the given path. |
JSONDecodeError
|
The given file is not a valid JSON file. |
Examples:
Source code in analytix/auth.py
refresh
Updates your tokens to match those you refreshed.
Changed in version 5.0
This used to be update
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
Union[str, bytes]
|
Your refreshed tokens in JSON form. These will not entirely replace your previous tokens, but instead update any out-of-date keys. |
required |
Returns:
Type | Description |
---|---|
Tokens
|
Your refreshed tokens. |
See Also
- This method does not actually refresh your access token;
for that, you'll need to use
Client.refresh_access_token
. - To save tokens, you'll need the
save_to
method.
Examples:
Source code in analytix/auth.py
save_to
Save your tokens to disk.
Changed in version 5.0
This used to be write
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
PathLike
|
The path to save your tokens to. |
required |
Returns:
Type | Description |
---|---|
None
|
This method doesn't return anything. |
Examples:
Source code in analytix/auth.py
auth_uri
Returns the authentication URI and parameters.
Changed in version 5.0
- This now takes scopes as a parameter
- This now returns headers (albeit always empty) to be more consistent with other functions
- The redirect URI to use is now chosen more intelligently -- it will be the first in the list not intended to be used in OOB authorisation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
secrets |
Secrets
|
Your secrets. |
required |
scopes |
Scopes
|
The scopes to allow in requests. |
required |
port |
int
|
The websocket port you wish to use. |
required |
Returns:
Name | Type | Description |
---|---|---|
auth_uri |
str
|
The computed authentication URI. |
params |
Dict[str, str]
|
The query parameters as a dictionary. |
headers |
Dict[str, str]
|
Necessary request headers. This is always empty. |
Source code in analytix/auth.py
refresh_uri
Returns the refresh URI, data, and headers.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
secrets |
Secrets
|
Your secrets. |
required |
token |
str
|
Your refresh token. |
required |
Returns:
Name | Type | Description |
---|---|---|
token_uri |
str
|
Your token URI. |
data |
Dict[str, str]
|
Necessary request data. |
headers |
Dict[str, str]
|
Necessary request headers. |
Source code in analytix/auth.py
run_flow
Start a webserver and listen for an authentication code.
Changed in version 5.0
This used to be authenticate
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
auth_params |
Dict[str, str]
|
The parameters generated from the |
required |
Returns:
Type | Description |
---|---|
str
|
Your authentication code. |
Raises:
Type | Description |
---|---|
AuthorisationError
|
|
Source code in analytix/auth.py
state_token
Generates a state token.
Returns:
Type | Description |
---|---|
str
|
A new state token. |
Examples:
Source code in analytix/auth.py
token_uri
Returns the token URI, data, and headers.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
secrets |
Secrets
|
Your secrets. |
required |
code |
str
|
Your authentication code. |
required |
redirect_uri |
str
|
Your redirect URI. This should be identical to the one you
generated in |
required |
Returns:
Name | Type | Description |
---|---|---|
token_uri |
str
|
Your token URI. |
data |
Dict[str, str]
|
Necessary request data. |
headers |
Dict[str, str]
|
Necessary request headers. |