返回提交历史
Modified
g4f/Provider/github/GithubCopilot.py
+10
-7
Modified
g4f/api/__init__.py
+2
-0
XFEstudio/gpt4free
Refactor authorization header handling in update_headers to remove it when no new API key is provided
eb9a5523
代码差异
2 个文件
+12
-7
@@ -126,13 +126,13 @@ class GithubCopilot(OpenaiTemplate):
126
126
Otherwise, OAuth credentials will be used.
127
127
"""
128
128
# If no API key provided, use OAuth token
129
if api_key is None:
129
if not api_key:
130
130
try:
131
131
token_provider = cls._get_token_provider()
132
132
creds = await token_provider.get_valid_token()
133
133
api_key = creds.get("token")
134
134
if not api_key:
135
raise RuntimeError(
135
raise MissingAuthError(
136
136
"GitHub Copilot OAuth not configured. "
137
137
"Please run 'g4f auth github-copilot' to authenticate."
138
138
)
@@ -140,7 +140,7 @@ class GithubCopilot(OpenaiTemplate):
140
140
base_url = creds.get("endpoint", cls.base_url)
141
141
except TokenManagerError as e:
142
142
if "login" in str(e).lower() or "credentials" in str(e).lower():
143
raise RuntimeError(
143
raise MissingAuthError(
144
144
"GitHub Copilot OAuth not configured. "
145
145
"Please run 'g4f auth github-copilot' to authenticate."
146
146
) from e
@@ -159,7 +159,7 @@ class GithubCopilot(OpenaiTemplate):
159
159
@classmethod
160
160
def get_models(cls, api_key: Optional[str] = None, base_url: Optional[str] = None, timeout: Optional[int] = None):
161
161
# If no API key provided, use OAuth token
162
if api_key is None:
162
if not api_key:
163
163
try:
164
164
token_provider = cls._get_token_provider()
165
165
get_running_loop(check_nested=True)
@@ -169,7 +169,7 @@ class GithubCopilot(OpenaiTemplate):
169
169
base_url = creds.get("endpoint", cls.base_url)
170
170
except TokenManagerError as e:
171
171
if "login" in str(e).lower() or "credentials" in str(e).lower():
172
raise RuntimeError(
172
raise MissingAuthError(
173
173
"GitHub Copilot OAuth not configured. "
174
174
"Please run 'g4f auth github-copilot' to authenticate."
175
175
) from e
@@ -249,12 +249,15 @@ class GithubCopilot(OpenaiTemplate):
249
249
client = GithubOAuth2Client()
250
250
github_creds = await client.sharedManager.getValidCredentials(client)
251
251
if not github_creds or not github_creds.get("access_token"):
252
raise MissingAuthError("No GitHub OAuth token available. Please login first.")
252
raise MissingAuthError(
253
"GitHub Copilot OAuth not configured. "
254
"Please run 'g4f auth github-copilot' to authenticate."
255
)
253
256
254
257
github_token = github_creds["access_token"]
255
258
url = f"https://api.github.com/copilot_internal/user"
256
259
headers = {
257
"Accept": "application/json",
260
"accept": "application/json",
258
261
"authorization": f"token {github_token}",
259
262
"editor-version": EDITOR_VERSION,
260
263
"editor-plugin-version": EDITOR_PLUGIN_VERSION,
@@ -180,6 +180,8 @@ def update_headers(request: Request, new_api_key: str = None, user: str = None)
180
180
new_headers = request.headers.mutablecopy()
181
181
if new_api_key:
182
182
new_headers["authorization"] = f"Bearer {new_api_key}"
183
else:
184
new_headers.pop("authorization", None)
183
185
if user:
184
186
new_headers["x-user"] = user
185
187
request.scope["headers"] = new_headers.raw