具有野兽增强功能的OAuth2返回临时重定向307

时间:2018-05-14 11:18:21

标签: c++ oauth-2.0 google-oauth google-oauth2 boost-beast

我尝试使用theoauth2身份验证来实现可以访问goast boost C ++的谷歌驱动器的应用。

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();

这个回归:

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吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

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

我希望其他人(也许是你?)将建立在野兽之上,并以开源库的形式提供更高级别的功能。