php - 无法继续通过 OAuth2.0
问题描述
我已经生成了令牌和代码以及所有内容,并且我能够登录并授权用户,但不知何故我遇到了错误。发现了两个错误。
第一
(
[error] => invalid_grant
[error_description] => Authorization code doesn't exist or is invalid for the client
)
有时没有生成授权码,所以我遇到了上述错误。有时当我能够生成 auth_code 时,我会遇到错误
{"error":"invalid_token","error_description":"The access token provided is invalid"}
我是网站 abcd.com 的开发人员,现在我有一个平台,我需要为该平台开发我的 abcd.com。要实现登录,我必须使用 OAuth2.0,这是平台的要求。我已经阅读了很多关于 OAuth2.0 的文档并构建了应用程序。我正在使用codeigniter。
服务器.php
$dsn = 'mysql:dbname=my_oauth2_db;host=localhost';
$username = 'root';
$password = '';
// error reporting (this is a demo, after all!)
ini_set('display_errors',1);error_reporting(E_ALL);
// Autoloading (composer is preferred, but for this example let's just do this)
require_once('oauth2-server-php/src/OAuth2/Autoloader.php');
OAuth2\Autoloader::register();
// $dsn is the Data Source Name for your database, for exmaple "mysql:dbname=my_oauth2_db;host=localhost"
$storage = new OAuth2\Storage\Pdo(array('dsn' => $dsn, 'username' => $username, 'password' => $password));
// Pass a storage object or array of storage objects to the OAuth2 server class
$server = new OAuth2\Server($storage);
// Add the "Client Credentials" grant type (it is the simplest of the grant types)
$server->addGrantType(new OAuth2\GrantType\ClientCredentials($storage));
// Add the "Authorization Code" grant type (this is where the oauth magic happens)
$server->addGrantType(new OAuth2\GrantType\AuthorizationCode($storage));
$scope = new OAuth2\Scope(array(
'supported_scopes' => array('email')
));
$server->setScopeUtil($scope);
这是我的authorize.php
require_once __DIR__.'/server.php';
$request = OAuth2\Request::createFromGlobals();
$client_id = $request->query['client_id'];
$response = new OAuth2\Response();
if (!$server->validateAuthorizeRequest($request, $response)) {
$response->send();
die;
}
if (empty($_POST)) {
exit('<form method="post">
<label>Do You Authorize '.$client_id.'?</label><br />
<input class="yes_authorize" type="submit" name="authorized" value="Yes">
<input class="no_authorize" type="submit" name="authorized" value="No">
</form>');
}
$is_authorized = ($_POST['authorized'] === 'Yes');
$server->handleAuthorizeRequest($request, $response, $is_authorized);
if ($is_authorized) {
$code = substr($response->getHttpHeader('Location'), strpos($response->getHttpHeader('Location'), 'code=')+5, 40);
$client_secret = $_GET['state'];
$token_value = shell_exec("curl -u CLIENT_ID:CLIENT_SECRET https://abcd.com/api/token.php -d 'grant_type=authorization_code&code=$code'");
$token = json_decode($token_value);
$access_token = $token->access_token;
$expires_in = $token->expires_in;
$state = $_GET['state'];
$resource_result = shell_exec("curl https://abcd.com/api/resource.php -d 'access_token=$code'");
$redirect_url = $_GET['redirecturi']."?code=$access_token&state=".$_GET['state'];
exit(header("location: $redirect_url"));
}
$response->send();
资源.php
require_once __DIR__.'/server.php';
if (!$server->verifyResourceRequest(OAuth2\Request::createFromGlobals())) {
$server->getResponse()->send();
die;
}
echo json_encode(array('success' => true, 'message' => 'You accessed my APIs!'));
令牌.php
require_once __DIR__.'/server.php';
$server->handleTokenRequest(OAuth2\Request::createFromGlobals())->send();
如果您看到 authorize.php,在通过 token.php 生成 access_token 后,如果我将其传递给 resource.php,则会出现第二个错误。而且我已经将 auth_code 和 access_token 都传递给了我的重定向 url,但是我无法通过。
解决方案
推荐阅读
- c++11 - 访问私有类数据类型
- c# - 如何使用 mongodb c# 驱动程序按存储为字符串的 TimeSpan 字段进行过滤?
- python - 如何简单地为 numpy 直方图设置替代不等式条件?
- node.js - 通过NodeJS将文件加载到facebook messenger的问题
- r - 带闪亮的在线按钮
- c# - SQL SERVER Compact 4.0 中的加密/哈希密码表
- c# - 为什么我的项目没有从 Visual Studio 上传到 github?
- python - 烧瓶复选框和文本框问题
- python - 如何在 Seaborn catplot 中指定自定义误差线?
- android - 我是否安全地注入了上下文?匕首,Android Studio 警告