|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import argparse |
| 3 | +import requests |
| 4 | +import warnings |
| 5 | +import sys |
| 6 | +import json |
| 7 | + |
| 8 | +# Redirect warnings to stderr |
| 9 | +warnings.simplefilter('always', category=UserWarning, append=True) |
| 10 | +warnings.showwarning = lambda message, category, filename, lineno, file=None, line=None: \ |
| 11 | + sys.stderr.write(warnings.formatwarning(message, category, filename, lineno, line)) |
| 12 | + |
| 13 | +# Fixed client credentials |
| 14 | +CLIENT_ID = "ui" |
| 15 | +CLIENT_SECRET = "uiman" |
| 16 | + |
| 17 | +# Function to get the access token |
| 18 | +def get_access_token(token_url, username, password): |
| 19 | + response = requests.post( |
| 20 | + token_url, |
| 21 | + headers={"Content-Type": "application/x-www-form-urlencoded"}, |
| 22 | + data={ |
| 23 | + "grant_type": "password", |
| 24 | + "username": username, |
| 25 | + "password": password |
| 26 | + }, |
| 27 | + auth=(CLIENT_ID, CLIENT_SECRET) |
| 28 | + ) |
| 29 | + |
| 30 | + if response.status_code == 200: |
| 31 | + access_token = response.json().get("access_token") |
| 32 | + return access_token, None, None |
| 33 | + else: |
| 34 | + return None, response.status_code, response.json() |
| 35 | + |
| 36 | +# Parse command-line arguments |
| 37 | +def parse_args(): |
| 38 | + parser = argparse.ArgumentParser(description="Retrieve access token from Report Portal.") |
| 39 | + parser.add_argument("--url", required=True, help="Base URL for Report Portal.") |
| 40 | + parser.add_argument("--username", required=True, help="Username for authentication.") |
| 41 | + parser.add_argument("--password", required=True, help="Password for authentication.") |
| 42 | + return parser.parse_args() |
| 43 | + |
| 44 | +def main(): |
| 45 | + args = parse_args() |
| 46 | + |
| 47 | + # Construct the full token URL |
| 48 | + token_url = f"{args.url}/uat/sso/oauth/token" |
| 49 | + |
| 50 | + access_token, status_code, response_json = get_access_token( |
| 51 | + token_url=token_url, |
| 52 | + username=args.username, |
| 53 | + password=args.password |
| 54 | + ) |
| 55 | + |
| 56 | + # Prepare JSON output |
| 57 | + output = {} |
| 58 | + if access_token: |
| 59 | + output["access_token"] = access_token |
| 60 | + output["status"] = "success" |
| 61 | + else: |
| 62 | + output["status"] = "failure" |
| 63 | + output["error_code"] = status_code |
| 64 | + output["error_message"] = response_json |
| 65 | + |
| 66 | + # Print JSON output |
| 67 | + print(json.dumps(output, indent=4)) |
| 68 | + |
| 69 | +if __name__ == "__main__": |
| 70 | + main() |
| 71 | + |
0 commit comments