title: libcurl formating post for login to google
body: <p>So I'm trying to get the hang of libcurl and when every i run my script it gets redirected back to the login. I'm trying to login to google. I think this is happening because i'm formatting the post incorrectly. I'm just looking for ideas on how to format it because <code>email=testUser&amp;password=testpass</code> doesn't work. I have the minimal code down bellow.</p>
  
  <p>My main</p>
  
  <pre class="lang-cpp prettyprint-override"><code>int main() {
      ReadClassroom classroom;
      classroom.username = "user";
      classroom.psswrd = "pass";
  
      if (classroom.start()) {
          std::cout &lt;&lt; "Finished" &lt;&lt; std::endl;
      }
      else
          std::cout &lt;&lt; "cancelling" &lt;&lt; std::endl;
  
      while (1) {
          //don't close
      }
  }
  </code></pre>
  
  <p>class.h</p>
  
  <pre class="lang-cpp prettyprint-override"><code>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&lt;char*&gt;(login.c_str());
  };
  </code></pre>
  
  <p>class.cpp</p>
  
  <pre class="lang-cpp prettyprint-override"><code>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 = "&amp;password=" + psswrd;
      login = (username_login + psswrd_login);
  
      std::cout &lt;&lt; "downloading classrooms html" &lt;&lt; std::endl;
      if (downloadMainHtml())
          std::cout &lt;&lt; "downloaded classrooms html" &lt;&lt; std::endl;
      else {
          std::cout &lt;&lt; "download failed" &lt;&lt; std::endl;
          return false;
      }
      std::cout &lt;&lt; "finished" &lt;&lt; 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 &lt;&lt; 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 &lt;&lt; "failed on cookies" &lt;&lt; std::endl &lt;&lt; "Curl error code: " &lt;&lt; result &lt;&lt; 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 &lt;&lt; "failed on login" &lt;&lt; std::endl &lt;&lt; "Curl error code: " &lt;&lt; result &lt;&lt; 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 &lt;&lt; "failed after login" &lt;&lt; std::endl &lt;&lt; "Curl error code: " &lt;&lt; result &lt;&lt; std::endl;
              std::cout &lt;&lt; finishedUrl &lt;&lt; std::endl;
              return false;
          }
      }
      curl_easy_cleanup(curl);
      return true;
  }
  </code></pre>