Skip to content

oAuth2 - Client Credentials

oauth2-client-credentials.py


#!/usr/bin/python3
import json
import requests
import urllib3

urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)


def get_token():
    data = {
        'grant_type': 'client_credentials',
        'client_id': '{CLIENT_ID}',
        'client_secret': open("{FILE_WITH_CLIENT_SECRET}").readline().rstrip(),
        'scope': 'openid read write'
    }

    headers = {
        "Content-Type": "application/x-www-form-urlencoded"
    }

    response = requests.post(
        'https://sso.corp.com/oauth2/token',
        data=data,
        headers=headers,
        verify=False
    )

    return json.loads(response.content.decode('utf8'))


print(get_token()['access_token'])