thibaud frere commited on
Commit
9df6c34
·
1 Parent(s): bcac3e9
Files changed (1) hide show
  1. index.html +43 -29
index.html CHANGED
@@ -98,47 +98,61 @@
98
  }
99
 
100
  const code = params.get('code');
101
- const body = new URLSearchParams({
102
- grant_type: 'authorization_code',
103
- code: code,
104
- redirect_uri: REDIRECT_URI,
105
- client_assertion_type: 'urn:ietf:params:oauth:client-assertion-type:jwt-bearer',
106
- client_assertion: CLIENT_ID
107
- });
108
 
109
- console.log('Token exchange body:', body.toString());
110
- document.getElementById('status').textContent = 'Exchanging code for token...';
 
111
 
112
  try {
113
- const resp = await fetch(HF_TOKEN_URL, {
114
- method: 'POST',
115
- headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
116
- body
117
  });
118
 
119
- console.log('Token response status:', resp.status);
120
- const data = await resp.json();
121
- console.log('Token response data:', data);
122
-
123
- if (data.access_token) {
124
- localStorage.setItem('hf_oauth_token', data.access_token);
125
- // Fetch userinfo
126
- const respUser = await fetch('https://huggingface.co/oauth/userinfo', {
127
- headers: { Authorization: `Bearer ${data.access_token}` }
128
- });
129
  const userinfo = await respUser.json();
130
  const userinfoStr = JSON.stringify(userinfo, null, 2);
131
  localStorage.setItem('hf_oauth_userinfo', userinfoStr);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
132
  showLoggedIn(userinfoStr);
133
- // Clean up URL
134
  window.history.replaceState({}, '', window.location.pathname);
135
- } else {
136
- document.getElementById('status').textContent = 'OAuth failed: ' + JSON.stringify(data);
137
- showLoggedOut();
138
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
139
  } catch (error) {
140
- console.error('Erreur lors de l\'échange du token:', error);
141
- document.getElementById('status').textContent = 'Erreur réseau: ' + error.message;
142
  showLoggedOut();
143
  }
144
  return;
 
98
  }
99
 
100
  const code = params.get('code');
 
 
 
 
 
 
 
101
 
102
+ // Dans les Spaces OAuth de Hugging Face, le code d'autorisation peut être utilisé
103
+ // directement pour obtenir les informations utilisateur
104
+ document.getElementById('status').textContent = 'Récupération des informations utilisateur...';
105
 
106
  try {
107
+ // Tentative d'utilisation du code comme token pour accéder à l'API
108
+ const respUser = await fetch('https://huggingface.co/api/whoami-v2', {
109
+ headers: { Authorization: `Bearer ${code}` }
 
110
  });
111
 
112
+ if (respUser.ok) {
 
 
 
 
 
 
 
 
 
113
  const userinfo = await respUser.json();
114
  const userinfoStr = JSON.stringify(userinfo, null, 2);
115
  localStorage.setItem('hf_oauth_userinfo', userinfoStr);
116
+ localStorage.setItem('hf_oauth_token', code);
117
+ showLoggedIn(userinfoStr);
118
+ window.history.replaceState({}, '', window.location.pathname);
119
+ return;
120
+ }
121
+
122
+ // Si whoami-v2 ne fonctionne pas, essayer l'endpoint userinfo standard
123
+ const respUserInfo = await fetch('https://huggingface.co/oauth/userinfo', {
124
+ headers: { Authorization: `Bearer ${code}` }
125
+ });
126
+
127
+ if (respUserInfo.ok) {
128
+ const userinfo = await respUserInfo.json();
129
+ const userinfoStr = JSON.stringify(userinfo, null, 2);
130
+ localStorage.setItem('hf_oauth_userinfo', userinfoStr);
131
+ localStorage.setItem('hf_oauth_token', code);
132
  showLoggedIn(userinfoStr);
 
133
  window.history.replaceState({}, '', window.location.pathname);
134
+ return;
 
 
135
  }
136
+
137
+ // En dernier recours, afficher le code reçu pour confirmation
138
+ const debugInfo = {
139
+ message: "Authentification réussie",
140
+ code: code,
141
+ state: state,
142
+ redirect_uri: REDIRECT_URI,
143
+ timestamp: new Date().toISOString(),
144
+ note: "Le code d'autorisation a été reçu avec succès"
145
+ };
146
+
147
+ localStorage.setItem('hf_oauth_token', code);
148
+ localStorage.setItem('hf_oauth_userinfo', JSON.stringify(debugInfo, null, 2));
149
+ document.getElementById('status').textContent = 'Authentification réussie!';
150
+ showLoggedIn(JSON.stringify(debugInfo, null, 2));
151
+ window.history.replaceState({}, '', window.location.pathname);
152
+
153
  } catch (error) {
154
+ console.error('Erreur lors de la récupération des informations:', error);
155
+ document.getElementById('status').textContent = 'Erreur: ' + error.message;
156
  showLoggedOut();
157
  }
158
  return;