-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathauthenticate.py
71 lines (58 loc) · 1.92 KB
/
authenticate.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
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
68
69
70
71
# Copyright (c) 2024-2025 Datalayer, Inc.
#
# MIT License
import requests
import time
GITHUB_CLIENT_ID = "Iv1.b507a08c87ecfe98"
HEADERS = {
'accept': 'application/json',
'editor-version': 'Neovim/0.6.1',
'editor-plugin-version': 'copilot.vim/1.16.0',
'content-type': 'application/json',
'user-agent': 'GitHubCopilot/1.155.0',
'accept-encoding': 'gzip,deflate,br'
}
def authenticate():
print()
access_token = get_access_token()
token = get_token(access_token)
with open('.env', 'a') as f:
f.write(f'\nGITHUB_TOKEN="{token}"\n')
print()
print('Successfull authentication with GitHub!')
def get_token(access_token):
response = requests.get(
'https://api.github.com/copilot_internal/v2/token',
headers={**HEADERS, 'authorization': f'token {access_token}'}
)
response.raise_for_status()
return response.json().get('token')
def get_access_token():
response = requests.post(
'https://github.com/login/device/code',
headers=HEADERS,
json={'client_id': GITHUB_CLIENT_ID, 'scope': 'read:user'}
)
response.raise_for_status()
data = response.json()
device_code = data.get('device_code')
user_code = data.get('user_code')
verification_uri = data.get('verification_uri')
print(f'Please visit {verification_uri} and enter code {user_code} to authenticate.')
while True:
time.sleep(5)
response = requests.post(
'https://github.com/login/oauth/access_token',
headers=HEADERS,
json={
'client_id': GITHUB_CLIENT_ID,
'device_code': device_code,
'grant_type': 'urn:ietf:params:oauth:grant-type:device_code'
}
)
response.raise_for_status()
access_token = response.json().get('access_token')
if access_token:
return access_token
if __name__ == '__main__':
authenticate()