OAuth2 with beast boost returns 临时重定向 307

OAuth2 with beast boost returns temporary redirect 307

我正在尝试使用 oauth2 身份验证在 beast boost C++ 中实现一个可以访问 google 驱动器的应用程序。

https://developers.google.com/identity/protocols/OAuth2ForDevices

我尝试使用以下 POST 请求在 Postman 中获取用户代码:

POST /o/oauth2/device/code HTTP/1.1
Host: accounts.google.com
Content-Type: application/x-www-form-urlencoded
Cache-Control: no-cache


scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive.file&client_id=610490019085-l1v2mv7lv95lu7cr111vbtqmp1bigv42.apps.googleusercontent.com

它工作得很好,返回:

{
"verification_url": "https://www.google.com/device",
"expires_in": 1800,
"interval": 5,
"device_code": "AH-1Ng0IgBnIXIUeltwDoL7AwNExNTT0rozdxD5FMnP8dip4DaDi8_XtzK2aVT92YKYmYa7KWqHRVqw5AmJCDtalzK3k6pvbFw",
"user_code": "LWZY-BDXD"

}

现在我想使用 boost 在 C++ 中执行相同的请求,请求的代码片段如下:

http::request<http::string_body> req{http::verb::post, "/o/oauth2/device/code", 11};

req.set(http::field::host, "accounts.google.com");
req.set("Cache-Control", "no-cache");
req.set(http::field::content_type, "application/x-www-form-urlencoded");
req.body() = "scope=https://www.googleapis.com/auth/drive.file&client_id=610490019085-l1v2mv7lv95lu7cr111vbtqmp1bigv42.apps.googleusercontent.com";

req.prepare_payload();

这个returns:

HTTP/1.0 307 Temporary Redirect
Content-Type: text/html; charset=UTF-8
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: Mon, 01 Jan 1990 00:00:00 GMT
Date: Mon, 14 May 2018 11:06:01 GMT
Location: https://accounts.google.com/o/oauth2/device/code
Content-Length: 232
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
Server: GSE

<HTML>
<HEAD>
<TITLE>Temporary Redirect</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Temporary Redirect</H1>
The document has moved <A HREF="https://accounts.google.com/o/oauth2/device/code">here</A>.
</BODY>
</HTML>

有什么想法可以像 Postman 一样返回 JSON 吗?

谢谢!

Beast 是一个低级协议库,它对解析域名或连接套接字一无所知。它甚至不知道 TCP/IP,只知道如何序列化和反序列化 HTTP/1 满足 Asio 流概念要求的对象的消息(示例:SyncReadStream 或 AsyncWriteStream)。您必须自己处理重定向。如果您收到重定向响应,提取 Location 字段值并解析 URI,解析域,然后对指定资源发出另一个 GET 请求。

我希望其他人(也许是你?)将在 beast 之上构建并以开源库的形式提供像这样的更高级别的功能。