c++ - 用于登录谷歌的 libcurl 格式化帖子
问题描述
所以我试图掌握 libcurl 的窍门,当我每次运行我的脚本时,它都会被重定向回登录。我正在尝试登录谷歌。我认为这是因为我错误地格式化了帖子。我只是在寻找有关如何格式化它的想法,因为email=testUser&password=testpass
它不起作用。我在下面有最小的代码。
我的主要
int main() {
ReadClassroom classroom;
classroom.username = "user";
classroom.psswrd = "pass";
if (classroom.start()) {
std::cout << "Finished" << std::endl;
}
else
std::cout << "cancelling" << std::endl;
while (1) {
//don't close
}
}
类.h
class ReadClassroom
{
public:
std::string username;
std::string psswrd;
bool start();
bool downloadMainHtml();
private:
//html file
std::string htmlfile;
std::string fileLoc_links;
std::string finishedUrl;
//cookies
std::string cookieFile;
//for loging in
std::string username_login;
std::string psswrd_login;
std::string login;
const char* loginPtr = const_cast<char*>(login.c_str());
};
类.cpp
bool ReadClassroom::start() {
//init
fileLoc_links = std::string("Classroom/test.h");
finishedUrl = std::string("https://classroom.google.com/u/0/c/NDI3NzUxMjc3ODJa");
htmlfile = std::string("Classroom/test.html");
cookieFile = std::string("Classroom/test.cookiejar");
username_login = "email=" + username;
psswrd_login = "&password=" + psswrd;
login = (username_login + psswrd_login);
std::cout << "downloading classrooms html" << std::endl;
if (downloadMainHtml())
std::cout << "downloaded classrooms html" << std::endl;
else {
std::cout << "download failed" << std::endl;
return false;
}
std::cout << "finished" << std::endl;
}
bool ReadClassroom::downloadMainHtml() {
curl_global_init(CURL_GLOBAL_ALL);
CURL* curl;
CURLcode result;
curl = curl_easy_init();
if (curl) {
//create cookie file
std::ofstream cookie(cookieFile);
cookie << std::endl;
cookie.close();
//init
curl_easy_setopt(curl, CURLOPT_USERAGENT, "Mozilla/4.0");
curl_easy_setopt(curl, CURLOPT_AUTOREFERER, 1);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(curl, CURLOPT_COOKIEJAR, cookieFile);
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookieFile);
//go to login link
curl_easy_setopt(curl, CURLOPT_URL, "https://www.google.com/accounts/ServiceLoginAuth");
//save and reload cookies
curl_easy_setopt(curl, CURLOPT_COOKIELIST, "FLUSH");
curl_easy_setopt(curl, CURLOPT_COOKIELIST, "RELOAD");
//visit webpage and load cookies and get return code
result = curl_easy_perform(curl);
if (result != CURLE_OK) {
curl_easy_cleanup(curl);
std::cout << "failed on cookies" << std::endl << "Curl error code: " << result << std::endl;
return false;
}
//define login info
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, loginPtr);
//login and get return code
result = curl_easy_perform(curl);
if (result != CURLE_OK) {
curl_easy_cleanup(curl);
std::cout << "failed on login" << std::endl << "Curl error code: " << result << std::endl;
return false;
}
curl_easy_setopt(curl, CURLOPT_POST, 0);
//go to google classroom
curl_easy_setopt(curl, CURLOPT_URL, finishedUrl);
//execute and get return code
result = curl_easy_perform(curl);
if (result != CURLE_OK) {
curl_easy_cleanup(curl);
std::cout << "failed after login" << std::endl << "Curl error code: " << result << std::endl;
std::cout << finishedUrl << std::endl;
return false;
}
}
curl_easy_cleanup(curl);
return true;
}
解决方案
推荐阅读
- python - 是否有可能在升级python之后,所有以前安装的第三方模块都会在新版本中自动可用?
- python - 带有 for 循环和 if 语句的 Python 函数不能正常工作
- mysql - 在mySQL中复制同一张表中的一些记录
- solr - 在 q/fq 中添加字段名称时,Solr 同义词不起作用
- python - 翻译超过 10 亿个字符的文本 - 消除瓶颈并降低价格
- css - 获取 Mapbox 绘制形状为 CSS
- mongodb - MongoDB独立地从集合中聚合两个字段并计数
- excel - 将公式拖过去但保留字母并更改数字
- symfony - Doctrine QueryBuilder - ManyToMany - NOT IN - 如何仅过滤关系不包含 ID 的实体?
- c++ - K-Nearest Neighbors 程序总是报告相同的类值