Skip to content

Commit

Permalink
Adjust.
Browse files Browse the repository at this point in the history
Signed-off-by: godai_0519 <godai0519@gmail.com>
  • Loading branch information
godai0519 committed Dec 15, 2012
1 parent c44c2dd commit a8a04ce
Show file tree
Hide file tree
Showing 20 changed files with 342 additions and 850 deletions.
33 changes: 10 additions & 23 deletions include/twit-library/client/common_v1.hpp
Expand Up @@ -5,8 +5,8 @@
// OAuth1.0用,定義リスト
//

#ifndef TWIT_LIB_COMMON_V1
#define TWIT_LIB_COMMON_V1
#ifndef TWIT_LIB_COMMON_V1_HPP
#define TWIT_LIB_COMMON_V1_HPP

#include "../detail/oauth_version1.hpp"

Expand All @@ -16,31 +16,18 @@ namespace client{
template<class URL_Set>
class common_v1: public oauth::detail::oauth_version1{
public:
common_v1(boost::shared_ptr<Key_Type> &key,boost::shared_ptr<bstcon::client> &client)
: oauth_version1(key,client)
{
}
virtual ~common_v1(){}
common_v1(boost::shared_ptr<Key_Type> &key,boost::shared_ptr<bstcon::client> &client);
virtual ~common_v1();

virtual void get_request_token()
{
oauth_version1::get_request_token(
URL_Set::get_request_method(),
client_->service_protocol()+"://"+URL_Set::get_host()+URL_Set::get_request_path()
);
}

virtual void get_access_token(const std::string& pin_code)
{
oauth_version1::get_access_token(
URL_Set::get_access_method(),
client_->service_protocol()+"://"+URL_Set::get_host()+URL_Set::get_access_path(),
pin_code
);
}
virtual void get_request_token();
virtual void get_access_token(const std::string& pin_code);
};

} // namespace client
} // namespace oauth

#ifdef BOOSTCONNECT_LIB_BUILD
#include "impl/common_v1.ipp"
#endif

#endif
110 changes: 19 additions & 91 deletions include/twit-library/client/twitter.hpp
Expand Up @@ -5,120 +5,48 @@
// Twitter URL Listとその実用化
//

#ifndef TWIT_LIB_TWITTER
#define TWIT_LIB_TWITTER
#ifndef TWIT_LIB_TWITTER_HPP
#define TWIT_LIB_TWITTER_HPP

#include <string>
#include <boost/shared_ptr.hpp>
#include "common_v1.hpp"

