mirror of
https://gitea.com/PublicAffairs/openai-github-copilot.git
synced 2025-07-23 12:23:08 +02:00
Scripts
This commit is contained in:
13
scripts/Encode-Token.ps1
Normal file
13
scripts/Encode-Token.ps1
Normal file
@@ -0,0 +1,13 @@
|
||||
if($args.Count -lt 1) {
|
||||
Write-Host "Usage: $($MyInvocation.MyCommand.Name) <string to convert>"
|
||||
exit
|
||||
}
|
||||
|
||||
[string]$Text = $args[0]
|
||||
|
||||
$Bytes = [System.Text.Encoding]::UTF8.GetBytes($Text)
|
||||
$Base64 = [System.Convert]::ToBase64String($Bytes)
|
||||
|
||||
$output = "sk-" + $Base64.Replace('=', '')
|
||||
$output|Set-Clipboard
|
||||
Write-Host "Encoded token is: $output"
|
18
scripts/Find-Token.ps1
Normal file
18
scripts/Find-Token.ps1
Normal file
@@ -0,0 +1,18 @@
|
||||
$hostsFile = "$env:LOCALAPPDATA\github-copilot\hosts.json"
|
||||
$tokenFile = "$env:HOMEDRIVE$env:HOMEPATH\.copilot-cli-access-token"
|
||||
|
||||
if (Test-Path $hostsFile) {
|
||||
Write-Output "Found hosts.json:"
|
||||
Get-Content $hostsFile
|
||||
} else {
|
||||
Write-Output "hosts.json not found."
|
||||
}
|
||||
|
||||
Write-Output ""
|
||||
|
||||
if (Test-Path $tokenFile) {
|
||||
Write-Output "Found .copilot-cli-access-token:"
|
||||
Get-Content $tokenFile
|
||||
} else {
|
||||
Write-Output ".copilot-cli-access-token not found."
|
||||
}
|
48
scripts/Get-CopilotToken.ps1
Normal file
48
scripts/Get-CopilotToken.ps1
Normal file
@@ -0,0 +1,48 @@
|
||||
function getToken {
|
||||
$Timeout = 10
|
||||
$ClientId = "Iv1.b507a08c87ecfe98" # GitHub Copilot Plugin by GitHub
|
||||
# get login info
|
||||
$url = "https://github.com/login/device/code"
|
||||
$body = @{
|
||||
"client_id" = $ClientId
|
||||
"scope" = "read:user"
|
||||
}
|
||||
$headers = @{
|
||||
"accept" = "application/json"
|
||||
"content-type" = "application/json"
|
||||
}
|
||||
$loginInfo = Invoke-RestMethod -Uri $url -Method Post -Headers $headers -Body (ConvertTo-Json $body) -TimeoutSec $Timeout
|
||||
|
||||
Write-Host "Please open $($loginInfo.verification_uri) in browser and enter $($loginInfo.user_code) [copied in clipboard] to login."
|
||||
$loginInfo.user_code | Set-Clipboard
|
||||
Start-Sleep -Seconds 3
|
||||
Start-Process $loginInfo.verification_uri
|
||||
|
||||
$url = "https://github.com/login/oauth/access_token"
|
||||
$body = @{
|
||||
"client_id" = $ClientId
|
||||
"device_code" = $loginInfo.device_code
|
||||
"grant_type" = "urn:ietf:params:oauth:grant-type:device_code"
|
||||
}
|
||||
while ($true) { # poll auth
|
||||
$data = Invoke-RestMethod -Uri $url -Method Post -Headers $headers -Body (ConvertTo-Json $body) -TimeoutSec $Timeout
|
||||
if ($data.access_token) {
|
||||
return $data.access_token
|
||||
} elseif ($data.error -eq "authorization_pending") {
|
||||
Start-Sleep -Seconds $loginInfo.interval
|
||||
} else {
|
||||
throw $data.error_description
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
$token = getToken
|
||||
}
|
||||
catch {
|
||||
Write-Host $_.Exception.GetType().Name $_.Exception.Message
|
||||
return
|
||||
}
|
||||
Write-Host "Your token is [copied to clipboard]:"
|
||||
Write-Host $token
|
||||
$token | Set-Clipboard
|
58
scripts/README.md
Normal file
58
scripts/README.md
Normal file
@@ -0,0 +1,58 @@
|
||||
To use the `openai-github-copilot` service, you need a GitHub Copilot-enabled _access token_. This is a string that starts with either `ghu_` or `gho_`.
|
||||
|
||||
This directory contains scripts that assist you in locating the _access token_ on your system (_if you've previously used the official Copilot plugin or CLI_), or generating a new one.
|
||||
|
||||
> [!WARNING]
|
||||
> You still need an active Copilot subscription.
|
||||
|
||||
For your convenience, we've listed command lines below that you can execute immediately, without the need to download any script explicitly.
|
||||
|
||||
Of course, you can also execute these scripts locally.
|
||||
|
||||
While the scripts are straightforward and you can review their source code yourself, their use is entirely optional. You can achieve the same results without using these third-party scripts:
|
||||
|
||||
<details>
|
||||
|
||||
- If you have previously used Copilot in _VSCode_ or _JetBrains_, you should check the `hosts.json` file, which is located at
|
||||
`~/.config/github-copilot/hosts.json` _(for Linux)_, or
|
||||
`%LOCALAPPDATA%\github-copilot\hosts.json` _(for Windows)_.
|
||||
- If you have previously used Copilot through the [github-copilot-cli](https://githubnext.com/projects/copilot-cli),
|
||||
you should check a different file:
|
||||
`~/.copilot-cli-access-token` _(for Linux)_, or `%HOMEDRIVE%HOMEPATH%\.copilot-cli-access-token` _(for Windows)_.
|
||||
|
||||
If not, then follow next steps:
|
||||
- Install it using `npm i -g @githubnext/github-copilot-cli` (you need [Node.js](https://nodejs.org/en/download/) for this).
|
||||
- Run `github-copilot-cli auth` and follow the instructions.
|
||||
Ignore the _"Failed to authenticate.."_ message, it's not relevant here.
|
||||
- Retrieve your token from `copilot-cli-access-token`, as described above.
|
||||
</details>
|
||||
|
||||
|
||||
|
||||
## Find existing Github Copilot access token
|
||||
|
||||
* Bash
|
||||
```sh
|
||||
curl -LsSf https://gitea.com/PublicAffairs/openai-github-copilot/raw/main/scripts/find_token.sh | sh
|
||||
```
|
||||
* Powershell
|
||||
```sh
|
||||
powershell -c "irm https://gitea.com/PublicAffairs/openai-github-copilot/raw/main/scripts/Find-Token.ps1 | iex"
|
||||
```
|
||||
|
||||
## Generate Github Copilot access token
|
||||
|
||||
* Bash
|
||||
```sh
|
||||
curl -LsSf https://gitea.com/PublicAffairs/openai-github-copilot/raw/main/scripts/get_copilot_token.sh | sh
|
||||
```
|
||||
* Python
|
||||
```sh
|
||||
curl -LsSf https://gitea.com/PublicAffairs/openai-github-copilot/raw/main/scripts/get_copilot_token.py | python
|
||||
```
|
||||
* Powershell
|
||||
```sh
|
||||
powershell -c "irm https://gitea.com/PublicAffairs/openai-github-copilot/raw/main/scripts/Get-CopilotToken.ps1 | iex"
|
||||
```
|
||||
|
||||
[jq]: https://jqlang.github.io/jq/
|
23
scripts/find_token.cmd
Normal file
23
scripts/find_token.cmd
Normal file
@@ -0,0 +1,23 @@
|
||||
@echo off
|
||||
setlocal
|
||||
|
||||
set "HOSTS_FILE=%LOCALAPPDATA%\github-copilot\hosts.json"
|
||||
set "TOKEN_FILE=%HOMEDRIVE%%HOMEPATH%\.copilot-cli-access-token"
|
||||
|
||||
if exist "%HOSTS_FILE%" (
|
||||
echo Found hosts.json:
|
||||
type "%HOSTS_FILE%"
|
||||
) else (
|
||||
echo hosts.json not found.
|
||||
)
|
||||
|
||||
echo.
|
||||
|
||||
if exist "%TOKEN_FILE%" (
|
||||
echo Found .copilot-cli-access-token:
|
||||
type "%TOKEN_FILE%"
|
||||
) else (
|
||||
echo .copilot-cli-access-token not found.
|
||||
)
|
||||
|
||||
endlocal
|
20
scripts/find_token.sh
Normal file
20
scripts/find_token.sh
Normal file
@@ -0,0 +1,20 @@
|
||||
#!/bin/bash
|
||||
|
||||
host_file="$HOME/.config/github-copilot/hosts.json"
|
||||
token_file="$HOME/.copilot-cli-access-token"
|
||||
|
||||
if [ -f "$host_file" ]; then
|
||||
echo "Found hosts.json:"
|
||||
cat "$host_file"
|
||||
else
|
||||
echo "hosts.json not found."
|
||||
fi
|
||||
|
||||
echo
|
||||
|
||||
if [ -f "$token_file" ]; then
|
||||
echo "Found .copilot-cli-access-token:"
|
||||
cat "$token_file"
|
||||
else
|
||||
echo ".copilot-cli-access-token not found."
|
||||
fi
|
53
scripts/get_copilot_token.py
Normal file
53
scripts/get_copilot_token.py
Normal file
@@ -0,0 +1,53 @@
|
||||
import http.client
|
||||
import json
|
||||
import time
|
||||
|
||||
TIMEOUT = 10
|
||||
CLIENT_ID = "Iv1.b507a08c87ecfe98" # GitHub Copilot Plugin by GitHub
|
||||
|
||||
|
||||
def get_token():
|
||||
# get login info
|
||||
conn = http.client.HTTPSConnection("github.com", timeout=TIMEOUT)
|
||||
headers = {
|
||||
"accept": "application/json",
|
||||
"content-type": "application/json"
|
||||
}
|
||||
body = {
|
||||
"client_id": CLIENT_ID,
|
||||
"scope": "read:user"
|
||||
}
|
||||
conn.request("POST", "/login/device/code", json.dumps(body), headers)
|
||||
resp = conn.getresponse()
|
||||
if resp.status != 200:
|
||||
raise Exception(f"Request failed with status {resp.status}: {resp.reason}")
|
||||
login_info = json.loads(resp.read().decode())
|
||||
|
||||
print(f"Please open {login_info['verification_uri']} in browser and enter {login_info['user_code']} to login.")
|
||||
|
||||
body = {
|
||||
"client_id": CLIENT_ID,
|
||||
"device_code": login_info['device_code'],
|
||||
"grant_type": "urn:ietf:params:oauth:grant-type:device_code"
|
||||
}
|
||||
while True: # poll auth
|
||||
conn.request("POST", "/login/oauth/access_token", json.dumps(body), headers)
|
||||
resp = conn.getresponse()
|
||||
if resp.status != 200:
|
||||
raise Exception(f"Request failed with status {resp.status}: {resp.reason}")
|
||||
data = json.loads(resp.read().decode())
|
||||
if 'access_token' in resp:
|
||||
return data['access_token']
|
||||
elif data.get('error') == "authorization_pending":
|
||||
time.sleep(login_info['interval'])
|
||||
else:
|
||||
raise Exception(data['error_description'])
|
||||
|
||||
|
||||
try:
|
||||
token = get_token()
|
||||
except Exception as e:
|
||||
print(type(e).__name__, e)
|
||||
else:
|
||||
print("Your token is:")
|
||||
print(token)
|
50
scripts/get_copilot_token.sh
Normal file
50
scripts/get_copilot_token.sh
Normal file
@@ -0,0 +1,50 @@
|
||||
#!/bin/bash
|
||||
|
||||
CLIENT_ID='Iv1.b507a08c87ecfe98' # GitHub Copilot Plugin by GitHub
|
||||
|
||||
if command -v jq >/dev/null 2>&1; then
|
||||
json_value() { #https://jqlang.github.io/jq/
|
||||
echo $1 | jq -r .$2
|
||||
}
|
||||
else
|
||||
json_value() {
|
||||
echo $1 | grep -o "\"$2\":[^,]*" | cut -d ':' -f2 | tr -d '" ' | tr -d '}'
|
||||
}
|
||||
fi
|
||||
|
||||
# get login info
|
||||
if ! login_info=$(curl -LsSf -X POST \
|
||||
-H 'accept: application/json' \
|
||||
-H 'content-type: application/json' \
|
||||
-d '{"client_id": "'"$CLIENT_ID"'", "scope": "read:user"}' \
|
||||
'https://github.com/login/device/code')
|
||||
then exit 1; fi
|
||||
|
||||
verification_uri=$(json_value "$login_info" verification_uri)
|
||||
user_code=$( json_value "$login_info" user_code)
|
||||
device_code=$( json_value "$login_info" device_code)
|
||||
interval=$( json_value "$login_info" interval)
|
||||
|
||||
echo "Please open $verification_uri in browser and enter $user_code to login"
|
||||
|
||||
# poll auth
|
||||
while true;do
|
||||
if ! data=$(curl -LsSf -X POST \
|
||||
-H 'accept: application/json' \
|
||||
-H 'content-type: application/json' \
|
||||
-d '{"client_id": "'"$CLIENT_ID"'", "device_code": "'"$device_code"'", "grant_type": "urn:ietf:params:oauth:grant-type:device_code"}' \
|
||||
'https://github.com/login/oauth/access_token')
|
||||
then exit 1; fi
|
||||
|
||||
access_token=$(json_value "$data" access_token)
|
||||
if [ "$access_token" != "null" ] && [ "$access_token" != "" ]; then
|
||||
echo 'Your token is:'
|
||||
echo $access_token
|
||||
break
|
||||
elif [ "$(json_value "$data" error)" = "authorization_pending" ]; then
|
||||
sleep $interval
|
||||
else
|
||||
echo "Error: $(json_value "$data" error_description)"
|
||||
break
|
||||
fi
|
||||
done
|
Reference in New Issue
Block a user