function _get_authorization_header
Formats an OAuth token into a standard HTTP Authorization header string by combining the token type and access token.
/tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/runtime/auth/authentication_context.py
24 - 28
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
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function test_sharepoint_token 55.6% similar
-
function test_azure_token 53.2% similar
-
function test_sharepoint_with_token 53.0% similar
-
class TokenResponse 52.6% similar
-
function test_different_scopes 47.2% similar