namespace oauth{

struct twitter_set{
static std::string get_host()
{
return "api.twitter.com";
}
static std::string get_request_method()
{
return "POST";
}
static std::string get_request_path()
{
return "/oauth/request_token";
}
static std::string get_access_method()
{
return "POST";
}
static std::string get_access_path()
{
return "/oauth/access_token";
}
static std::string get_host();
static std::string get_request_method();
static std::string get_request_path();
static std::string get_access_method();
static std::string get_access_path();
};

class twitter : public oauth::client::common_v1<oauth::twitter_set>{
typedef oauth::client::common_v1<oauth::twitter_set> MyParent;
typedef oauth::twitter_set URL_Set;
public:
twitter(boost::shared_ptr<Key_Type> &key,boost::shared_ptr<bstcon::client> &client): MyParent(key,client){}
virtual ~twitter(){}
twitter(boost::shared_ptr<Key_Type> &key,boost::shared_ptr<bstcon::client> &client);
virtual ~twitter();

std::string get_user_id() const { return user_id_; }
std::string get_screen_name() const { return screen_name_; }
std::string get_user_id() const;
std::string get_screen_name() const;

#ifdef USE_SSL_BOOSTCONNECT
void get_xauth_token(const std::string& id, const std::string& password, GetTokenHandler handler = [](const boost::shared_ptr<bstcon::response>,const boost::system::error_code&)->void{})
{
Param_Type params = boost::assign::map_list_of
("oauth_consumer_key",key_->get_consumer_key())
("oauth_signature_method","HMAC-SHA1")
("oauth_timestamp",oauth::utility::get_timestamp())
("oauth_nonce",oauth::utility::nonce())
("oauth_callback","oob")
("oauth_version","1.0")
("x_auth_password",password)
("x_auth_username",id)
("x_auth_mode","client_auth");

Param_Type xauth_params = boost::assign::map_list_of
("x_auth_password",password)
("x_auth_username",id)
("x_auth_mode","client_auth");

boost::assign::insert(params)
("oauth_signature",signature_(URL_Set::get_access_method(),client_->service_protocol()+"://"+URL_Set::get_host()+URL_Set::get_access_path(),key_->get_signature_key(),params));

params.erase("x_auth_password");
params.erase("x_auth_username");
params.erase("x_auth_mode");

const std::string body = oauth::utility::get_urlencoded(xauth_params);

boost::shared_ptr<boost::asio::streambuf> buf(new boost::asio::streambuf());
{
std::ostream os(buf.get());
os << URL_Set::get_access_method() << " " << URL_Set::get_access_path() << " HTTP/1.1" << "\r\n";
os << "Host: " << URL_Set::get_host() << "\r\n";
os << "Content-Type: " << "application/x-www-form-urlencoded" << "\r\n";
os << "Content-Length: " << body.length() << "\r\n";
os << "Authorization: " << "OAuth " << oauth::utility::get_authorization_field(params) << "\r\n\r\n";
os << body;
}

(*client_)(
URL_Set::get_host(),
[buf,handler,this](bstcon::client::connection_ptr connection, boost::system::error_code ec)
{
connection->send(buf,boost::bind(&twitter::set_access_token,this,_1,_2,handler));
});

//client_->operator() (URL_Set::get_host(),buf,
// boost::bind(&twitter::set_access_token,this,_1,_2,handler));

return;
}
void get_xauth_token(const std::string& id, const std::string& password, GetTokenHandler handler = [](const boost::shared_ptr<bstcon::response>,const boost::system::error_code&)->void{});
#endif

protected:
void set_access_token(const boost::shared_ptr<bstcon::response> response,const boost::system::error_code& ec,GetTokenHandler handler)
{
if(ec) return;
if(200 <= response->status_code && response->status_code < 300)
{
const Param_Type parsed = oauth::utility::parse_urlencoded(response->body);
key_->set_access_token (oauth::utility::percent_decode(parsed.at("oauth_token")));
key_->set_access_secret(oauth::utility::percent_decode(parsed.at("oauth_token_secret")));
user_id_ = oauth::utility::percent_decode(parsed.at("user_id"));
screen_name_ = oauth::utility::percent_decode(parsed.at("screen_name"));
}
handler(response,ec);

return;
}
void set_access_token(const boost::shared_ptr<bstcon::response> response,const boost::system::error_code& ec,GetTokenHandler handler);

std::string user_id_;
std::string screen_name_;
};

} // namespace oauth

#ifdef BOOSTCONNECT_LIB_BUILD
#include "impl/twitter.ipp"
#endif

#endif
34 changes: 12 additions & 22 deletions include/twit-library/client/yahoo.hpp
Expand Up @@ -5,38 +5,28 @@
// Yahoo URL Listとその実用化
//

#ifndef TWIT_LIB_YAHOO
#define TWIT_LIB_YAHOO
#ifndef TWIT_LIB_YAHOO_HPP
#define TWIT_LIB_YAHOO_HPP

#include <string>
#include "common_v1.hpp"

namespace oauth{

struct yahoo_set{
static std::string get_host()
{
return "auth.login.yahoo.co.jp";
}
static std::string get_request_method()
{
return "POST";
}
static std::string get_request_path()
{
return "/oauth/v2/get_request_token";
}
static std::string get_access_method()
{
return "POST";
}
static std::string get_access_path()
{
return "/oauth/v2/get_token";
}
static std::string get_host();
static std::string get_request_method();
static std::string get_request_path();
static std::string get_access_method();
static std::string get_access_path();
};

typedef oauth::client::common_v1<oauth::yahoo_set> yahoo;

} // namespace oauth

#ifdef BOOSTCONNECT_LIB_BUILD
#include "impl/yahoo.ipp"
#endif

#endif

0 comments on commit a8a04ce

Please sign in to comment.