Skip to content

Instantly share code, notes, and snippets.

@BroVic
Forked from komasaru/Twitcurl.cpp
Created January 18, 2018 10:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BroVic/cac3e8893ae55b89766d0dcbe758681f to your computer and use it in GitHub Desktop.
Save BroVic/cac3e8893ae55b89766d0dcbe758681f to your computer and use it in GitHub Desktop.
C++ source code to tweet by twitcurl library.
/**
* Tweet by twitcurl
*/
#include <iostream>
#include <string>
#include <twitcurl.h>
using namespace std;
/*
* [CLASS] Process
*/
class Proc
{
twitCurl twitterObj;
string strConsumerKey, strConsumerSecret;
string strAccessTokenKey, strAccessTokenSecret;
string strReplyMsg;
public:
Proc(); // Constructor
bool execMain(string); // Main Process
};
/*
* Proc - Constructor
*/
Proc::Proc()
{
/// Initialize
strConsumerKey = "your_consumer_key";
strConsumerSecret = "your_consumer_secret";
strAccessTokenKey = "your_access_token";
strAccessTokenSecret = "your_access_token_secret";
}
/*
* Main Process
*/
bool Proc::execMain(string strString)
{
try {
// Set Twitter consumer key and secret,
// OAuth access token key and secret
twitterObj.getOAuth().setConsumerKey(strConsumerKey);
twitterObj.getOAuth().setConsumerSecret(strConsumerSecret);
twitterObj.getOAuth().setOAuthTokenKey(strAccessTokenKey);
twitterObj.getOAuth().setOAuthTokenSecret(strAccessTokenSecret);
// Verify account credentials
if (!twitterObj.accountVerifyCredGet()) {
twitterObj.getLastCurlError(strReplyMsg);
cerr << "\ntwitCurl::accountVerifyCredGet error:\n"
<< strReplyMsg.c_str() << endl;
return false;
}
// Post a message
strReplyMsg = "";
if (!twitterObj.statusUpdate(strString)) {
twitterObj.getLastCurlError(strReplyMsg);
cerr << "\ntwitCurl::statusUpdate error:\n"
<< strReplyMsg.c_str() << endl;
return false;
}
} catch (char *e) {
cerr << "[EXCEPTION] " << e << endl;
return false;
}
return true;
}
/*
* Execution
*/
int main(int argc, char* argv[]){
try {
if (argc != 2) {
cout << "Usage: ./Twitcurl sentence" << endl;
return true;
}
Proc objMain;
bool bRet = objMain.execMain(argv[1]);
if (!bRet) cout << "ERROR!" << endl;
} catch (char *e) {
cerr << "[EXCEPTION] " << e << endl;
return 1;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment