我正在尝试根据文档通过 HuggingFace.co 构建身份验证解决方案:https://huggingface.co/docs/hub/oauth。我已经完成了以下操作,但不确定我犯了什么错误。请帮我打印电子邮件和个人资料详细信息。蒂亚!
注意:我已在开发者应用程序中设置了 URL (http://localhost/ai/) 和回调 (http://localhost/ai/hf):
我有以下代码:
$clientId = '37779938-b...';
$clientSecret = '098260e6-1...';
$redirectUri = 'http://localhost/ai/hf';
// Step 1: Redirect user to Hugging Face's authorization page with desired scopes
$authorizationUrl = 'https://huggingface.co/oauth/authorize';
$authorizationUrl .= '?client_id=' . $clientId;
$authorizationUrl .= '&redirect_uri=' . $redirectUri;
$authorizationUrl .= '&response_type=code';
$authorizationUrl .= '&scope=openid%20profile%20email'; // Adjust the scope as needed
$authorizationUrl .= '&state=' . bin2hex(random_bytes(16));
header('Location: ' . $authorizationUrl);
exit;
// The user will be redirected back to your specified redirect URI with an authorization code.
?>
和 hf.php
<?php
$clientId = '37779938-b...';
$clientSecret = '098260e6-1...';
$redirectUri = 'http://localhost/ai/hf';
// Step 2: Handle the callback and verify the state parameter
if (isset($_GET['code']) && isset($_GET['state'])) {
$state = $_GET['state'];
$code = $_GET['code'];
// Verify the state parameter to prevent CSRF attacks
// Perform your state validation here
// Step 3: Use the code to get an access token and id token
$tokenUrl = 'https://huggingface.co/oauth/token';
$tokenData = [
'client_id' => $clientId,
'client_secret' => $clientSecret,
'redirect_uri' => $redirectUri,
'code' => $_GET['code'],
'grant_type' => 'authorization_code',
];
$options = [
'http' => [
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($tokenData),
],
];
$context = stream_context_create($options);
$response = @file_get_contents($tokenUrl, false, $context);
if ($response === false) {
die("Failed to make the token request. Check your server configuration.");
}
$tokens = json_decode($response, true);
if (json_last_error() !== JSON_ERROR_NONE) {
die("Failed to decode token JSON. Error: " . json_last_error_msg());
}
if ($tokens !== null) {
// Step 4: Use the access token to fetch user data
$userInfoUrl = 'https://huggingface.co/oauth/userinfo';
$userInfoOptions = [
'http' => [
'header' => "Authorization: Bearer " . $tokens['access_token'] . "\r\n",
'method' => 'GET',
],
];
$userInfoContext = stream_context_create($userInfoOptions);
$userInfoResponse = @file_get_contents($userInfoUrl, false, $userInfoContext);
if ($userInfoResponse === false) {
die("Failed to make the user data request.");
}
// Process the user profile data
$userData = json_decode($userInfoResponse, true);
if (json_last_error() !== JSON_ERROR_NONE) {
die("Failed to decode user data JSON. Error: " . json_last_error_msg());
}
echo '<pre>';
print_r($userData);
echo '</pre>';
} else {
die("Failed to decode token JSON.");
}
} else {
echo "Authorization code or state is missing.";
}
?>
我在 hf.php 中遇到以下错误
令牌请求失败。检查您的服务器配置。
错误“无法发出令牌请求。请检查您的服务器配置。”表明向 Hugging Face 令牌端点发出 HTTP 请求时存在问题。您可以在代码中检查和修改以下内容:
启用错误报告: 在 PHP 脚本的开头添加以下行以启用错误报告。这将帮助您查看是否有任何 PHP 错误。
error_reporting(E_ALL);
ini_set('display_errors', 1);
验证 HTTP 上下文选项: 确保 HTTP 上下文选项设置正确。修改您的代码如下:
$options = [
'http' => [
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($tokenData),
'ignore_errors' => true, // Ignore HTTP errors to check the response content
],
];
$context = stream_context_create($options);
$response = file_get_contents($tokenUrl, false, $context);
if ($response === false) {
$error = error_get_last();
die("Failed to make the token request. Error: " . $error['message']);
}
添加
'ignore_errors' => true
允许您检查响应内容,即使请求导致 HTTP 错误。
检查 HTTPS 连接: 确保您的服务器支持 HTTPS。某些 OAuth 提供商需要令牌请求的安全连接。如果您的服务器通过 HTTP 运行,请考虑使用 HTTPS。