首页 > 解决方案 > 用于登录谷歌的 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;
}

标签: c++libcurl

解决方案


推荐阅读