moved TwitterClient to NetSSL samples, use OAuth10Credentials class

This commit is contained in:
Guenter Obiltschnig
2014-11-10 22:54:05 +01:00
parent 362eaad5cb
commit 9d7a814292
27 changed files with 5296 additions and 0 deletions

View File

@@ -0,0 +1,155 @@
//
// TwitterApp.cpp
//
// $Id: //poco/1.4/Net/samples/TwitterClient/src/TweetApp.cpp#2 $
//
// A very simple command-line Twitter client.
//
// Copyright (c) 2009-2013, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "Poco/Util/Application.h"
#include "Poco/Util/Option.h"
#include "Poco/Util/OptionSet.h"
#include "Poco/Util/HelpFormatter.h"
#include "Twitter.h"
#include <iostream>
using Poco::Util::Application;
using Poco::Util::Option;
using Poco::Util::OptionSet;
using Poco::Util::HelpFormatter;
using Poco::Util::AbstractConfiguration;
using Poco::Util::OptionCallback;
class TweetApp: public Application
/// A very simple Twitter command-line client.
{
public:
TweetApp()
{
}
protected:
void defineOptions(OptionSet& options)
{
Application::defineOptions(options);
options.addOption(
Option("help", "h", "Display help information on command line arguments.")
.required(false)
.repeatable(false)
.callback(OptionCallback<TweetApp>(this, &TweetApp::handleHelp)));
options.addOption(
Option("message", "m", "Specify the status message to post.")
.required(true)
.repeatable(false)
.argument("message")
.callback(OptionCallback<TweetApp>(this, &TweetApp::handleMessage)));
options.addOption(
Option("ckey", "c", "Specify the Twitter consumer key.")
.required(true)
.repeatable(false)
.argument("consumer key")
.callback(OptionCallback<TweetApp>(this, &TweetApp::handleConsumerKey)));
options.addOption(
Option("csecret", "s", "Specify the Twitter consumer secret.")
.required(true)
.repeatable(false)
.argument("consumer secret")
.callback(OptionCallback<TweetApp>(this, &TweetApp::handleConsumerSecret)));
options.addOption(
Option("token", "t", "Specify the Twitter access token.")
.required(true)
.repeatable(true)
.argument("access token")
.callback(OptionCallback<TweetApp>(this, &TweetApp::handleAccessToken)));
options.addOption(
Option("tsecret", "S", "Specify the Twitter access token secret.")
.required(true)
.repeatable(true)
.argument("access token secret")
.callback(OptionCallback<TweetApp>(this, &TweetApp::handleAccessTokenSecret)));
}
void handleHelp(const std::string& name, const std::string& value)
{
displayHelp();
stopOptionsProcessing();
}
void handleConsumerKey(const std::string& name, const std::string& value)
{
_consumerKey = value;
}
void handleConsumerSecret(const std::string& name, const std::string& value)
{
_consumerSecret = value;
}
void handleAccessToken(const std::string& name, const std::string& value)
{
_accessToken = value;
}
void handleAccessTokenSecret(const std::string& name, const std::string& value)
{
_accessTokenSecret = value;
}
void handleMessage(const std::string& name, const std::string& value)
{
_message = value;
}
void displayHelp()
{
HelpFormatter helpFormatter(options());
helpFormatter.setCommand(commandName());
helpFormatter.setUsage("OPTIONS");
helpFormatter.setHeader("A simple Twitter command line client for posting status updates.");
helpFormatter.format(std::cout);
}
int main(const std::vector<std::string>& args)
{
try
{
if (!_message.empty())
{
Twitter twitter;
twitter.login(_consumerKey, _consumerSecret, _accessToken, _accessTokenSecret);
Poco::Int64 statusId = twitter.update(_message);
std::cout << statusId << std::endl;
}
}
catch (Poco::Exception& exc)
{
std::cerr << exc.displayText() << std::endl;
return Application::EXIT_SOFTWARE;
}
return Application::EXIT_OK;
}
private:
std::string _consumerKey;
std::string _consumerSecret;
std::string _accessToken;
std::string _accessTokenSecret;
std::string _message;
};
POCO_APP_MAIN(TweetApp)

View File

