-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathaigen.py
More file actions
40 lines (36 loc) · 1.5 KB
/
Copy pathaigen.py
File metadata and controls
40 lines (36 loc) · 1.5 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
import requests
import json
api_key = "YOUR_API_KEY"
api_secret = "YOUR_API_SECRET"
def gen_image(oauth):
"""Generate an image using AI"""
url = "https://api.gettyimages.com/v3/ai/image-generations"
headers = {
"Api-Key": oauth["api_key"],
"Authorization": f"Bearer {oauth['access_token']}",
"content-type": "application/json"
}
payload = {
"prompt": "Person hiking in the woods",
"negative_prompt": "bad quality",
"aspect_ratio": "1:1",
"media_type": "photography",
"mood": "warm",
"lens_type": "telephoto",
"depth_of_field": "deep"
}
response = requests.request("POST", url, json=payload, headers=headers, verify=False)
return response
def get_client_credentials_token(key, secret):
"""Get an access token using client credentials grant"""
url = "https://authentication.gettyimages.com/oauth2/token"
payload = f"grant_type=client_credentials&client_id={key}&client_secret={secret}"
headers = {"content-type": "application/x-www-form-urlencoded"}
response = requests.request("POST", url, data=payload, headers=headers, verify=False)
oauth = json.loads(response.content)
return oauth
oauth = get_client_credentials_token(api_key, api_secret)
oauth["api_key"] = api_key
response = gen_image(oauth)
response = json.loads(response.content)
print(response)