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
+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);
}