简体   繁体   English

使用无头 WP CMS 后端从 Angular2+ 获取 https://.../wp-json/wp/v2/users/me 401(未经授权)

[英]GET https://…/wp-json/wp/v2/users/me 401 (Unauthorized) from Angular2+ with Headless WP CMS backend

What I'm trying to do: I want to be able to retrieve currently logged in private user details, ie.我正在尝试做的事情:我希望能够检索当前登录的私人用户详细信息,即。 current user email, posts made by the current user, past terawallet transactions, wallet balance, user settings and display it on the angular front end from the wp backend.当前用户 email,当前用户发布的帖子,过去的 terawallet 交易,钱包余额,用户设置并从 wp 后端显示在 angular 前端。 For this, I am using the wp-api-angular library为此,我正在使用 wp-api-angular 库

What I've been able to achieve:我能够实现的目标:

-log in to various accounts from angular front end -从angular前端登录各种账号

-create basic posts - 创建基本帖子

-delete posts - 删除帖子

-edit posts - 编辑帖子

-Create token and I can see it in my Networks tab in dev tools, with the user_display_name, user_email, and user_nicename -创建令牌,我可以在开发工具的“网络”选项卡中看到它,其中包含 user_display_name、user_email 和 user_nicename

Essentially I've been able to replicate the demo content here https://github.com/wordpress-clients/wp-api-angular#wpapiusers , the issue is when I try to reach site.com/wp-json/wp/v2/users/me (where I assume all the details I'm looking for are at) it returns to me a 401 unauthorized基本上我已经能够在这里复制演示内容https://github.com/wordpress-clients/wp-api-angular#wpapiusers ,问题是当我尝试访问 site.com/wp-json/wp/ v2/users/me(我假设我正在寻找的所有细节都在)它返回给我一个 401 未经授权

What I've tried to do: I've been going at it for a few hours and I can't find the issue (I'm still new to json)我试图做的事情:我已经做了几个小时了,但我找不到问题(我还是 json 新手)

-Added to .htaccess -添加到 .htaccess

RewriteEngine on
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule ^(.*) - [E=HTTP_AUTHORIZATION:%1]

-Added to /etc/apache2/sites-enabled/000-default.conf -添加到 /etc/apache2/sites-enabled/000-default.conf

AllowOverride All

-Changed permissions from None to All in /etc/apache2/apache2.conf in /var/www/ -在 /var/www/ 中的 /etc/apache2/apache2.conf 中将权限从无更改为全部

<Directory /var/www/>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

On postman it says I have a CORS issue, but on reqbin it goes through just fine, so I'm confused在 postman 上它说我有一个 CORS 问题,但在 reqbin 上它通过就好了,所以我很困惑

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

So I was able to find a temporary, dev-only solution to this with the chrome extension called "CORS Unblock", this is aligned to the issue I was seeing on Postman that said that I had a CORS issue.因此,我能够使用名为“CORS Unblock”的 chrome 扩展找到一个临时的、仅限开发的解决方案,这与我在 Postman 上看到的问题一致,即我遇到了 CORS 问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM