diff --git a/src/cocoatweet/api/model/bearerToken.cc b/src/cocoatweet/api/model/bearerToken.cc new file mode 100644 index 0000000..01920f1 --- /dev/null +++ b/src/cocoatweet/api/model/bearerToken.cc @@ -0,0 +1,14 @@ +#include "bearerToken.h" + +namespace CocoaTweet::API::Model { + +BearerToken::BearerToken() : token_("") {} +BearerToken::BearerToken(const std::string _token) + : token_(_token) {} +const std::string& BearerToken::token() const { + return token_; +} +void BearerToken::token(const std::string& _token) { + token_ = _token; +} +} // namespace CocoaTweet::API::Model \ No newline at end of file diff --git a/src/cocoatweet/api/model/bearerToken.h b/src/cocoatweet/api/model/bearerToken.h new file mode 100644 index 0000000..5ac023f --- /dev/null +++ b/src/cocoatweet/api/model/bearerToken.h @@ -0,0 +1,17 @@ +#ifndef COCOATWEET_API_MODEL_BEARERTOKEN_H_ +#define COCOATWEET_API_MODEL_BEARERTOKEN_H_ +#include + +namespace CocoaTweet::API::Model { +class BearerToken { + std::string token_; + +public: + BearerToken(); + BearerToken(const std::string _token); + const std::string& token() const; + void token(const std::string& _token); +}; +} // namespace CocoaTweet::API::Model + +#endif \ No newline at end of file diff --git a/src/cocoatweet/api/oauth1/accessToken.cc b/src/cocoatweet/api/oauth1/accessToken.cc index 3deb982..49d96f7 100644 --- a/src/cocoatweet/api/oauth1/accessToken.cc +++ b/src/cocoatweet/api/oauth1/accessToken.cc @@ -2,6 +2,7 @@ #include #include +#include namespace CocoaTweet::API::OAuth1 { AccessToken::AccessToken() { contentType_ = "application/x-www-form-urlencoded"; @@ -18,8 +19,16 @@ void AccessToken::oauthToken(const CocoaTweet::API::Model::OAuthToken _token) { const CocoaTweet::API::Model::OAuthToken AccessToken::process( std::weak_ptr _oauth) { + + auto key = oauth_.lock()->key(); + key.authType(CocoaTweet::Authentication::Key::AUTH_TYPE::OAUTH10A); + key.accessToken(oauthToken_.oauthToken()); + key.accessTokenSecret(oauthToken_.oauthTokenSecret()); + auto oauth = std::make_shared(key); + + CocoaTweet::API::Model::OAuthToken oauthToken; - HttpPost::process(_oauth, [&oauthToken](const std::string& _rcv) { + HttpPost::process(oauth, [&oauthToken](const std::string& _rcv) { auto mp = CocoaTweet::Util::parse(_rcv, '&', '='); if (mp.count("oauth_token")) { oauthToken.oauthToken(mp.at("oauth_token")); diff --git a/src/cocoatweet/api/oauth1/oauth.cc b/src/cocoatweet/api/oauth1/oauth.cc index c0b8130..293aa9c 100644 --- a/src/cocoatweet/api/oauth1/oauth.cc +++ b/src/cocoatweet/api/oauth1/oauth.cc @@ -8,13 +8,13 @@ OAuth::OAuth(std::shared_ptr _oau CocoaTweet::API::Model::OAuthToken OAuth::requestToken( const std::string& _oauthCallback) const { - auto key = oauth_.lock()->key(); - key.authType(CocoaTweet::Authentication::Key::AUTH_TYPE::OAUTH10A); - auto oauth = std::make_shared(key); +// auto key = oauth_.lock()->key(); +// key.authType(CocoaTweet::Authentication::Key::AUTH_TYPE::OAUTH10A); +// auto oauth = std::make_shared(key); CocoaTweet::API::OAuth1::RequestToken requestToken; requestToken.oauthCallback(_oauthCallback); - return requestToken.process(oauth); + return requestToken.process(oauth_); } const std::string OAuth::authorize(const CocoaTweet::API::Model::OAuthToken _oauthToken) const { @@ -25,13 +25,13 @@ const std::string OAuth::authorize(const CocoaTweet::API::Model::OAuthToken _oau const CocoaTweet::API::Model::OAuthToken OAuth::accessToken( const CocoaTweet::API::Model::OAuthToken _oauthToken, const std::string _verifier) const { - auto key = oauth_.lock()->key(); - key.authType(CocoaTweet::Authentication::Key::AUTH_TYPE::OAUTH10A); - key.accessToken(_oauthToken.oauthToken()); - key.accessTokenSecret(_oauthToken.oauthTokenSecret()); - auto oauth = std::make_shared(key); +// auto key = oauth_.lock()->key(); +// key.authType(CocoaTweet::Authentication::Key::AUTH_TYPE::OAUTH10A); +// key.accessToken(_oauthToken.oauthToken()); +// key.accessTokenSecret(_oauthToken.oauthTokenSecret()); +// auto oauth = std::make_shared(key); CocoaTweet::API::OAuth1::AccessToken accessToken; accessToken.oauthVerifier(_verifier); - return accessToken.process(oauth); + return accessToken.process(oauth_); } } // namespace CocoaTweet::API::OAuth1 diff --git a/src/cocoatweet/api/oauth1/requestToken.cc b/src/cocoatweet/api/oauth1/requestToken.cc index 44fd54e..680c453 100644 --- a/src/cocoatweet/api/oauth1/requestToken.cc +++ b/src/cocoatweet/api/oauth1/requestToken.cc @@ -1,6 +1,7 @@ #include #include #include +#include namespace CocoaTweet::API::OAuth1 { RequestToken::RequestToken() { @@ -14,8 +15,12 @@ void RequestToken::oauthCallback(const std::string& _oauthCallback) { CocoaTweet::API::Model::OAuthToken RequestToken::process( std::weak_ptr _oauth) { + auto key = oauth_.lock()->key(); + key.authType(CocoaTweet::Authentication::Key::AUTH_TYPE::OAUTH10A); + auto oauth = std::make_shared(key); + CocoaTweet::API::Model::OAuthToken oauthToken; - HttpPost::process(_oauth, [&oauthToken](const std::string& _rcv) { + HttpPost::process(oauth, [&oauthToken](const std::string& _rcv) { auto mp = CocoaTweet::Util::parse(_rcv, '&', '='); if (mp.count("oauth_token")) { oauthToken.oauthToken(mp.at("oauth_token")); diff --git a/src/cocoatweet/api/oauth2/invalidateToken.cc b/src/cocoatweet/api/oauth2/invalidateToken.cc index e69de29..4a38f01 100644 --- a/src/cocoatweet/api/oauth2/invalidateToken.cc +++ b/src/cocoatweet/api/oauth2/invalidateToken.cc @@ -0,0 +1,30 @@ +#include +#include "nlohmann/json.hpp" + +namespace CocoaTweet::API::OAuth2 { +InvalidateToken::InvalidateToken() { + contentType_ = "application/x-www-form-urlencoded"; + url_ = "https://api.twitter.com/oauth2/invalidate_token"; +} + +void InvalidateToken::accessToken(const std::string _bearer){ + // bodyParam_.insert_or_assign("access_token", _bearer); + bearer_ = _bearer; +} + +const CocoaTweet::API::Model::BearerToken InvalidateToken::process( + std::weak_ptr _oauth) { + auto org = url_; + auto url = url_ + "?access_token=" + bearer_; + url_ = url; + auto basic = std::make_shared(_oauth.lock()->key()); + CocoaTweet::API::Model::BearerToken bearer; + HttpPost::process(basic, [&bearer](const std::string& _rcv) { + auto j = nlohmann::json::parse(_rcv); + bearer.token(j["access_token"]); + }); + url_ = org; + return bearer; +} + +} // namespace CocoaTweet::API::OAuth2 \ No newline at end of file diff --git a/src/cocoatweet/api/oauth2/invalidateToken.h b/src/cocoatweet/api/oauth2/invalidateToken.h index 5872d9a..649b9b5 100644 --- a/src/cocoatweet/api/oauth2/invalidateToken.h +++ b/src/cocoatweet/api/oauth2/invalidateToken.h @@ -1,6 +1,22 @@ #ifndef COCOATWEET_API_OAUTH2_INVALIDATETOKEN_H_ #define COCOATWEET_API_OAUTH2_INVALIDATETOKEN_H_ +#include +#include +#include + +namespace CocoaTweet::API::OAuth2{ +class InvalidateToken : public CocoaTweet::API::Interface::HttpPost { +public: + InvalidateToken(); + void accessToken(const std::string _bearer); + const CocoaTweet::API::Model::BearerToken process( + std::weak_ptr _oauth); + +private: +std::string bearer_; +}; +} #endif \ No newline at end of file diff --git a/src/cocoatweet/api/oauth2/oauth2.cc b/src/cocoatweet/api/oauth2/oauth2.cc index fb11ce8..b99f914 100644 --- a/src/cocoatweet/api/oauth2/oauth2.cc +++ b/src/cocoatweet/api/oauth2/oauth2.cc @@ -7,10 +7,19 @@ OAuth2::OAuth2(std::shared_ptr _o } const std::string OAuth2::token() const { - auto key = oauth_.lock()->key(); - auto oauth = std::make_shared(key); +// auto key = oauth_.lock()->key(); +// auto oauth = std::make_shared(key); CocoaTweet::API::OAuth2::Token token; - return token.process(oauth); + return token.process(oauth_); +} + + const CocoaTweet::API::Model::BearerToken OAuth2::invalidateToken(const std::string& _bearer) const { +// auto key = oauth_.lock()->key(); +// auto oauth = std::make_shared(key); + + CocoaTweet::API::OAuth2::InvalidateToken invalidateToken; + invalidateToken.accessToken(_bearer); + return invalidateToken.process(oauth_); } } // namespace CocoaTweet::API::OAuth2 diff --git a/src/cocoatweet/api/oauth2/oauth2.h b/src/cocoatweet/api/oauth2/oauth2.h index 62d386c..6d66aad 100644 --- a/src/cocoatweet/api/oauth2/oauth2.h +++ b/src/cocoatweet/api/oauth2/oauth2.h @@ -2,8 +2,10 @@ #define COCOATWEET_API_OAUTH2_OAUTH2_H_ #include "cocoatweet/api/interface/groupInterface.h" +#include #include #include +#include #include #include @@ -20,6 +22,8 @@ public: OAuth2(std::shared_ptr _oauth); const std::string token() const; + + const CocoaTweet::API::Model::BearerToken invalidateToken(const std::string& _bearer) const; }; } // namespace CocoaTweet::API::OAuth2 diff --git a/src/main.cc b/src/main.cc index 00137b1..94f8513 100644 --- a/src/main.cc +++ b/src/main.cc @@ -8,6 +8,8 @@ #include #include +#include + auto main() -> int { // Generate Key object // auto consumerKey = "your consumer key"; @@ -35,6 +37,20 @@ auto main() -> int { auto user = api.user().show("milkcocoa9692"); std::cout << user.id() << std::endl; + + std::cout << "consumerKey : " << key.consumerKey() << std::endl; + std::cout << "consumerSecret : " << key.consumerSecret() << std::endl; + std::cout << "bearer : " << key.bearerToken() << std::endl; + std::cout << "bearer : " << CocoaTweet::Util::urlDecode(key.bearerToken()) << std::endl; + + auto newBearerToken = api.oauth2().invalidateToken(key.bearerToken()); + key.bearerToken(newBearerToken.token()); + api.swapKey(key); + user = api.user().show("milkcocoa9692"); + std::cout << user.id() << std::endl; + + + // api.directMessage().messageCreate( // user.id(), // "これはクソみたいなスパムDMです。\nCocoaTwitterLibraryで、user/" @@ -47,7 +63,7 @@ auto main() -> int { // api.generateBearerToken(); // Now, you can use a twitter api - auto status = api.status().update("Hello Twitter World via Cocoa Twitter Library!!"); + // auto status = api.status().update("Hello Twitter World via Cocoa Twitter Library!!"); // std::cout << status.id() << std::endl; // api.favorite().create(status.id()); // api.favorite().destroy(status.id());