【问题标题】:Poco HTTPSClientSession with google drive apiPoco HTTPSClientSession 与谷歌驱动 api
【发布时间】:2018-01-10 02:59:01
【问题描述】:

为了让我的桌面应用程序访问我的谷歌驱动器,我使用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”

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

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

谢谢, 苏里亚

【问题讨论】:

    标签: c++ c++11 google-drive-api poco poco-libraries


    【解决方案1】:

    回答我自己的问题。

    我没有将表单数据写入出现问题的请求正文中。改成这样的代码

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

    现在可以了。

    【讨论】:

      猜你喜欢
      • 2012-07-04
      • 1970-01-01
      • 1970-01-01
      • 2023-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多