简体   繁体   English

使用Boost C ++的HTTP GET请求-oauth2访问令牌

[英]HTTP GET request with boost c++ - oauth2 access token

I need to connect from ac ++ application to a rest server, my problem is when sending the token access provided by the server. 我需要从ac ++应用程序连接到其他服务器,我的问题是在发送服务器提供的令牌访问时。

I have tried in several ways but I can not find the correct syntax. 我已经尝试了几种方法,但是找不到正确的语法。

The rest server uses oauth2 for authentication and the token type is bearer. 其余服务器使用oauth2进行身份验证,令牌类型为bearer。

In oauth1 I found this syntax: 在oauth1中,我发现了以下语法:

std::ostream request_stream(&request_);
request_stream << "GET " << path << " HTTP/1.0\r\n";
request_stream << "Host: " << server << "\r\n";
request_stream << "Content-Type: application/x-www-form-urlencoded\r\n";
request_stream << "Authorization: OAuth oauth_version=\"1.0\", "
<< oauth_signature_method=\"PLAINTEXT\", "
<<"oauth_consumer_key=\"<key>\", "
<<"oauth_signature=\"<secret>&\"\r\n";
request_stream << "Accept: */*\r\n";
request_stream << "Connection: close\r\n\r\n";

But for oauth2 I can not find the correct syntax. 但是对于oauth2我找不到正确的语法。

Of all the attempts I have made I will highlight 2: 在我进行的所有尝试中,我将重点介绍2:

1. 1。

std::ostream request_stream(&request_);
request_stream << "GET " << path << " HTTP/1.0\r\n";
request_stream << "Host: " << server << "\r\n";
request_stream << "Authorization: Bearer accesstoken\r\n";
request_stream << "Accept: */*\r\n";
request_stream << "Connection: close\r\n\r\n";

Error returned: "Exception: unspecified file(1): unterminated string" 返回错误:“异常:未指定的文件(1):未终止的字符串”

2. 2。

std::ostream request_stream(&request_);
request_stream << "GET " << path << " HTTP/1.0\r\n";
request_stream << "Host: " << server << "\r\n";
request_stream << "Content-Type: application/x-www-form-urlencoded\r\n";
request_stream << "Authorization: OAuth oauth_version=\"2.0\", "
<<"oauth_token_type=\"Bearer\", "
<<"oauth_access_token=\"accesstoken\"\r\n";
request_stream << "Accept: */*\r\n";
request_stream << "Connection: close\r\n\r\n";

Error returned: "Response returned with status code 401" 返回错误:“响应返回状态码401”

Additionally, try without "\\ r \\ n" at the end of the line in both cases, but it doesn't work. 此外,在两种情况下,请在行尾均不使用“ \\ r \\ n”,但这是行不通的。

request_stream << "Authorization: Bearer accesstoken";

request_stream << "Authorization: OAuth oauth_version=\"2.0\", "
<<"oauth_token_type=\"Bearer\", "
<<"oauth_access_token=\"accesstoken\"";

I need the correct text string of the request "Authorization" to send the access token to the server: 我需要请求“授权”的正确文本字符串,以将访问令牌发送到服务器:

request_stream << "Authorization: ??????? \r\n";

Does anyone know the correct syntax for sending the access token? 有谁知道发送访问令牌的正确语法?

Thanks!! 谢谢!!

Edited - Nov 18 编辑-11月18日

Forget to mention that the rest server is made with symfony2 and fosrestbundle. 忘记提及其余服务器是由symfony2和fosrestbundle制成的。 Apparently fosrestbundle requires knowing it well to properly configure the service because otherwise it can give problems. 显然fosrestbundle需要了解它才能正确配置服务,因为否则它可能会带来问题。

Now I'm testing rest with symfony3 and firebase-jwt. 现在,我正在使用symfony3和firebase-jwt测试其余部分。 I guess I will not have problems because I can configure almost everything, without having to go so far in an outside bundle. 我想我不会有问题,因为我可以配置几乎所有东西,而不必在外部捆绑包中走得太远。

I have not tried with symfony3 and fosrestbundle, but it seems that fosrestbundle requires knowing it well to configure it correctly. 我没有尝试使用symfony3和fosrestbundle,但似乎fosrestbundle需要了解它才能正确配置。

When I say that more advanced knowledge about this bundle is required, it is because it works me very well from other applications that I did, for example an android application; 当我说需要有关此捆绑软件的更高级知识时,是因为它对我所做的其他应用程序(例如android应用程序)非常有效。 But from ac ++ application gives me problems. 但是从ac ++应用程序给我带来了问题。

I hope this information is useful for someone 我希望这些信息对某人有用

