Question

I wanted to write C++ a program that's going to log on to my twitter and alert me if I have any new messages or tweets. I found an article that explains how to log on to a website with libcurl, https://www.hackthissite.org/articles/read/1078. Using that article I've written a simple program :

#include <ios>
#include <sstream>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <curl/curl.h>
#include <string.h>
#include <string>
#include <fstream>

using namespace std;

size_t write_to_string(void *ptr, size_t size, size_t count, void *stream) {
((string*)stream)->append((char*)ptr, 0, size*count);
return size*count;
}



/*this function logs on to the website */
int login(char username[24],char password[24])
{   

curl_global_init( CURL_GLOBAL_ALL );
CURL * myHandle = curl_easy_init ( );
// Set some initial paramaters

curl_easy_setopt(myHandle, CURLOPT_USERAGENT, "Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7A341 Safari/528.16");
curl_easy_setopt(myHandle, CURLOPT_AUTOREFERER, 1 );
curl_easy_setopt(myHandle, CURLOPT_FOLLOWLOCATION, 1 );
curl_easy_setopt(myHandle, CURLOPT_COOKIEFILE, "");


//turn the output in to a string using a function
string response;
curl_easy_setopt(myHandle, CURLOPT_WRITEFUNCTION, write_to_string);
curl_easy_setopt(myHandle, CURLOPT_WRITEDATA, &response);


// Visit the login page once to obtain a PHPSESSID cookie
curl_easy_setopt(myHandle, CURLOPT_URL, "https://mobile.twitter.com/session/new");
curl_easy_perform( myHandle );


    //find the authenticity token in the html 
    size_t pos1 = response.find("type=\"hidden\" value=\"");
    size_t pos2 = response.find("/></span>");

     string auth_token = response.substr(pos1,pos2-pos1);

    auth_token.erase (1,21);
    auth_token.erase (20,2);


    //create a post request

stringstream postreq;
postreq << "authenticity_token=" << auth_token;
postreq << "&username=" << username;
postreq << "&password=" << password;


cout << postreq.str() << endl;

//convert the string into an array
char *post_request_gen = (char*)malloc(248);
strcpy(post_request_gen, postreq.str().c_str());    



//post mode
curl_easy_setopt(myHandle, CURLOPT_POST, 1 );

//turn the output in to a string using a function
string response2;
    curl_easy_setopt(myHandle, CURLOPT_WRITEFUNCTION, write_to_string);
    curl_easy_setopt(myHandle, CURLOPT_WRITEDATA, &response2);


curl_easy_setopt(myHandle, CURLOPT_REFERER, "https://mobile.twitter.com/session/new");

curl_easy_setopt(myHandle, CURLOPT_POSTFIELDS, post_request_gen);
curl_easy_perform( myHandle );
curl_easy_cleanup( myHandle );

//output the HTML
cout << response2;

//find out if the account works by searching a keyword only present after loging in
string word1 = "logout";
size_t found = response2.find(word1);
if (found!=std::string::npos) 
cout << "Account works";

//This is where I'll create a function that's going to check for tweets or messages



else 
cout << "Wrong username or password" << "\n";
return 0;
}


int main ()
{


login("username", "password");



return 0;
}

It downloads the log on page, extracts the authenticity token and puts it in to the post request with the username and password and sends it. The only problem is that instead of logging in it returns the log on page again. It logs on to hackthissite.org, so I's there any way to log on to twitter using libcurl ??

Était-ce utile?

La solution

You need to set the header cookies for the second request. You must get the cookies returned from the first request and set them in the second. To send a cookie, you must enable cookies and then set them:

curl_easy_setopt(myHandle, CURLOPT_COOKIEFILE, "");
curl_easy_setopt(myHandle, CURLOPT_COOKIE, "key1=value1; key2=value2;"

To parse the header, you may use the CURLOPT_HEADERFUNCTION to curl_easy_setopt passing a function with this signature: size_t function( void *ptr, size_t size, size_t nmemb, void *userdata); and parse the block (all headers will be in ptr, following the HTTP standard, i.e., Header: value\r\n), find the Cookie header and pass to the second request.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top