This commit is contained in:
keita
2021-10-07 20:55:23 +09:00
parent 5d32261b9e
commit 98ed362e12
11 changed files with 82 additions and 97 deletions
+1 -1
View File
@@ -9,7 +9,7 @@ API::API(CocoaTweet::OAuth::Key _key) {
directMessage_ = DirectMessages::DirectMessage(oauth_); directMessage_ = DirectMessages::DirectMessage(oauth_);
} }
const std::string& API::generateBearerToken() const{ const std::string& API::generateBearerToken() const {
return oauth_->generateBearerToken(); return oauth_->generateBearerToken();
} }
+6 -9
View File
@@ -26,7 +26,7 @@ void HttpGet::process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth,
auto url = url_; auto url = url_;
// エンドポイントへのパラメータにOAuthパラメータを付加して署名作成 // エンドポイントへのパラメータにOAuthパラメータを付加して署名作成
auto oauth = _oauth.lock(); auto oauth = _oauth.lock();
// auto oauthParam = oauth->oauthParam(); // auto oauthParam = oauth->oauthParam();
// auto sigingParam = oauthParam; // auto sigingParam = oauthParam;
// if (contentType_ == "application/x-www-form-urlencoded") { // if (contentType_ == "application/x-www-form-urlencoded") {
@@ -40,7 +40,6 @@ void HttpGet::process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth,
// // 作成した署名をエンドポイントへのパラメータ及びOAuthパラメータに登録 // // 作成した署名をエンドポイントへのパラメータ及びOAuthパラメータに登録
// oauthParam.merge(signature); // oauthParam.merge(signature);
// // ヘッダの構築 // // ヘッダの構築
// std::string oauthHeader = "authorization: OAuth "; // std::string oauthHeader = "authorization: OAuth ";
// { // {
@@ -53,7 +52,7 @@ void HttpGet::process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth,
auto oauthHeader = std::string(); auto oauthHeader = std::string();
if (contentType_ == "application/x-www-form-urlencoded") { if (contentType_ == "application/x-www-form-urlencoded") {
oauthHeader = oauth->calculateAuthHeader(bodyParam_, "GET", url_); oauthHeader = oauth->calculateAuthHeader(bodyParam_, "GET", url_);
}else{ } else {
oauthHeader = oauth->calculateAuthHeader({}, "GET", url_); oauthHeader = oauth->calculateAuthHeader({}, "GET", url_);
} }
@@ -105,7 +104,7 @@ void HttpGet::process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth,
exit(1); exit(1);
} }
std::cout << rcv << std::endl; std::cout << rcv << std::endl;
if ((responseCode / 100) == 4) { if ((responseCode / 100) == 4) {
auto j = nlohmann::json::parse(rcv); auto j = nlohmann::json::parse(rcv);
auto error = j["errors"][0]["code"]; auto error = j["errors"][0]["code"];
@@ -118,17 +117,15 @@ std::cout << rcv << std::endl;
throw CocoaTweet::Exception::TweetNotFoundException(message.get<std::string>().c_str()); throw CocoaTweet::Exception::TweetNotFoundException(message.get<std::string>().c_str());
} else if (error.get<int>() == 32) { } else if (error.get<int>() == 32) {
throw CocoaTweet::Exception::AuthenticateException(message.get<std::string>().c_str()); throw CocoaTweet::Exception::AuthenticateException(message.get<std::string>().c_str());
}else if(error.get<int>() == 89){ } else if (error.get<int>() == 89) {
throw CocoaTweet::Exception::TokenInvalidException(message.get<std::string>().c_str()); throw CocoaTweet::Exception::TokenInvalidException(message.get<std::string>().c_str());
}else if (error.get<int>() == 187) { } else if (error.get<int>() == 187) {
throw CocoaTweet::Exception::TweetDuplicateException(message.get<std::string>().c_str()); throw CocoaTweet::Exception::TweetDuplicateException(message.get<std::string>().c_str());
} else if (error.get<int>() == 88 || error.get<int>() == 185) { } else if (error.get<int>() == 88 || error.get<int>() == 185) {
throw CocoaTweet::Exception::RateLimitException(message.get<std::string>().c_str()); throw CocoaTweet::Exception::RateLimitException(message.get<std::string>().c_str());
} else if (error.get<int>() == 186) { } else if (error.get<int>() == 186) {
throw CocoaTweet::Exception::TweetTooLongException(message.get<std::string>().c_str()); throw CocoaTweet::Exception::TweetTooLongException(message.get<std::string>().c_str());
} } else {
else{
} }
} }
+1 -1
View File
@@ -126,7 +126,7 @@ void HttpPost::process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth,
throw CocoaTweet::Exception::TweetNotFoundException(message.get<std::string>().c_str()); throw CocoaTweet::Exception::TweetNotFoundException(message.get<std::string>().c_str());
} else if (error.get<int>() == 32) { } else if (error.get<int>() == 32) {
throw CocoaTweet::Exception::AuthenticateException(message.get<std::string>().c_str()); throw CocoaTweet::Exception::AuthenticateException(message.get<std::string>().c_str());
}else if(error.get<int>() == 89){ } else if (error.get<int>() == 89) {
throw CocoaTweet::Exception::TokenInvalidException(message.get<std::string>().c_str()); throw CocoaTweet::Exception::TokenInvalidException(message.get<std::string>().c_str());
} else if (error.get<int>() == 187) { } else if (error.get<int>() == 187) {
throw CocoaTweet::Exception::TweetDuplicateException(message.get<std::string>().c_str()); throw CocoaTweet::Exception::TweetDuplicateException(message.get<std::string>().c_str());
+2 -2
View File
@@ -3,7 +3,7 @@
namespace CocoaTweet::API::Model { namespace CocoaTweet::API::Model {
Tweet Tweet::parse(const std::string& _json) { Tweet Tweet::parse(const std::string& _json) {
auto j = nlohmann::json::parse(_json); auto j = nlohmann::json::parse(_json);
Tweet tweet; Tweet tweet;
tweet.id(j["id_str"]); tweet.id(j["id_str"]);
@@ -11,7 +11,7 @@ Tweet Tweet::parse(const std::string& _json) {
tweet.text(j["text"]); tweet.text(j["text"]);
tweet.source(j["source"]); tweet.source(j["source"]);
tweet.user(CocoaTweet::API::Model::User(j["user"].dump())); tweet.user(CocoaTweet::API::Model::User(j["user"].dump()));
return tweet; return tweet;
} }
+10 -12
View File
@@ -10,26 +10,24 @@ CocoaTweet::API::Model::User User::parse(const std::string& _json) {
return user; return user;
} }
void User::id(const std::string& _id){ void User::id(const std::string& _id) {
id_ = _id; id_ = _id;
} }
void User::name(const std::string& _name){ void User::name(const std::string& _name) {
name_ = _name; name_ = _name;
} }
void User::screenName(const std::string& _screen){ void User::screenName(const std::string& _screen) {
screenName_ = _screen; screenName_ = _screen;
} }
void User::location(const std::string& _location){ void User::location(const std::string& _location) {
location_ = _location; location_ = _location;
} }
const std::string& User::id() const{ const std::string& User::id() const {
return id_; return id_;
} }
} // namespace CocoaTweet::API::Model } // namespace CocoaTweet::API::Model
+28 -29
View File
@@ -25,36 +25,35 @@ public:
void id(const std::string& _id); void id(const std::string& _id);
void name(const std::string& _name); void name(const std::string& _name);
void screenName(const std::string& _screen); void screenName(const std::string& _screen);
void location(const std::string& _location); void location(const std::string& _location);
void url(const std::string& _url); void url(const std::string& _url);
void description(const std::string& _description); void description(const std::string& _description);
void protectedUser(const bool _protected); void protectedUser(const bool _protected);
void follower(const long _follower); void follower(const long _follower);
void follow(const long _follow); void follow(const long _follow);
void listed(const long _listed); void listed(const long _listed);
void favorite(const long _favorite); void favorite(const long _favorite);
void tweet(const long _tweet); void tweet(const long _tweet);
void createdAt(const std::string& _created); void createdAt(const std::string& _created);
void bannerUrl(const std::string& _banner); void bannerUrl(const std::string& _banner);
void iconUrl(const std::string& _icon); void iconUrl(const std::string& _icon);
const std::string& id() const; const std::string& id() const;
const std::string& name() const; const std::string& name() const;
const std::string& screenName() const; const std::string& screenName() const;
const std::string& location() const; const std::string& location() const;
const std::string& url() const; const std::string& url() const;
const std::string& description() const; const std::string& description() const;
bool protectedUser() const; bool protectedUser() const;
long follower() const; long follower() const;
long follow() const; long follow() const;
long listed() const; long listed() const;
long favorite() const; long favorite() const;
long tweet() const; long tweet() const;
const std::string& created() const; const std::string& created() const;
const std::string& banner() const; const std::string& banner() const;
const std::string& icon() const; const std::string& icon() const;
private: private:
std::string id_; std::string id_;
@@ -73,6 +72,6 @@ private:
std::string bannerUrl_; std::string bannerUrl_;
std::string iconUrl_; std::string iconUrl_;
}; };
} } // namespace CocoaTweet::API::Model
#endif #endif
+5 -9
View File
@@ -4,7 +4,6 @@
#include <string> #include <string>
#include <typeinfo> #include <typeinfo>
namespace CocoaTweet::OAuth { namespace CocoaTweet::OAuth {
Key Key::fromJsonFile(const std::string _jsonFile) { Key Key::fromJsonFile(const std::string _jsonFile) {
std::ifstream ifs(_jsonFile); std::ifstream ifs(_jsonFile);
@@ -12,25 +11,22 @@ Key Key::fromJsonFile(const std::string _jsonFile) {
auto j = nlohmann::json::parse(str); auto j = nlohmann::json::parse(str);
Key key; Key key;
if(j.contains("consumer_key")){ if (j.contains("consumer_key")) {
key.consumerKey(j["consumer_key"].get<std::string>()); key.consumerKey(j["consumer_key"].get<std::string>());
} }
if (j.contains("consumer_secret")) {
if(j.contains("consumer_secret")){
key.consumerSecret(j["consumer_secret"].get<std::string>()); key.consumerSecret(j["consumer_secret"].get<std::string>());
} }
if (j.contains("access_token")) {
if(j.contains("access_token")){
key.accessToken(j["access_token"].get<std::string>()); key.accessToken(j["access_token"].get<std::string>());
} }
if (j.contains("access_token_secret")) {
if(j.contains("access_token_secret")){
key.accessTokenSecret(j["access_token_secret"].get<std::string>()); key.accessTokenSecret(j["access_token_secret"].get<std::string>());
} }
return key; return key;
} }
} // namespace CocoaTweet::OAuth } // namespace CocoaTweet::OAuth
+8 -7
View File
@@ -20,25 +20,26 @@ public:
consumerSecret_(_consumerSecret), consumerSecret_(_consumerSecret),
accessToken_(_accessToken), accessToken_(_accessToken),
accessTokenSecret_(_accessTokenSecret) {} accessTokenSecret_(_accessTokenSecret) {}
Key(const std::string& _consumerKey, const std::string& _consumerSecret): consumerKey_(_consumerKey), consumerSecret_(_consumerSecret){} Key(const std::string& _consumerKey, const std::string& _consumerSecret)
: consumerKey_(_consumerKey), consumerSecret_(_consumerSecret) {}
void consumerKey(const std::string& _consumerKey){ void consumerKey(const std::string& _consumerKey) {
consumerKey_ = _consumerKey; consumerKey_ = _consumerKey;
} }
void consumerSecret(const std::string& _consumerSecret){ void consumerSecret(const std::string& _consumerSecret) {
consumerSecret_ = _consumerSecret; consumerSecret_ = _consumerSecret;
} }
void accessToken(const std::string& _accessToken){ void accessToken(const std::string& _accessToken) {
accessToken_ = _accessToken; accessToken_ = _accessToken;
} }
void accessTokenSecret(const std::string& _accessTokenSecret){ void accessTokenSecret(const std::string& _accessTokenSecret) {
accessTokenSecret_ = _accessTokenSecret; accessTokenSecret_ = _accessTokenSecret;
} }
void bearerToken(const std::string& _bearer){ void bearerToken(const std::string& _bearer) {
bearerToken_ = _bearer; bearerToken_ = _bearer;
} }
const std::string& consumerKey() const { const std::string& consumerKey() const {
@@ -54,7 +55,7 @@ public:
return accessTokenSecret_; return accessTokenSecret_;
} }
const std::string& bearerToken() const{ const std::string& bearerToken() const {
return bearerToken_; return bearerToken_;
} }
+16 -16
View File
@@ -23,7 +23,7 @@ extern "C" {
#endif #endif
namespace CocoaTweet::OAuth { namespace CocoaTweet::OAuth {
OAuth1::OAuth1(): authType_(AuthType::OAuth) {} OAuth1::OAuth1() : authType_(AuthType::OAuth) {}
OAuth1::OAuth1(const Key _key) : key_(_key), authType_(AuthType::OAuth) {} OAuth1::OAuth1(const Key _key) : key_(_key), authType_(AuthType::OAuth) {}
@@ -46,12 +46,14 @@ std::map<std::string, std::string> OAuth1::signature(
return ret; return ret;
} }
const std::string OAuth1::calculateAuthHeader(std::map<std::string, std::string> _bodyParam, const std::string& _method, const std::string& _url){ const std::string OAuth1::calculateAuthHeader(std::map<std::string, std::string> _bodyParam,
if(authType_ == AuthType::Bearer){ const std::string& _method,
const std::string& _url) {
if (authType_ == AuthType::Bearer) {
return "Authorization: Bearer " + key_.bearerToken(); return "Authorization: Bearer " + key_.bearerToken();
} }
auto authParam = oauthParam(); auto authParam = oauthParam();
auto sigingParam = authParam; auto sigingParam = authParam;
if (!_bodyParam.empty()) { if (!_bodyParam.empty()) {
for (const auto [k, v] : _bodyParam) { for (const auto [k, v] : _bodyParam) {
@@ -62,7 +64,7 @@ const std::string OAuth1::calculateAuthHeader(std::map<std::string, std::string>
auto sign = signature(sigingParam, _method, _url); auto sign = signature(sigingParam, _method, _url);
authParam.merge(sign); authParam.merge(sign);
// ヘッダの構築 // ヘッダの構築
std::string oauthHeader = "authorization: OAuth "; std::string oauthHeader = "authorization: OAuth ";
{ {
std::vector<std::string> tmp; std::vector<std::string> tmp;
@@ -75,14 +77,13 @@ const std::string OAuth1::calculateAuthHeader(std::map<std::string, std::string>
return oauthHeader; return oauthHeader;
} }
const std::string& OAuth1::generateBearerToken() {
auto signature = key_.consumerKey() + ":" + key_.consumerSecret();
const std::string& OAuth1::generateBearerToken(){
auto signature = key_.consumerKey() + ":" + key_.consumerSecret();
auto k64Signature = base64(signature); auto k64Signature = base64(signature);
auto authHeader = std::string("Authorization: Basic ") + k64Signature; auto authHeader = std::string("Authorization: Basic ") + k64Signature;
auto contentType = std::string("Content-Type: application/x-www-form-urlencoded;charset=UTF-8"); auto contentType =
auto url = std::string("https://api.twitter.com/oauth2/token"); std::string("Content-Type: application/x-www-form-urlencoded;charset=UTF-8");
auto url = std::string("https://api.twitter.com/oauth2/token");
auto requestBody = std::string("grant_type=client_credentials"); auto requestBody = std::string("grant_type=client_credentials");
// do post // do post
@@ -118,8 +119,7 @@ const std::string& OAuth1::generateBearerToken(){
exit(1); exit(1);
} }
auto j = nlohmann::json::parse(rcv);
auto j = nlohmann::json::parse(rcv);
if ((responseCode / 100) == 4) { if ((responseCode / 100) == 4) {
auto error = j["errors"][0]["code"]; auto error = j["errors"][0]["code"];
auto message = j["errors"][0]["message"]; auto message = j["errors"][0]["message"];
@@ -128,14 +128,14 @@ const std::string& OAuth1::generateBearerToken(){
throw new CocoaTweet::Exception::Exception(j["error"]); throw new CocoaTweet::Exception::Exception(j["error"]);
} }
if (error.get<int>() == 44) { if (error.get<int>() == 44) {
throw CocoaTweet::Exception::InvalidParameterException(message.get<std::string>().c_str()); throw CocoaTweet::Exception::InvalidParameterException(
message.get<std::string>().c_str());
} }
} }
key_.bearerToken(j["access_token"]); key_.bearerToken(j["access_token"]);
authType_ = AuthType::Bearer; authType_ = AuthType::Bearer;
return key_.bearerToken(); return key_.bearerToken();
} }
const std::string OAuth1::nonce() const { const std::string OAuth1::nonce() const {
+4 -9
View File
@@ -9,21 +9,17 @@
namespace CocoaTweet::OAuth { namespace CocoaTweet::OAuth {
class OAuth1 { class OAuth1 {
public: public:
enum AuthType { OAuth, Bearer };
enum AuthType{
OAuth,
Bearer
};
OAuth1(); OAuth1();
OAuth1(const Key _key); OAuth1(const Key _key);
std::map<std::string, std::string> signature(const std::map<std::string, std::string>& _param, std::map<std::string, std::string> signature(const std::map<std::string, std::string>& _param,
const std::string& _method, const std::string& _method,
const std::string& _url); const std::string& _url);
const std::string& generateBearerToken();
const std::string calculateAuthHeader(std::map<std::string, std::string> _bodyParam, const std::string& _method, const std::string& _url);
const std::string& generateBearerToken();
const std::string calculateAuthHeader(std::map<std::string, std::string> _bodyParam,
const std::string& _method, const std::string& _url);
const std::string nonce() const; const std::string nonce() const;
const std::string timestamp() const; const std::string timestamp() const;
@@ -44,7 +40,6 @@ private:
_stream->append(_ptr, realsize); _stream->append(_ptr, realsize);
return realsize; return realsize;
} }
}; };
} // namespace CocoaTweet::OAuth } // namespace CocoaTweet::OAuth
+1 -2
View File
@@ -11,7 +11,6 @@
#include <cocoatweet/exception/exception.h> #include <cocoatweet/exception/exception.h>
#include <cocoatweet/exception/rateLimitException.h> #include <cocoatweet/exception/rateLimitException.h>
bool starts_with(const std::string& s, const std::string& prefix) { bool starts_with(const std::string& s, const std::string& prefix) {
auto size = prefix.size(); auto size = prefix.size();
if (s.size() < size) return false; if (s.size() < size) return false;
@@ -32,7 +31,7 @@ auto main() -> int {
// also can generate Key object from JSON file // also can generate Key object from JSON file
CocoaTweet::OAuth::Key key = CocoaTweet::OAuth::Key::fromJsonFile("api_key.json"); CocoaTweet::OAuth::Key key = CocoaTweet::OAuth::Key::fromJsonFile("api_key.json");
// auto oauth = CocoaTweet::OAuth::OAuth1(key); // auto oauth = CocoaTweet::OAuth::OAuth1(key);
// oauth.GenerateBearerToken(); // oauth.GenerateBearerToken();
// std::cout << "sdfgwregfresgfresdwefgweragregreagretgreawgrt#$QTWREATGREWTGF$ERTF"; // std::cout << "sdfgwregfresgfresdwefgweragregreagretgreawgrt#$QTWREATGREWTGF$ERTF";
// Generate API Entry object using Key object // Generate API Entry object using Key object