Hi everyone,
I wanted to write C++ a program that's going to logon 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 visits the logon page to optain a phpsesid cookie and the authenticity token, then it creates a post request wih the authenticity token, username and password and sends it. The only problem is that instead of logging in it returns the logon page again, I've tested it with hackthissite.org and it worked perfectly. Does anyone know how to fix this ?
Thanks!

Recommended Answers

All 2 Replies

I assume you have actually checked out the Twitter API?

no, I haven't

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.