0

我正在尝试将 NelmioApiDocBundle 用于 Symfony 3.4 项目 API 文档,同时还试图围绕 OAuth 2 授权来开始项目 API 访问。

到目前为止,我已经按照本教程了解如何让 FOSOAuthServerBundle 工作。到目前为止,我可以 1.) 使用命令行命令创建客户端:

php bin/console fos:oauth-server:create-client --redirect-uri="___" --grant-type="authorization_code" --grant-type="password" --grant-type="refresh_token" - -grant-type="token" --grant-type="client_credentials"

2.) 我也可以通过在我的服务器上访问这个 url 来手动获取访问令牌

http://127.0.0.1:8000/oauth/v2/token?client_id=______&client_secret=________&grant_type=client_credentials

3.) 我可以使用令牌访问我的 Symfony 项目中需要 OAuth 访问的区域,方法是在 GET 参数中包含令牌

但是,在 NelmioApiDocBundle Authorizations 中,我无法完成这项工作。这是一个屏幕截图:

NelmioApiDocBundle 授权页面

如果输入我的 client_id 和密钥,它会按预期将我带到登录页面。我可以输入我的登录信息并按预期将我带到批准或拒绝页面。此时,如果我单击 Approve 或 Deny,它会尝试使用http://localhost:3200/oauth2-redirect.html的“redirect_uri” 。无论我做什么,我都无法更改重定向 URI。

如何获得正确的重定向 URI?

4

1 回答 1

4

好的,这实际上很容易解决。您需要添加一行:

oauth2RedirectUrl: 'URLhere',

到位于 web/bundles/nelmioapidoc/ 中(Symfony 3.4)的文件 init-swagger-ui.js

最终文件最终看起来像这样:

window.onload = () => {
  const data = JSON.parse(document.getElementById('swagger-data').innerText);
  const ui = SwaggerUIBundle({
      oauth2RedirectUrl: 'URLhere',
    spec: data.spec,
    dom_id: '#swagger-ui',
    validatorUrl: null,
    presets: [
      SwaggerUIBundle.presets.apis,
      SwaggerUIStandalonePreset
    ],
    plugins: [
      SwaggerUIBundle.plugins.DownloadUrl
    ],
    layout: 'StandaloneLayout'
  });

  window.ui = ui;
};

此外,您可能希望从 Swagger 项目下载文件oauth2-redirect.html以包含实际重定向。

于 2018-03-27T17:33:34.930 回答