🔍 Code Extractor

function _get_authorization_header

Maturity: 20

Formats an OAuth token into a standard HTTP Authorization header string by combining the token type and access token.

File:
/tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/runtime/auth/authentication_context.py
Lines:
24 - 28
Complexity:
simple

Purpose

This utility function constructs a properly formatted Authorization header value for HTTP requests that require OAuth authentication. It takes a token object containing tokenType and accessToken attributes and returns a string in the format '{token_type} {access_token}' (e.g., 'Bearer abc123...'). This is commonly used in Office 365 and SharePoint API authentication workflows.

Source Code

def _get_authorization_header(token):
    # type: (Any) -> str
    return "{token_type} {access_token}".format(
        token_type=token.tokenType, access_token=token.accessToken
    )

Parameters

Name Type Default Kind
token - - positional_or_keyword

Parameter Details

token: A token object (likely TokenResponse or similar) that must have two attributes: 'tokenType' (string, typically 'Bearer') and 'accessToken' (string containing the OAuth access token). The object represents an authentication token obtained from an OAuth provider.

Return Value

Returns a string formatted as '{token_type} {access_token}', which is ready to be used as the value for an HTTP Authorization header. For example, if token.tokenType is 'Bearer' and token.accessToken is 'eyJ0eXAi...', it returns 'Bearer eyJ0eXAi...'.

Usage Example

# Assuming you have a token object from an OAuth flow
from office365.runtime.auth.token_response import TokenResponse

# Create a token object (normally obtained from authentication)
token = TokenResponse()
token.tokenType = 'Bearer'
token.accessToken = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6...'

# Generate the authorization header
auth_header = _get_authorization_header(token)
print(auth_header)
# Output: 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6...'

# Use in an HTTP request
import requests
headers = {'Authorization': auth_header}
response = requests.get('https://api.example.com/resource', headers=headers)

Best Practices

  • Ensure the token object passed has both 'tokenType' and 'accessToken' attributes to avoid AttributeError
  • This function does not validate the token format or expiration - validation should be done before calling this function
  • The returned string should be used as the value for the 'Authorization' HTTP header
  • Token objects should be obtained from secure authentication flows and handled carefully to prevent exposure
  • This is typically an internal helper function and may be prefixed with underscore to indicate private use within a module

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function test_sharepoint_token 55.6% similar

    Tests SharePoint OAuth2 authentication by acquiring an access token using client credentials flow and validates it with a SharePoint API call.

    From: /tf/active/vicechatdev/SPFCsync/diagnose_sharepoint.py
  • function test_azure_token 53.2% similar

    Tests Azure AD authentication by attempting to acquire an OAuth2 access token using client credentials flow for Microsoft Graph API access.

    From: /tf/active/vicechatdev/SPFCsync/diagnose_sharepoint.py
  • function test_sharepoint_with_token 53.0% similar

    Tests SharePoint REST API connectivity and authentication by making a GET request to retrieve site information using a provided access token.

    From: /tf/active/vicechatdev/SPFCsync/diagnose_permissions.py
  • class TokenResponse 52.6% similar

    A data class that represents an OAuth-style token response, storing access token information and validating token format.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/runtime/auth/token_response.py
  • function test_different_scopes 47.2% similar

    Tests OAuth2 authentication with different permission scopes for SharePoint and Microsoft Graph APIs, attempting to acquire access tokens and validate them against a SharePoint site.

    From: /tf/active/vicechatdev/SPFCsync/diagnose_permissions.py
← Back to Browse