资源所有者密码授予“必须是POST请求”

时间:2020-04-25 15:20:57

标签: javascript node.js oauth

我一直在尝试使用资源所有者密码授予oAuth进行登录,但是我似乎无法使其正常工作。

我没有构建向其发送请求的后端,而是使用Invision Power Board's。我无法使用简单的const form = { 'grant_type': 'password', 'username': 'username', 'password': 'pass', 'scope': 'profile', 'client_id': 'client_id', } var formBody = []; for (var property in form) { var encodedKey = encodeURIComponent(property); var encodedValue = encodeURIComponent(form[property]); formBody.push(encodedKey + "=" + encodedValue); } formBody = formBody.join("&"); await fetch(`https://example.com/oauth/token`, { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' }, body: formBody }) 找到任何示例请求,所以我不确定是否做错了什么。

这是我的请求被发送到客户端:

{
    "error": "invalid_request",
    "error_description": "request must be a POST request"
}

我得到的只是一个错误:

POST /oauth/token HTTP/1.1
Host: example.com
Content-Type: application/x-www-form-urlencoded
Content-Length: 85

grant_type=password&username=username&password=pass&scope=profile&client_id=client_id

我格式化的方式有误吗?还是我遗漏了一些东西?我一直在关注oAuth文档,但也许我误解了如何执行此操作。

我的服务器有问题吗?

编辑-使用reqbin的示例请求:

{
    "error": "invalid_client"
}

奇怪的是,这里的响应是:

{{1}}

2 个答案:

答案 0 :(得分:0)

您正在使用<?php if(isset($_POST['submit'])){ if(empty($_POST['email'])){ echo 'An email is required <br />'; } else { $email = $_GET['email']; } if(empty($_POST['title'])){ echo 'A title is required <br />'; } else { $title = $_GET["title"]; } if(empty($_POST['ingredients'])){ echo 'At least one ingredient is required <br />'; } else { $ingredients = $_GET["ingredients"]; } } ?> <html> <form action="index.php" method="POST" class="white"> <label> Your email:</label> <input type="text" name="email"> <label> Pizza title:</label> <input type="text" name="title"> <label> Ingredients</label> <input type="text" name="ingredients"> <div class="center"> <input type="submit" name="submit" value="submit" class="btn brand z-depth-0"> </div> </form> </html> 方法,但是您的请求正文不是有效的POST文件。

查看如何将对象转换为Urlencoded字符串并将其发送到此处:https://stackoverflow.com/a/37562814/7638119

答案 1 :(得分:0)

根据规范,密码流需要用户的凭据以及客户端凭据。但是我看不到您的请求具有“授权”标头。