oauth2client is a Python library designed for interacting with OAuth 2.0 protected resources, primarily for Google APIs. As of version 4.1.0, the library is officially deprecated, with no new features planned and limited support. Users are strongly advised to migrate to `google-auth` and `oauthlib` for modern and actively maintained OAuth 2.0 client functionality. The current version is 4.1.3.
Install & Compatibility
Where this runs
tested against v4.1.3 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.925 runs
installs and imports cleanly · install 0.0s · import 0.542s · 22.9MB
glibcpy 3.10–3.925 runs
installs and imports cleanly · install 2.1s · import 0.484s · 23MB
21MB installed
● package 21MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
GoogleCredentials
✓ from oauth2client.client import GoogleCredentials
OAuth2WebServerFlow
✓ from oauth2client.client import OAuth2WebServerFlow
run_flow
✓ from oauth2client.tools import run_flow
Storage
✓ from oauth2client.file import Storage
This quickstart demonstrates a basic OAuth 2.0 web server flow using `oauth2client.client.OAuth2WebServerFlow` and `oauth2client.tools.run_flow` to obtain user credentials. The `run_flow` utility is suitable for local development as it opens a browser for user interaction. In a production web application, the authorization redirect and code exchange steps would be handled explicitly. Credentials are saved to a local JSON file using `oauth2client.file.Storage` for simplicity.
import os
import httplib2 # oauth2client relies heavily on httplib2
from oauth2client.client import OAuth2WebServerFlow
from oauth2client.tools import run_flow
from oauth2client.file import Storage
# NOTE: This library is deprecated. Consider migrating to google-auth and oauthlib.
# These values would typically come from your Google API Console project.
# For a quickstart, we use environment variables for demonstration.
CLIENT_ID = os.environ.get('OAUTH2CLIENT_CLIENT_ID', 'YOUR_CLIENT_ID')
CLIENT_SECRET = os.environ.get('OAUTH2CLIENT_CLIENT_SECRET', 'YOUR_CLIENT_SECRET')
REDIRECT_URI = 'http://localhost:8080/oauth2callback' # Must match a registered redirect URI in your Google project
def main():
if CLIENT_ID == 'YOUR_CLIENT_ID' or CLIENT_SECRET == 'YOUR_CLIENT_SECRET':
print("Please set OAUTH2CLIENT_CLIENT_ID and OAUTH2CLIENT_CLIENT_SECRET environment variables,")
print("or replace 'YOUR_CLIENT_ID' and 'YOUR_CLIENT_SECRET' in the code.")
return
# 1. Create a flow object for a web server application
flow = OAuth2WebServerFlow(
client_id=CLIENT_ID,
client_secret=CLIENT_SECRET,
scope='https://www.googleapis.com/auth/userinfo.email',
redirect_uri=REDIRECT_URI
)
# 2. Authorize the user
# The `run_flow` function is typically used for local development and
# will open a browser window for user authentication.
# In a production web application, you would manage the redirects and
# authorization code exchange manually.
try:
http = httplib2.Http()
# Simple file storage for credentials; in production, use a secure database.
storage = Storage('oauth2client_creds.json')
print(f"Attempting to authorize. Please check your browser or navigate to: {flow.step1_get_authorize_url()}")
credentials = run_flow(flow, storage, http=http)
print(f"\nAuthorization successful!")
print(f"Access Token: {credentials.access_token[:10]}...{credentials.access_token[-10:]}")
if credentials.refresh_token:
print(f"Refresh Token: {credentials.refresh_token[:10]}...{credentials.refresh_token[-10:]}")
else:
print("No Refresh Token (may be due to scope or one-time access).")
print(f"Credentials saved to: {storage.filename}")
except Exception as e:
print(f"\nAn error occurred during OAuth2 flow: {e}")
print("Ensure your client ID, client secret, and redirect URI are correctly configured and match your Google project.")
print("Also, ensure 'oauth2client_creds.json' is writable or doesn't exist.")
if __name__ == '__main__':
main()
Debug
Known issues
breakingThe `oauth2client` library is officially deprecated as of v4.1.0. No new features will be added, and support is winding down. Continued use is discouraged.fixMigrate your application to use `google-auth` for Google-specific authentication and `oauthlib` for general OAuth 2.0 client needs.
affects: 4.1.0 and later
breakingVersion 4.0.0 dropped support for Python 2.6 and 3.3. It also removed the `oauth2client.contrib.multistore_file` module.fixEnsure your environment uses Python 2.7 or 3.4+. If you were using `multistore_file`, refactor your code to use `oauth2client.contrib.multiprocess_storage` or implement custom storage.
affects: 4.0.0 and later
breakingChanges in `oauth2client.contrib.django_util` and `oauth2client.contrib.django_orm` in v2.2.0 broke compatibility with Django versions below 1.8.fixIf using `oauth2client` with Django, ensure your Django project is running version 1.8 or higher.
affects: 2.2.0 and later (for Django users)
gotchaThe library is tightly coupled with `httplib2`, which has faced periods of limited maintenance. This dependency may introduce security vulnerabilities or compatibility issues with modern HTTP practices.fixPrefer modern OAuth libraries (like `google-auth` or `oauthlib`) that use actively maintained HTTP clients (e.g., `requests`, `httpx`).
affects: All versions
gotchaThe OAuth 2.0 Implicit Flow, which `oauth2client` may facilitate, is now considered deprecated due to inherent security vulnerabilities (e.g., token exposure in URLs, no refresh token support).fixIf your application uses the Implicit Flow, migrate to the more secure Authorization Code Flow with PKCE (Proof Key for Code Exchange).
affects: All versions
gotchaThe `oauth2client` library requires `OAUTH2CLIENT_CLIENT_ID` and `OAUTH2CLIENT_CLIENT_SECRET` (or similar client credentials) to be set as environment variables or configured in the code for authentication and authorization flows to function.fixBefore attempting to use `oauth2client` for any authentication flows, ensure that the necessary client ID and client secret are provided to the application, typically via environment variables or configuration files.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'oauth2client'
The 'oauth2client' library is not installed in the Python environment, or the Python interpreter being used does not have access to the installed package.
fixInstall the library using pip: `pip install oauth2client` or `pip3 install oauth2client` if using Python 3 specifically. For Google App Engine, vendoring the library might be necessary.
invalid_grant
This generic OAuth2 error typically occurs during the token exchange process when the authorization grant (e.g., authorization code or refresh token) is invalid, expired, revoked, or has been used already. Common reasons include expired authorization codes, reusing a one-time authorization code, or a revoked refresh token.
fixRe-authenticate the user to obtain a new authorization code and refresh token. Ensure authorization codes are exchanged for tokens immediately upon receipt and not reused. Check if the refresh token has been revoked or expired. For service accounts, verify the credentials are correct and not compromised.
oauth2client.clientsecrets.InvalidClientSecretsError: File not found: "client_secrets.json"
The application cannot locate the `client_secrets.json` file, which contains the OAuth 2.0 client credentials. This usually means the file is missing, misspelled, or not in the expected directory.
fixEnsure the `client_secrets.json` file is present in the same directory as your script, or provide the correct absolute or relative path to the file when loading credentials. Verify that the file is correctly named and not corrupted.
oauth2client is now deprecated. No more features will be added to the libraries and the core team is turning down support. We recommend you use google-auth and oauthlib.
This is a deprecation warning indicating that the `oauth2client` library is no longer actively maintained or developed. While it might still function, Google recommends migrating to `google-auth` and `oauthlib` for future compatibility, security, and features.
fixMigrate your authentication code to use the `google-auth` library (and `oauthlib` where appropriate). This involves updating imports and authentication flow logic, for example, replacing `oauth2client.service_account.ServiceAccountCredentials` with `google.oauth2.service_account.Credentials.from_service_account_file()`.
Audit
Dependencies
No dependency data recorded yet.