Poco HTTPSClientSession with google drive api

Poco HTTPSClientSession with google drive api

本文关键字:drive api google with HTTPSClientSession Poco      更新时间:2023-10-16

为了让我的桌面应用程序访问我的谷歌云端硬盘,我正在使用 Poco 进行身份验证和查询。请求访问令牌时,我遇到了Poco::Net::HTTPSClientSession问题。我的代码如下。

namespace net = Poco::Net;
net::Context::Ptr ctx =
    new net::Context(net::Context::CLIENT_USE, "", net::Context::VerificationMode::VERIFY_NONE);
net::HTTPSClientSession client("accounts.google.com", net::HTTPSClientSession::HTTPS_PORT, ctx);
net::HTTPRequest req(net::HTTPRequest::HTTP_POST, "/o/oauth2/token", net::HTTPMessage::HTTP_1_1);
net::HTMLForm    form;
form.add("code", accessCode);
form.add("client_secret", client_secret);
form.add("client_id", client_id);
form.add("grant_type", "authorization_code");
form.add("redirect_uri", redirect_uri);
form.prepareSubmit(req);
// req.setChunkedTransferEncoding(true);
form.write(std::cout);
std::cout << std::endl;
client.sendRequest(req);
req.write(std::cout);
std::cout << "Method: " << req.getMethod() << std::endl;
net::HTTPResponse resp;
auto&&            is = client.receiveResponse(resp);
std::copy(
    std::istream_iterator<char>(is), std::istream_iterator<char>(), std::ostream_iterator<char>(std::cout));
std::cout << std::endl;

我收到上述代码超时异常。如果我打开分块传输编码,我会收到来自谷歌的错误

"缺少必需参数:grant_type">

我的代码中的传输编码存在一些错误,我无法弄清楚。我错过了什么?

编辑:邮递员中的相同请求工作正常(有或没有分块传输编码(。

谢谢苏里亚

回答我自己的问题。

我没有将表单数据写入请求的正文中,这会带来问题。像这样更改了代码

auto&& strm = client.sendRequest(req);
form.write(strm);

现在它起作用了。