【发布时间】:2019-03-08 04:19:08
【问题描述】:
我相信我刚刚找到了过去几天一直困扰我的错误,但它的影响让我非常头疼。
我在 macOS mojave 机器上,使用本地 libcurl 工具(在 /usr/lib 中),但我认为 cURL 本身不是问题。
我一直在尝试使用以下代码向 Twitter 提交 OAuth2 请求,并正确提供了所有标头(未图示)。
// supplying https://api.twitter.com/oauth2/token?grant_type=client_credentials for ease of use
curl_easy_setopt(curl, CURLOPT_URL, "https://api.twitter.com/oauth2/token");
std::string grantType = "grant_type=client_credentials";
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, grantType.c_str());
但是,这会失败并显示状态代码
{"errors":[{"code":170,"message":"Missing required parameter: grant_type","label":"forbidden_missing_parameter"}]}
困惑,我试了一下:
curl_easy_setopt(curl, CURLOPT_URL, "https://api.twitter.com/oauth2/token");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "grant_type=client_credentials");
效果很好:
{"token_type":"bearer","access_token":"AAAAAAAAAAAAAAAAAAAAAIMu6QAAAAAArr86Rv2W70fTicd4yAir7..."}
所以要么我不了解 C++ 的一个关键方面,要么我打破了宇宙——为什么调用 c_str() 不会产生相同的输出?
我还在一个单独的项目中运行了以下内容,以尝试了解正在发生的事情。
std::string string1 = "test";
char* string2 = "test";
assert((strcmp(string1.c_str(), string2)) || (string1.c_str() == string2));
为什么定义一个 char* 与定义一个字符串然后调用 c_str() 产生的值不同?
【问题讨论】:
-
std::string超出范围时会破坏其字符串,但const char*文字不会被破坏。难道curl_easy_setopt要求传递的字符串比包含std::string的函数寿命更长? -
我强烈建议您使用调试器单步执行您的代码,以便您可以检查
grantType和grantType.c_str()的值 -
非常感谢,确保字符串不会被破坏超出范围工作完美!刚刚尝试(暂时)“curl_easy_setopt(curl, CURLOPT_URL, "api.twitter.com/oauth2/token"); std::string* s = new std::string("grant_type=client_credentials"); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, s->c_str());"
-
不确定如何将答案归功于您,如果您想创建一个我会接受它(或者我可以制作一个引用您的评论的)。
-
@Makiah“确保字符串不会在范围之外被破坏完美!” - 如果你事先有read the documentation,你会知道的。仅供参考,您对
new字符串的“解决方案”是等待发生的内存泄漏,如果您在 libcurl 完成使用后不delete字符串