简体   繁体   中英

login to google with libcurl in C

I try to login google using the libcurl library in C. But google answers me : "Your browser's cookie functionality is turned off. Please turn it on."

Here is the code :

#include <stdio.h>
#include <stdlib.h>
#include <curl/curl.h>

size_t get_buffer(void *buffer, size_t size, size_t nmemb, void *userp); 

int main(void)
{
  CURL *curl;
  CURLcode res;
  char *data="service=...&dsh=...&GALX=...&pstMsg=0&dnCon=&checkConnection=&Email=...&Passwd=...";   

  curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL, "https://accounts.google.com/ServiceLoginAuth");
    curl_easy_setopt(curl, CURLOPT_POST, 1);
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
    curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "cookie");
    curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "cookie"); 
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, get_buffer);
    res = curl_easy_perform(curl);


    curl_easy_cleanup(curl);
  }
  return 0;
}

Any idea ? Thanks.

You need to call the web page twice to get the cookie. Please see:

http://www.hackthissite.org/articles/read/1078

There's your answer @ "Retrieving a webpage (With cookies!):"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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