Vonage Network Auth
The `vonage-network-auth` library (version 1.0.2) provides Python tools for interacting with Vonage Network APIs that require OAuth2 for authentication. It focuses on simplifying token acquisition and refresh, allowing developers to securely access network-specific services. Released in November 2023, its release cadence is independent but generally slower than the main Vonage Python SDK.
Common errors
-
ModuleNotFoundError: No module named 'vonage_network_auth'
cause The `vonage-network-auth` package is not installed in your Python environment or is not accessible.fixRun `pip install vonage-network-auth` in your terminal to install the library. -
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: .../oauth2/token
cause This error most commonly indicates that the `client_id`, `client_secret`, or the requested `scope` parameter passed to `get_access_token()` is invalid or incorrect.fixDouble-check your Vonage Client ID and Client Secret for typos or invalid values. Verify that the requested `scope` is valid and authorized for your application's credentials. -
jwt.exceptions.ExpiredSignatureError: Signature has expired
cause While not directly thrown by `vonage-network-auth`, this is a common error encountered when attempting to use an access token (obtained via this library) after its `expires_in` duration has passed.fixImplement robust token management by checking the `expires_in` value returned with the access token. Prioritize refreshing the token using `client.refresh_access_token()` before making any new API requests if the token is expired or close to expiry.
Warnings
- gotcha The `vonage-network-auth` library is a standalone package and not directly integrated into the main `vonage-python-sdk` client object. You cannot access its functionality via `vonage.Client`.
- gotcha OAuth2 access tokens are typically short-lived. Always implement logic to check token expiry (using the `expires_in` field in the response) and refresh tokens as needed to maintain session validity without requiring user re-authentication.
- breaking Incorrect or missing OAuth2 scopes in the `get_access_token` call will result in `InvalidScope` errors or insufficient permissions for subsequent API calls, even if authentication succeeds.
Install
-
pip install vonage-network-auth
Imports
- Client
from vonage import NetworkAuthClient
from vonage_network_auth.client import Client
Quickstart
import os
from vonage_network_auth.client import Client
# Ensure these are set as environment variables or replace with actual credentials
client_id = os.environ.get("VONAGE_CLIENT_ID", "YOUR_CLIENT_ID")
client_secret = os.environ.get("VONAGE_CLIENT_SECRET", "YOUR_CLIENT_SECRET")
scope = os.environ.get("VONAGE_SCOPE", "network:quality_of_service") # Example scope
if not client_id or not client_secret or client_id == "YOUR_CLIENT_ID":
print("Error: VONAGE_CLIENT_ID and VONAGE_CLIENT_SECRET must be set.")
print("Please set environment variables or replace placeholders.")
else:
try:
client = Client(client_id, client_secret)
# Request an access token for the specified scope
token_response = client.get_access_token(scope)
access_token = token_response["access_token"]
expires_in = token_response["expires_in"]
print(f"Access Token obtained successfully. Starts with: {access_token[:10]}...")
print(f"Token expires in: {expires_in} seconds")
# If the API provides a refresh token (not all do, depending on scope/grant type)
# refresh_token = token_response.get("refresh_token")
# if refresh_token:
# print("Refresh Token also obtained.")
# # Example of refreshing the token:
# # refreshed_token_response = client.refresh_access_token(refresh_token, scope)
# # print(f"Refreshed Access Token: {refreshed_token_response['access_token'][:10]}...")
except Exception as e:
print(f"An error occurred: {e}")
# In a production application, handle specific authentication errors (e.g., InvalidGrant).