-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrefresh_token_example.py
More file actions
67 lines (52 loc) · 1.95 KB
/
Copy pathrefresh_token_example.py
File metadata and controls
67 lines (52 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import argparse
import requests
import json
def refresh_access_token(
refresh_token: str,
client_id: str,
client_secret: str,
token_url: str = "https://api.snyk.io/oauth2/token",
) -> dict[str, str]:
"""
Refreshes the access token using the provided refresh token.
Args:
refresh_token (str): The refresh token.
client_id (str): The client ID for the Snyk app.
client_secret (str): The client secret for the Snyk app.
token_url (str): The token endpoint URL.
Returns:
dict: The response containing the new access_token, refresh_token, etc.
"""
data: dict = {
"grant_type": "refresh_token",
"refresh_token": refresh_token,
"client_id": client_id,
"client_secret": client_secret,
}
headers: dict = {"Content-Type": "application/x-www-form-urlencoded"}
response: requests.Response = requests.post(token_url, data=data, headers=headers)
response.raise_for_status()
token_data = response.json()
return token_data
def main():
parser: argparse.ArgumentParser = argparse.ArgumentParser(
description="Refresh a Snyk OAuth2 access token using a refresh token."
)
parser.add_argument("--refresh-token", required=True, help="Refresh token")
parser.add_argument("--client-id", required=True, help="Snyk App client ID")
parser.add_argument("--client-secret", required=True, help="Snyk App client secret")
parser.add_argument(
"--token-url",
default="https://api.snyk.io/oauth2/token",
help="Token endpoint URL (default: https://api.snyk.io/oauth2/token)",
)
args: argparse.Namespace = parser.parse_args()
token_data: dict[str, str] = refresh_access_token(
refresh_token=args.refresh_token,
client_id=args.client_id,
client_secret=args.client_secret,
token_url=args.token_url,
)
print(json.dumps(token_data, indent=2))
if __name__ == "__main__":
main()