gpt4 book ai didi

php - 仅使用 bshaffer OAuth2.0 库实现客户端凭据授权类型

转载 作者:搜寻专家 更新时间:2023-10-31 20:35:25 24 4
gpt4 key购买 nike

我决定将 bshaffer 的库用于 OAuth2.0 ( https://bshaffer.github.io/oauth2-server-php-docs/ )。我正在使用它为我的 API 实现客户端凭证授权类型。请求访问 token 时(使用硬编码的 client_id 和 client_secret),一切正常。我通过以下内容

grant_type => client_credentials
client_id => oauthuser
client_secret => xkJ7ua2p9zaRQ78YxYAfTCKGUaGEfMS6

结果如下:

{
"access_token": "855b36508abfdfcd25281e36020ab48917d4a637",
"expires_in": 3600,
"token_type": "Bearer",
"scope": null
}

但是每当我使用这个作为标题请求我的数据时:

authorization => Bearer 855b36508abfdfcd25281e36020ab48917d4a637

我收到一条错误消息,指出我的 token 无效:

{
"error": "invalid_token",
"error_description": "The access token provided is invalid"
}

我做错了什么?如演示应用程序所示,client_credentials 授权类型是否可以在没有授权授权类型的情况下使用?

这是我的一些代码:

对于初始化 OAuth 2.0 服务器的文件:

namespace App\Libraries;

use Silex\Application;
use Silex\ControllerProviderInterface;
use OAuth2\Storage\Memory as OAuth2MemoryStoraage;
use OAuth2\Server as OAuth2Server;
use OAuth2\GrantType\ClientCredentials;
use OAuth2\HttpFoundationBridge\Response as BridgeResponse;

class OAuth2Library implements ControllerProviderInterface
{
public function setup(Application $app)
{
$clients = array('oauthuser' => array(
'client_secret' => 'xkJ7ua2p9zaRQ78YxYAfTCKGUaGEfMS6'
));
$storage = new OAuth2MemoryStoraage(array('client_credentials' => $clients));

$server = new OAuth2Server($storage, array('issuer' => $_SERVER['HTTP_HOST']));

$server->addGrantType(new ClientCredentials($storage));

$app['oauth_server'] = $server;

$app['oauth_response'] = new BridgeResponse();
}

public function connect(Application $app)
{
$this->setup($app);

$routing = $app['controllers_factory'];
$routing->post('/accesstoken', 'App\\Controllers\\OAuthController::authorize');

return $routing;
}
}

对于给出访问 token 的函数(在另一个文件中):

namespace App\Controllers;

use OAuth2;
use Silex\Application;
use Symfony\Component\HttpFoundation\Response;
use OAuth2\HttpFoundationBridge\Request as BridgeRequest;

class OAuthController
{
public function authorize(Application $app)
{
$server = $app['oauth_server'];
$response = $app['oauth_response'];
return $server->handleTokenRequest($app['request'], $response);
//return $app->json($encoded, 200);
}
}

最后,在另一个文件中,获取资源的函数:

namespace App\Controllers;

use Silex\Application;
use Symfony\Component\HttpFoundation\Response;
use OAuth2;

class HelloController
{
public function get(Application $app)
{
$server = $app['oauth_server'];
$response = $app['oauth_response'];
if (!$server->verifyResourceRequest($app['request'], $response)) {
return $server->getResponse();
}
else
{
$result = $app['db']->fetchAssoc("select * from user");
return new Response(json_encode($result));
}
}
}

我做错了什么?谢谢!

最佳答案

OAuth2\Storage\Memory 不会在请求之间持久化,因此您需要使用数据库(例如 OAuth2\Storage\Pdo)来存储 access_tokens。

您可以将 SQLite 作为单个文件与 PDO 一起用于测试:sqlite.org/onefile.html

关于php - 仅使用 bshaffer OAuth2.0 库实现客户端凭据授权类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36791937/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com