Install & Compatibility
Where this runs
No compatibility data collected yet for this library.
Code
Verified usage
This quickstart demonstrates how to execute the `oauth2-tool` command-line utility from Python using `subprocess`. The tool itself is designed to be run from the terminal to interactively generate OAuth2 credentials. It requires a `client_secrets.json` file (downloaded from Google Cloud Console) and will typically open a browser for user authentication. The output is a credential file (e.g., in `~/.oauth2_credentials`).
import subprocess
import os
# This tool is a command-line utility, not a Python library for direct import.
# The following Python code demonstrates how to execute it via subprocess.
# Replace 'client_secrets.json' with your actual Google client secrets file.
# Ensure 'client_secrets.json' is in the same directory or provide its full path.
client_secrets_file = os.path.join(os.getcwd(), 'client_secrets.json') # Example placeholder
# Create a dummy client_secrets.json for demonstration if it doesn't exist
# In a real scenario, this file would be downloaded from Google Cloud Console.
if not os.path.exists(client_secrets_file):
print(f"Warning: '{client_secrets_file}' not found. Creating a dummy file for demonstration.")
print("This dummy file will NOT work for actual authentication.")
with open(client_secrets_file, 'w') as f:
f.write("""
{
"web": {
"client_id": "YOUR_CLIENT_ID",
"project_id": "your-project-id",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_secret": "YOUR_CLIENT_SECRET",
"redirect_uris": ["http://localhost"]
}
}
""")
try:
# Run the oauth2-tool command. It will likely open a browser for authentication.
# For this example, we use a very short timeout and expect it to fail
# because it's interactive and needs user input.
print(f"\nAttempting to run: oauth2-tool {client_secrets_file}")
print("This command requires user interaction (browser). It will likely time out in this automated run.")
process = subprocess.run(
['oauth2-tool', client_secrets_file],
capture_output=True,
text=True,
check=False, # Don't raise CalledProcessError for non-zero exit codes
timeout=10 # Set a short timeout as it expects user interaction
)
print("\n--- oauth2-tool STDOUT ---")
print(process.stdout)
print("\n--- oauth2-tool STDERR ---")
print(process.stderr)
if process.returncode != 0:
print(f"\nCommand exited with non-zero status code {process.returncode}. This is expected for interactive tools in automated runs.")
except FileNotFoundError:
print("Error: 'oauth2-tool' command not found. Ensure it's installed and in your PATH.")
except subprocess.TimeoutExpired:
print("\nCommand timed out. This is expected as 'oauth2-tool' requires browser interaction.")
# If it timed out, process.stdout and process.stderr are available from the exception object
# print(f"STDOUT: {e.stdout.decode()}\nSTDERR: {e.stderr.decode()}")
except Exception as e:
print(f"An unexpected error occurred: {e}")
# In a real interactive environment, after running the tool, it would open a browser
# for you to authenticate, and then save the credentials to a file (e.g., ~/.oauth2_credentials).
google-oauth2-tool --version
Errors
Common errors & fixes
oauth2-tool: command not found
The `oauth2-tool` command-line script is either not installed, or its installation directory is not in your system's PATH.
fixEnsure the package is installed: `pip install google-oauth2-tool`. If installed within a virtual environment, activate it or specify the full path to the executable (e.g., `venv/bin/oauth2-tool`).
Error: invalid_client - The OAuth client was not found.
The `client_secrets.json` file provided is either invalid, malformed, or contains credentials that are no longer recognized or active by Google's OAuth 2.0 servers due to changes in Google Cloud Console projects or API deprecations.
fixThis tool is likely outdated. Verify your `client_secrets.json` in the Google Cloud Console. For new projects, it is highly recommended to use the `google-auth` library directly or generate credentials via the Google Cloud Console's official methods, which are more robust and up-to-date.
AttributeError: module 'google.oauth2.credentials' has no attribute 'Credentials'
This error often occurs when `google-oauth2-tool` (which depends on older versions of `google-auth`) is installed in an environment that also has a newer version of `google-auth` or other conflicting Google libraries. The older tool expects a different API surface.
fixCreate a dedicated virtual environment for `google-oauth2-tool` to isolate its dependencies: `python -m venv oauth2_env && source oauth2_env/bin/activate && pip install google-oauth2-tool`. However, due to the tool's age, consider abandoning its use entirely in favor of modern Google authentication methods.
Upgrade
Version history
0.0.3latest on PyPI · released May 1, 2019
Audit
Dependencies
google-api-python-clientrequiredRequired for Google API interactions, likely pinned to an old version.
google-auth-oauthlibrequiredHandles OAuth 2.0 authorization flow, likely pinned to an old version.
google-authrequiredCore Google authentication library, likely pinned to an old version.
Resources
No resource links recorded.