基本身份验证不适用于带有Ionic/Angular的WP API [英] Basic authentication not working for WP API with Ionic/Angular

查看:69
本文介绍了基本身份验证不适用于带有Ionic/Angular的WP API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将这个Ionic项目转到了使用WP API的地方.现在,在我的应用程序中,我希望用户能够提交帖子,并将数据保存到WP DB.我正在尝试通过让我的应用自动为所有人认证为1个用户来实现此目的.在以下文档的基础上,我安装了 angular-base64 .挂钩一切并运行该应用程序,但我仍然收到401(未经授权).

I got this Ionic project going where I make use of the WP API. Now in my app I want the user to be able to submit a post and that the data gets saved to the WP DB. I am trying to achieve this by having my app automatically authenticate as 1 user for everyone. Base of the following documentation, I installed the Basic Auth Plugin. I also installed angular-base64. Hooked everything up and run the app but I am still getting a 401 (unauthorised).

遵循关于CRUD的WP API V2文档用于发布的api路由如下所示-> POST/wp-json/wp/v2/posts创建一个新帖子

Following the WP API V2 documentation on CRUD the api route to use to post anything is the following -> POST /wp-json/wp/v2/posts to create a new Post

我的HTML代码的一部分:

Part of my HTML code:

<div class="padding">
        <textarea class="customTextarea" name="question" id="" cols="30" rows="30" 
        placeholder="Add Question Here" ng-model="question"></textarea>

        <button ng-click="save()">Submit</button>
    </div>

我的控制器代码的一部分:

Part of my controller code:

.controller('AddQuestionCtrl', function($scope, $http, $base64){
    $scope.save = function(){
        console.log('SAVE');
        var url = 'http://XXXXX/wp-json/wp/v2/posts';
        var username = 'XXXX';
        var psw2 = 'XXXXX';

        $http.post(url, {question: $scope.question},{
            headers:{
                'Content-Type' : 'application/x-www-form-urlencoded; charset=UTF-8',
                'Authorization' : 'Basic' + $base64.encode(username + ':' + psw2),
                'Access-Control-Allow-Origin': '*' 
            }
        }).then(function(response){
            $scope.result = response;
            console.log('The data has been saved to DB', $scope.result);
        });


    }

})

请有人帮帮我.我忘记做错了什么.

Please can someone help me out. What have I forgotten of done wrong.

推荐答案

我认为这很简单,就像您在Basic之后缺少空格一样:

I think it may be as simple as you're missing a space after Basic:

'Authorization' : 'Basic ' + $base64.encode(username + ':' + psw2),

这篇关于基本身份验证不适用于带有Ionic/Angular的WP API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