Poco HTTPSClientSession与谷歌驱动器API

时间:2021-04-09 15:23:11

For my desktop app to access my google drive, I'm using Poco for authentication and queries. When requesting for access token, I'm facing problems with Poco::Net::HTTPSClientSession. My code is given below.

我的桌面应用程序访问我的谷歌驱动器,我正在使用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;

I'm getting Timeout exception with the above code. If I turn on chunked transfer encoding, I'm getting error from google

我正在使用上面的代码获得Timeout异常。如果我打开分块传输编码,我收到来自谷歌的错误

"Required parameter is missing: grant_type"

“缺少必需参数:grant_type”

There's something incorrect with transfer encoding in my code which I could not figure out. What am I missing ?

我的代码中的传输编码有些不正确,我无法弄清楚。我错过了什么?

Edit: The same request in Postman works fine (with or without chunked transfer-encoding).

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

Thanks, Surya

1 个解决方案

#1


0  

Answering my own question.

回答我自己的问题。

I'm not writing the form data into body of the request which is giving problems. Changed the code like this

我不是将表单数据写入提出问题的请求正文中。改变了这样的代码

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

Now it works.

现在它有效。

#1


0  

Answering my own question.

回答我自己的问题。

I'm not writing the form data into body of the request which is giving problems. Changed the code like this

我不是将表单数据写入提出问题的请求正文中。改变了这样的代码

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

Now it works.

现在它有效。