forked from sns-sdks/python-youtube
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subscription.py
41 lines (27 loc) · 1.01 KB
/
subscription.py
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
"""
This demo show how to use this library to do authorization and get your subscription.
"""
import pyyoutube
import webbrowser
CLIENT_ID = "your app id"
CLIENT_SECRET = "your app secret"
def get_subscriptions():
api = pyyoutube.Api(client_id=CLIENT_ID, client_secret=CLIENT_SECRET)
# need follows scope
scope = ["https://www.googleapis.com/auth/youtube.readonly"]
url, _ = api.get_authorization_url(scope=scope)
print(
"Try to start a browser to visit the authorization page. If not opened. you can copy and visit by hand:\n"
f"{url}"
)
webbrowser.open(url)
auth_response = input(
"\nCopy the whole url if you finished the step to authorize:\n"
)
api.generate_access_token(authorization_response=auth_response)
sub_res = api.get_subscription_by_me(mine=True, parts="id,snippet", count=None)
with open("subscriptions.json", "w+") as f:
f.write(sub_res.to_json())
print("Finished.")
if __name__ == "__main__":
get_subscriptions()