Skip to content

Commit

Permalink
oauth: Add project to saved credentials when using the notebook authe…
Browse files Browse the repository at this point in the history
…nticator.

PiperOrigin-RevId: 668535162
  • Loading branch information
Nate Schmitz authored and Google Earth Engine Authors committed Aug 28, 2024
1 parent 0846f64 commit b1e0caf
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions python/ee/oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import http.server
import json
import os
import re
import shutil
import subprocess
import sys
Expand Down Expand Up @@ -226,6 +227,14 @@ def _in_jupyter_shell() -> bool:
return False


def _project_number_from_client_id(client_id: Optional[str]) -> Optional[str]:
"""Returns the project number associated with the given OAuth client ID."""
# Client IDs are of the form:
# PROJECTNUMBER-BASE32STUFF.apps.googleusercontent.com.
match = re.search(r'(\d+)-', client_id or '')
return match.group(1) if match else None


def _obtain_and_write_token(
auth_code: Optional[str] = None,
code_verifier: Optional[str] = None,
Expand Down Expand Up @@ -259,6 +268,9 @@ def _obtain_and_write_token(
token = request_token(auth_code.strip(), code_verifier, **client_info)
client_info['refresh_token'] = token
client_info['scopes'] = scopes
project = _project_number_from_client_id(client_info.get('client_id'))
if project:
client_info['project'] = project
write_private_json(get_credentials_path(), client_info)
print('\nSuccessfully saved authorization token.')

Expand Down

0 comments on commit b1e0caf

Please sign in to comment.