OAuth2が出来るようになったよ。ブランチの目的からめちゃくちゃズレてるね

This commit is contained in:
keita
2021-10-07 20:53:25 +09:00
parent a4fd0d9830
commit 5d32261b9e
2184 changed files with 220055 additions and 42 deletions
+4
View File
@@ -9,6 +9,10 @@ API::API(CocoaTweet::OAuth::Key _key) {
directMessage_ = DirectMessages::DirectMessage(oauth_);
}
const std::string& API::generateBearerToken() const{
return oauth_->generateBearerToken();
}
Statuses::Status API::status() const {
return status_;
}
+1
View File
@@ -26,6 +26,7 @@ public:
Medias::Media media() const;
DirectMessages::DirectMessage directMessage() const;
const std::string& generateBearerToken() const;
private:
Statuses::Status status_;
+61 -20
View File
@@ -1,5 +1,12 @@
#include <cocoatweet/api/interface/httpGet.h>
#include "cocoatweet/util/util.h"
#include <cocoatweet/exception/tweetNotFoundException.h>
#include <cocoatweet/exception/authenticateException.h>
#include <cocoatweet/exception/tweetDuplicateException.h>
#include <cocoatweet/exception/tweetTooLongException.h>
#include <cocoatweet/exception/rateLimitException.h>
#include <cocoatweet/exception/tokenInvalidException.h>
#include "nlohmann/json.hpp"
#include <iterator>
#include <memory>
#include <vector>
@@ -20,19 +27,36 @@ void HttpGet::process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth,
// エンドポイントへのパラメータにOAuthパラメータを付加して署名作成
auto oauth = _oauth.lock();
auto oauthParam = oauth->oauthParam();
auto sigingParam = oauthParam;
// auto oauthParam = oauth->oauthParam();
// auto sigingParam = oauthParam;
// if (contentType_ == "application/x-www-form-urlencoded") {
// for (const auto [k, v] : bodyParam_) {
// sigingParam.insert_or_assign(k, v);
// }
// }
// auto signature = oauth->signature(sigingParam, "GET", url_);
// // 作成した署名をエンドポイントへのパラメータ及びOAuthパラメータに登録
// oauthParam.merge(signature);
// // ヘッダの構築
// std::string oauthHeader = "authorization: OAuth ";
// {
// std::vector<std::string> tmp;
// for (const auto& [key, value] : oauthParam) {
// tmp.push_back(key + "=" + CocoaTweet::Util::urlEncode(value));
// }
// oauthHeader += CocoaTweet::Util::join(tmp, ",");
// }
auto oauthHeader = std::string();
if (contentType_ == "application/x-www-form-urlencoded") {
for (const auto [k, v] : bodyParam_) {
sigingParam.insert_or_assign(k, v);
}
oauthHeader = oauth->calculateAuthHeader(bodyParam_, "GET", url_);
}else{
oauthHeader = oauth->calculateAuthHeader({}, "GET", url_);
}
auto signature = oauth->signature(sigingParam, "GET", url_);
// 作成した署名をエンドポイントへのパラメータ及びOAuthパラメータに登録
oauthParam.merge(signature);
// URLの構築
{
std::vector<std::string> tmp;
@@ -42,16 +66,6 @@ void HttpGet::process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth,
url_ += std::string("?" + CocoaTweet::Util::join(tmp, "&"));
}
// ヘッダの構築
std::string oauthHeader = "authorization: OAuth ";
{
std::vector<std::string> tmp;
for (const auto& [key, value] : oauthParam) {
tmp.push_back(key + "=" + CocoaTweet::Util::urlEncode(value));
}
oauthHeader += CocoaTweet::Util::join(tmp, ",");
}
// do post
CURL* curl;
CURLcode res;
@@ -91,6 +105,33 @@ void HttpGet::process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth,
exit(1);
}
std::cout << rcv << std::endl;
if ((responseCode / 100) == 4) {
auto j = nlohmann::json::parse(rcv);
auto error = j["errors"][0]["code"];
auto message = j["errors"][0]["message"];
if (j.count("error") != 0) {
// この形式はエラーコードを持たないのでエラー種別が特定できない
throw new CocoaTweet::Exception::Exception(j["error"]);
}
if (error.get<int>() == 144) {
throw CocoaTweet::Exception::TweetNotFoundException(message.get<std::string>().c_str());
} else if (error.get<int>() == 32) {
throw CocoaTweet::Exception::AuthenticateException(message.get<std::string>().c_str());
}else if(error.get<int>() == 89){
throw CocoaTweet::Exception::TokenInvalidException(message.get<std::string>().c_str());
}else if (error.get<int>() == 187) {
throw CocoaTweet::Exception::TweetDuplicateException(message.get<std::string>().c_str());
} else if (error.get<int>() == 88 || error.get<int>() == 185) {
throw CocoaTweet::Exception::RateLimitException(message.get<std::string>().c_str());
} else if (error.get<int>() == 186) {
throw CocoaTweet::Exception::TweetTooLongException(message.get<std::string>().c_str());
}
else{
}
}
if (_callback) {
_callback(rcv);
}
+3
View File
@@ -5,6 +5,7 @@
#include <cocoatweet/exception/tweetDuplicateException.h>
#include <cocoatweet/exception/tweetTooLongException.h>
#include <cocoatweet/exception/rateLimitException.h>
#include <cocoatweet/exception/tokenInvalidException.h>
#include "nlohmann/json.hpp"
#include <iterator>
#include <memory>
@@ -125,6 +126,8 @@ void HttpPost::process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth,
throw CocoaTweet::Exception::TweetNotFoundException(message.get<std::string>().c_str());
} else if (error.get<int>() == 32) {
throw CocoaTweet::Exception::AuthenticateException(message.get<std::string>().c_str());
}else if(error.get<int>() == 89){
throw CocoaTweet::Exception::TokenInvalidException(message.get<std::string>().c_str());
} else if (error.get<int>() == 187) {
throw CocoaTweet::Exception::TweetDuplicateException(message.get<std::string>().c_str());
} else if (error.get<int>() == 88 || error.get<int>() == 185) {
-2
View File
@@ -2,8 +2,6 @@
#include <cocoatweet/api/model/mediaStore.h>
#include <cocoatweet/exception/unsupportedMediaTypeException.h>
#include <fstream>
#include <iostream>
namespace CocoaTweet::API::Medias {
const std::map<std::string, std::string> Upload::mimeType = {{".jpg", "image/jpeg"},
{".jpeg", "image/jpeg"},
+14 -1
View File
@@ -3,7 +3,7 @@
namespace CocoaTweet::API::Model {
Tweet Tweet::parse(const std::string& _json) {
auto j = nlohmann::json::parse(_json);
auto j = nlohmann::json::parse(_json);
Tweet tweet;
tweet.id(j["id_str"]);
@@ -11,6 +11,8 @@ Tweet Tweet::parse(const std::string& _json) {
tweet.text(j["text"]);
tweet.source(j["source"]);
tweet.user(CocoaTweet::API::Model::User(j["user"].dump()));
return tweet;
}
@@ -29,16 +31,27 @@ void Tweet::source(const std::string _source) {
source_ = _source;
}
void Tweet::user(const CocoaTweet::API::Model::User _user) {
user_ = _user;
}
const std::string Tweet::id() const {
return id_;
}
const std::string Tweet::createdAt() const {
return createdAt_;
}
const std::string Tweet::text() const {
return text_;
}
const std::string Tweet::source() const {
return source_;
}
const CocoaTweet::API::Model::User Tweet::user() const {
return user_;
}
} // namespace CocoaTweet::API::Model
+6
View File
@@ -1,6 +1,7 @@
#ifndef COCOATWEET_API_MODEL_TWEET_H_
#define COCOATWEET_API_MODEL_TWEET_H_
#include <cocoatweet/api/model/user.h>
#include <string>
namespace CocoaTweet::API::Model {
@@ -43,6 +44,8 @@ public:
/// @param[out] none
void source(const std::string _source);
void user(const CocoaTweet::API::Model::User _user);
/// @brief get tweet id
/// @param[in] none
/// @param[out] const std::string : tweet id
@@ -63,11 +66,14 @@ public:
/// @param[out] const std::string : source information
const std::string source() const;
const CocoaTweet::API::Model::User user() const;
private:
std::string id_;
std::string createdAt_;
std::string text_;
std::string source_;
CocoaTweet::API::Model::User user_;
};
} // namespace CocoaTweet::API::Model
+35
View File
@@ -0,0 +1,35 @@
#include <cocoatweet/api/model/user.h>
#include "nlohmann/json.hpp"
namespace CocoaTweet::API::Model {
CocoaTweet::API::Model::User User::parse(const std::string& _json) {
auto j = nlohmann::json::parse(_json);
User user;
user.id(j["id_str"]);
return user;
}
void User::id(const std::string& _id){
id_ = _id;
}
void User::name(const std::string& _name){
name_ = _name;
}
void User::screenName(const std::string& _screen){
screenName_ = _screen;
}
void User::location(const std::string& _location){
location_ = _location;
}
const std::string& User::id() const{
return id_;
}
} // namespace CocoaTweet::API::Model
+78
View File
@@ -0,0 +1,78 @@
#ifndef COCOATWEET_API_MODEL_USER_H_
#define COCOATWEET_API_MODEL_USER_H_
#include <string>
namespace CocoaTweet::API::Model {
/// @brief data class for tweet object
class User final {
public:
/// @brief constructor
User() = default;
/// @brief copy constructor
User(const User&) = default;
/// @brief constructor for create object from json response
/// @param[in] const std::string& _json : received content from twitter endpoint
User(const std::string& _json) : User(User::parse(_json)) {}
/// @brief response parser for user object
/// @param[in] const std::string& _json : received content from twitter endpoint
/// @param[out] CocoaTweet::API::Model::User
static User parse(const std::string& _json);
void id(const std::string& _id);
void name(const std::string& _name);
void screenName(const std::string& _screen);
void location(const std::string& _location);
void url(const std::string& _url);
void description(const std::string& _description);
void protectedUser(const bool _protected);
void follower(const long _follower);
void follow(const long _follow);
void listed(const long _listed);
void favorite(const long _favorite);
void tweet(const long _tweet);
void createdAt(const std::string& _created);
void bannerUrl(const std::string& _banner);
void iconUrl(const std::string& _icon);
const std::string& id() const;
const std::string& name() const;
const std::string& screenName() const;
const std::string& location() const;
const std::string& url() const;
const std::string& description() const;
bool protectedUser() const;
long follower() const;
long follow() const;
long listed() const;
long favorite() const;
long tweet() const;
const std::string& created() const;
const std::string& banner() const;
const std::string& icon() const;
private:
std::string id_;
std::string name_;
std::string screenName_;
std::string location_;
std::string url_;
std::string description_;
bool protectedUser_;
long follower_;
long follow_;
long listed_;
long favorite_;
long tweet_;
std::string createdAt_;
std::string bannerUrl_;
std::string iconUrl_;
};
}
#endif
-1
View File
@@ -1,5 +1,4 @@
#include <cocoatweet/api/status/retweet.h>
#include <iostream>
namespace CocoaTweet::API::Statuses {
Retweet::Retweet() {}
-1
View File
@@ -1,5 +1,4 @@
#include <cocoatweet/api/status/unretweet.h>
#include <iostream>
namespace CocoaTweet::API::Statuses {
Unretweet::Unretweet() {}