@@ -0,0 +1,98 @@
//
// Twitter.cpp
//
// $Id: //poco/1.4/Net/samples/TwitterClient/src/Twitter.cpp#2 $
//
// A C++ implementation of a Twitter client based on the POCO Net library.
//
// Copyright (c) 2009-2013, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "Twitter.h"
#include "Poco/Net/HTTPSClientSession.h"
#include "Poco/Net/HTTPRequest.h"
#include "Poco/Net/HTTPResponse.h"
#include "Poco/Net/OAuth10Credentials.h"
#include "Poco/Util/JSONConfiguration.h"
#include "Poco/URI.h"
#include "Poco/Format.h"
#include "Poco/StreamCopier.h"
const std::string Twitter::TWITTER_URI("https://api.twitter.com/1.1/statuses/");
Twitter::Twitter():
_uri(TWITTER_URI)
{
}
Twitter::Twitter(const std::string& twitterURI):
_uri(twitterURI)
{
}
Twitter::~Twitter()
{
}
void Twitter::login(const std::string& consumerKey, const std::string& consumerSecret, const std::string& token, const std::string& tokenSecret)
{
_consumerKey = consumerKey;
_consumerSecret = consumerSecret;
_token = token;
_tokenSecret = tokenSecret;
}
Poco::Int64 Twitter::update(const std::string& status)
{
Poco::Net::HTMLForm form;
form.set("status", status);
Poco::AutoPtr<Poco::Util::AbstractConfiguration> pResult = invoke(Poco::Net::HTTPRequest::HTTP_POST, "update", form);
return pResult->getInt64("id", 0);
}
Poco::AutoPtr<Poco::Util::AbstractConfiguration> Twitter::invoke(const std::string& httpMethod, const std::string& twitterMethod, Poco::Net::HTMLForm& form)
{
// Create the request URI.
// We use the JSON version of the Twitter API.
Poco::URI uri(_uri + twitterMethod + ".json");
Poco::Net::HTTPSClientSession session(uri.getHost(), uri.getPort());
Poco::Net::HTTPRequest req(httpMethod, uri.getPath(), Poco::Net::HTTPMessage::HTTP_1_1);
// Sign request
Poco::Net::OAuth10Credentials creds(_consumerKey, _consumerSecret, _token, _tokenSecret);
creds.authenticate(req, uri, form);
// Send the request.
form.prepareSubmit(req);
std::ostream& ostr = session.sendRequest(req);
form.write(ostr);
// Receive the response.
Poco::Net::HTTPResponse res;
std::istream& rs = session.receiveResponse(res);
Poco::AutoPtr<Poco::Util::JSONConfiguration> pResult = new Poco::Util::JSONConfiguration(rs);
// If everything went fine, return the JSON document.
// Otherwise throw an exception.
if (res.getStatus() == Poco::Net::HTTPResponse::HTTP_OK)
{
return pResult;
}
else
{
throw Poco::ApplicationException("Twitter Error", pResult->getString("errors[0].message", ""));
}
}

View File

@@ -0,0 +1,73 @@
//
// Twitter.h
//
// $Id: //poco/1.4/Net/samples/TwitterClient/src/Twitter.h#2 $
//
// A C++ implementation of a Twitter client based on the POCO Net library.
//
// Copyright (c) 2009-2013, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef Twitter_INCLUDED
#define Twitter_INCLUDED
#include "Poco/Poco.h"
#include "Poco/Net/HTMLForm.h"
#include "Poco/Util/AbstractConfiguration.h"
#include "Poco/AutoPtr.h"
class Twitter
/// A simple implementation of a Twitter API client
/// (see <http://dev.twitter.com> for more information).
///
/// Currently, only the update message is supported.
{
public:
Twitter();
/// Creates the Twitter object, using
/// the default Twitter API URI (<http://api.twitter.com/1.1/statuses/>).
Twitter(const std::string& twitterURI);
/// Creates the Twitter object using the given Twitter API URI.
~Twitter();
/// Destroys the Twitter object.
void login(const std::string& consumerKey, const std::string& consumerSecret, const std::string& token, const std::string& tokenSecret);
/// Specifies the OAuth authentication information used in all API calls.
Poco::Int64 update(const std::string& status);
/// Updates the user's status.
///
/// Returns the ID of the newly created status.
Poco::AutoPtr<Poco::Util::AbstractConfiguration> invoke(const std::string& httpMethod, const std::string& twitterMethod, Poco::Net::HTMLForm& params);
/// Invokes the given method of the Twitter API, using the parameters
/// given in the Poco::Net::HTMLForm object. httpMethod must be GET or POST,
/// according to the Twitter API documentation.
///
/// Returns a Poco::Util::JSONConfiguration with the server's response if the
/// server's HTTP response status code is 200 (OK).
/// Otherwise, throws a Poco::ApplicationException.
static const std::string TWITTER_URI;
private:
Twitter(const Twitter&);
Twitter& operator = (const Twitter&);
std::string _uri;
std::string _consumerKey;
std::string _consumerSecret;
std::string _token;
std::string _tokenSecret;
};
#endif // Twitter_INCLUDED