This is getting worse, I am playing with curl more than I should be. This time, I would like to show a snippet of code that demonstrate basic http authentication. You might want to take a look at my previous code for the complete listing. The following code fetches the last 20 direct messages from your twitter.
43 44 45 46 47 48 49 50 51 52 53 54 55 | curl_global_init(CURL_GLOBAL_ALL); std::string sAuth(""); sAuth += sUser + ":" + sPass; hCurl = curl_easy_init(); hResult = curl_easy_setopt(hCurl, CURLOPT_URL, "http://twitter.com/direct_messages.xml"); hResult = curl_easy_setopt(hCurl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); hResult = curl_easy_setopt(hCurl, CURLOPT_USERPWD, sAuth.c_str()); hResult = curl_easy_perform(hCurl); if(CURLE_OK != hResult) { std::cout << "May be twitter is on a whale hunt" << std::endl; } curl_easy_cleanup(hCurl); curl_global_cleanup(); |
I can justify why I had decided to post it. I had tried with base64 encoding to convert a user/password and add it as a custom header field in the curl call. Had I had it known that libcurl supports basic authentication out of the box, I would have saved lot of time. I am sure there would be some hapless soul like me, that would head down my route. Who knows, may be I would completely forget about this and try the same path again. But; sure next time, I hope google will land me on my own blog.











