Python获取ghu

首先下文的一切都是对下面大佬的模仿,我仅作总结,如侵权请联系删除

@yep

1.在python坏境下运行下面代码

import requests
import time
import json

# Step 1: 获取device_code和user_code
data = {
    "client_id": "Iv1.b507a08c87ecfe98",
    "scope": "read:user"
}
response = requests.post('https://github.com/login/device/code', headers={'Accept': 'application/json', 'Content-Type': 'application/json'}, data=json.dumps(data))

result = response.json()
device_code = result['device_code']
user_code = result['user_code']

print("Your user code is: ", user_code)
print("Please go to https://github.com/login/device and enter this user code.")

# Step 2: 登录github验证
# 这一步需要用户手动执行

# Step 3: 查询获取访问令牌
data = {
    "client_id": "Iv1.b507a08c87ecfe98",
    "device_code": device_code,
    "grant_type": "urn:ietf:params:oauth:grant-type:device_code"
}

# 由于可能需要一些时间才能验证成功,所以这里使用一个循环来定期检查
while True:
    response = requests.post('https://github.com/login/oauth/access_token', headers={'Accept': 'application/json', 'Content-Type': 'application/json'}, data=json.dumps(data))
    result = response.json()
    if 'access_token' in result:
        print("Your access token is: ", result['access_token'])
        break
    elif 'error' in result:
        if result['error'] == 'authorization_pending':
            print("Authorization still pending. Will check again in 10s.")
            time.sleep(10)
        else:
            print("Error: ", result['error'])
            break
    else:
        print("Unexpected response: ", result)
        break

2.手动打开

https://github.com/login/device

该网址,填写控制台打印出的验证码

3.获取ghu,如下图

4.觉得有用,给作者一个小小的赞支持一下

34 个赞

我想发个 PHP 版本的,结果报 403 ,不知道触发什么了

3 个赞

感觉python写这种小脚本,很delicate

4 个赞

试一下优化后的:

代码
import requests
import time
import webbrowser


def get_device_code_and_user_code(client_id, scope):
    url = "https://github.com/login/device/code"
    payload = {"client_id": client_id, "scope": scope}
    headers = {"Accept": "application/json", "Content-Type": "application/json"}
    response = requests.post(url, json=payload, headers=headers)
    response.raise_for_status()  # 确保请求成功
    return response.json()


def poll_for_access_token(client_id, device_code):
    url = "https://github.com/login/oauth/access_token"
    payload = {
        "client_id": client_id,
        "device_code": device_code,
        "grant_type": "urn:ietf:params:oauth:grant-type:device_code",
    }
    headers = {"Accept": "application/json", "Content-Type": "application/json"}
    while True:
        response = requests.post(url, json=payload, headers=headers)
        response_data = response.json()
        if "access_token" in response_data:
            return response_data["access_token"]
        if "error" in response_data:
            error = response_data["error"]
            if error == "authorization_pending":
                time.sleep(response_data.get("interval", 5))
            elif error == "slow_down":
                time.sleep(response_data.get("interval", 5) + 5)
            else:
                raise Exception(f"Error: {error}")
        else:
            raise Exception("Unexpected response from GitHub.")


# Step 1: 获取device_code和user_code
client_id = "Iv1.b507a08c87ecfe98"
scope = "read:user"
device_code_data = get_device_code_and_user_code(client_id, scope)
user_code = device_code_data["user_code"]
device_code = device_code_data["device_code"]
verification_uri = device_code_data["verification_uri"]
interval = device_code_data["interval"]

print(f"Your user code is: {user_code}")
print(f"Please go to {verification_uri} and enter this user code.")

# Step 2: 自动打开浏览器到GitHub验证页面
webbrowser.open(verification_uri)
input("Press Enter to continue...")
# Step 3: 查询获取访问令牌
try:
    access_token = poll_for_access_token(client_id, device_code)
    print("Your access token is:", access_token)
except Exception as e:
    print(f"An error occurred: {e}")

10 个赞

自动打开太智能了,我还不知道xdm,C了多少个号在别的浏览器 :grin:

3 个赞

可以调用自动化库,把step2那一步,自动登陆,自动填入code,然后确认。

2 个赞

你要发个PHP的,始皇得给你移动到精华神贴

3 个赞

网页版本呢

3 个赞

你等通知吧

2 个赞

啊,对对对

2 个赞

人均大佬,没假了

3 个赞

学习一下~

3 个赞

大神真多

3 个赞

我很早前写了go 带web 版本,有需要的可以丢出来

更新:go版本代码

2 个赞

给大佬点个赞,代码收下了,谢谢:heart:

4 个赞

点赞,马克

1 个赞

666666

2 个赞

学习了

1 个赞

大神多 学习了 有各种方法实现的大神 都分享一下code呀

1 个赞

真大神