无法在 wordpress REST api 上设置 OAuth [英] Can not set OAuth on wordpress REST api
问题描述
我想在我的 Wordpress 网站中使用 授权 实现 Rest api.Wordpress 的版本是 4.9.5
和 REST api 版本 2.0-beta15
.为了保护我的 api,我需要在我的网站上实施 OAuth,首先,我从这个 link 和这个有用的链接.但是插件有这些问题:
I want to implement Rest api with authorization in my Wordpress website. the version of Wordpress is 4.9.5
and REST api version 2.0-beta15
. For securing my apis i need to implement OAuth on my site and at first, I implement WordPress REST API – OAuth 1.0a Server from this link and this useful link. But had these issues with plugin:
- rest-calls 仍然可以在未经授权的情况下工作,每个未经授权的用户都可以调用我的 rest-apis.
- 我想在我的 Android 应用程序中实现这个,所以我必须在后台处理所有步骤,但是使用这个插件,我应该向用户显示许多 webView,用户应该填充它们,并且最终的令牌也会显示给用户在 webview 中,这对我来说不太好.
所以我寻找另一个插件并找到WP-API 的 JWT 身份验证.这个插件更简单方便.我按照说明操作,可以获取令牌并添加到请求标头以发出请求,如果是有效响应,则为 200,如果不需要验证.一切似乎都很好!
So I looking for another plugin and find JWT Authentication for WP-API. This plugin is easier and handy. I followed the instructions and can get token and add to request header to make a request and if it is the valid response is 200 and if not need to validate. Every thing seems to work fine!
但同样的问题:
- 每个 REST API 甚至都可以在没有授权标头的情况下工作,如果我不放置标头,结果是 200 并且所有请求都像以前一样工作,无需任何授权
尽管文件说:
获得令牌后,您必须将其存储在您的应用程序,例如在 cookie 中或使用 localstorage.
Once you get the token, you must store it somewhere in your application, ex. in a cookie or using localstorage.
此时,您应该将此令牌传递给每个 API 调用
From this point, you should pass this token to every API call
所以现在我看到我不限制 API 调用在两个插件中都有 Authorization 标头,我如何实现这一点以及最好的解决方案是什么?
So now I see that I cloud not restrict API call to have Authorization header in both plugins, how I can achieve that and what is the best solution?
推荐答案
我终于用这个插件解决了这个问题:
I finally solve this problem with this plugin:
正如它的名字所说,它将完全按照我的意愿行事.它适用于我使用的两个插件
As its name says it will do exactly what i want. It works on both plugin i used
- WP REST API 的 JWT 身份验证
- WP OAuth 服务器
并限制所有未经授权且工作正常的 api 调用.所以我的最佳实践是 JWT 用于 Oauth + 禁用 REST API 和需要 JWT/OAuth 身份验证,它们是在 android 应用程序中为 word-press 实现 OAuth 的非常好的组合.
And restricts all api calls that not authorized and works fine. So i my best practice is JWT for Oauth + Disable REST API and Require JWT / OAuth Authentication and they are very good combination to implement OAuth in android application for word-press.
这篇关于无法在 wordpress REST api 上设置 OAuth的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!