Edited Nov 21 编辑11月21日

I tried with symfony3 and firebase-jwt and it keeps giving me the same errors. 我尝试使用symfony3和firebase-jwt,它一直给我同样的错误。

I was wrong with fosrestbundle, in fact both servers (fosrestbundle and symfony3-jwt) do the authentication and correctly send the data that the client requests. 我对fosrestbundle错了,实际上两个服务器(fosrestbundle和symfony3-jwt)都进行身份验证并正确发送客户端请求的数据。

The problem is on the client side: 问题出在客户端:

void Restclient::handle_read_content(const boost::system::error_code& err)
{
    if (!err)
    {
        std::ostringstream ss;
        ss << &response_;
        std::string s = ss.str();
        boost::property_tree::ptree pt2;
        stringstream is (s);
        boost::property_tree::read_json (is, pt2);

        boost::asio::async_read(socket_, response_, boost::asio::transfer_at_least(1), boost::bind(&Restclient::handle_read_content, this, boost::asio::placeholders::error));
    }
    else if (err != boost::asio::error::eof)
    {
        std::cout << "Error: " << err << "\n";
    }
}

After performing several tests, I verified that exactly in this line is where the error occurs: 在执行了几次测试之后,我验证了在这一行中正是发生错误的地方:

boost::property_tree::read_json (is, pt2);

The json arrives well: (for example) json顺利到达:(例如)

{ "Data1": "c ++", "data2": "java", "data3": "python", "data4": "php", "data5": "c #"}

I'm not going to put the actual json because it's a pretty long string, but it's just to give you an idea. 我不会放入实际的json,因为它是一个很长的字符串,但这只是为了给您一个想法。

And at the moment of reading and converting to json, this appears: 在读取并转换为json的时刻,将出现:

{ "Data1": "c ++", "data2": "java", "data3": "python", "dat
Exception: <unspecified file> (1): unterminated string

The code I exposed here is based on examples of boost c ++ async_client, but it gives me that problem. 我在这里公开的代码基于boost c ++ async_client的示例,但这给了我这个问题。

If someone has solved this problem, I would appreciate your help. 如果有人解决了这个问题,请多多帮助。

Edit - Nov 27 [SOLVED] 编辑-11月27日[已解决]

std::ostream request_stream(&request_);
request_stream << "GET " << path << " HTTP/1.0\r\n";
request_stream << "Host: " << server << "\r\n";
request_stream << "Authorization: Bearer accesstoken\r\n";
request_stream << "Accept: */*\r\n";
request_stream << "Content-Length: 0\r\n\r\n";

void Restclient::handle_read_content(const boost::system::error_code& err)
{
    if (!err)
    {
        boost::system::error_code error;
        string result = "";
        //Read the answer to the end
        while (boost::asio::read(socket_, response_, boost::asio::transfer_at_least(1), error))
        {
            std::ostringstream ss;
            ss << &response_;
            std::string output = ss.str();
            result = result + output;
        }

        //If the answer has already arrived and there is nothing left to read
        if (error == boost::asio::error::eof)
        {
            stringstream is (result);
            boost::property_tree::ptree pt2;
            boost::property_tree::read_json (is, pt2);
        }
        boost::asio::async_read(socket_, response_, boost::asio::transfer_at_least(1), boost::bind(&Restclient::handle_read_content, this, boost::asio::placeholders::error));
    }
    else if (err != boost::asio::error::eof)
    {
        std::cout << "Error: " << err << "\n";
    }
}

JSON is complete and ptree is generated correctly JSON已完成且ptree正确生成

You have some HTTP formatting issues. 您有一些HTTP格式问题。

None of your requests are sending the size of the message body (in your case zero) and since you are sending HTTP 1.0 requests, there is no need to send Connection: close , the server should close the connection after it has sent it's response. 您的请求都没有发送消息正文的大小(在您的情况下为零),并且由于您正在发送HTTP 1.0请求,因此无需发送Connection: close ,服务器应在发送响应后关闭连接。 If you don't want that behaviour then change HTTP/1.0 to HTTP/1.1 . 如果您不希望这种行为,请将HTTP/1.0更改为HTTP/1.1

Try replacing the last line of attempt 2: 尝试替换尝试2的最后一行:

request_stream << "Connection: close\r\n\r\n";

with: 有:

request_stream << "Content-Length: 0\r\n\r\n";

You should receive a 200 (OK) or a 401 (Unauthorized) response. 您应该收到200 (确定)或401 (未授权)的响应。

FYI the syntax is defined in the HTTP specification, RFC7230 , specifically RFC7235 for authentication. 仅供参考,语法在HTTP规范RFC7230中定义,尤其是在RFC7235中进行身份验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM