Vonage Network Auth

1.0.2 · active · verified Thu Apr 16

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

Warnings

Install

Imports

Quickstart

This example demonstrates how to initialize the `Client` with your Vonage OAuth2 credentials (Client ID and Client Secret) and retrieve an access token for a specified scope. Remember to replace placeholder values or set them as environment variables (VONAGE_CLIENT_ID, VONAGE_CLIENT_SECRET, VONAGE_SCOPE) for production use.

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).

view raw JSON →