From 45402eb2179e5e64dac27e17bb031fa5249c93fb Mon Sep 17 00:00:00 2001 From: keita Date: Wed, 17 Feb 2021 00:20:35 +0900 Subject: [PATCH] =?UTF-8?q?=E3=83=84=E3=82=A4=E3=83=BC=E3=83=88=E3=81=A7?= =?UTF-8?q?=E3=81=8D=E3=81=9F=E3=81=9E=EF=BC=81=EF=BC=81=EF=BC=81(#47)(#48?= =?UTF-8?q?)(#49)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 7 +++--- src/CMakeLists.txt | 1 - src/cocoatweet/CMakeLists.txt | 3 --- src/cocoatweet/api/status/update.cc | 32 ++++++++++++++------------ src/cocoatweet/oauth/oauth.cc | 35 +++++++++++++++-------------- src/cocoatweet/util/util.cc | 31 ++++++++++++------------- src/cocoatweet/util/util.h | 10 ++++----- src/main.cc | 16 ++++++------- 8 files changed, 68 insertions(+), 67 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 843d19d..3a599b2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -49,7 +49,6 @@ if(ENABLE_CODE_COVERAGE) endif() # Required libraries -find_package(Boost 1.61.0 COMPONENTS unit_test_framework REQUIRED) find_package(PkgConfig REQUIRED) find_package(OpenSSL REQUIRED) if(NOT OPENSSL_FOUND) @@ -62,14 +61,14 @@ find_package(CURL REQUIRED) if(NOT CURL_FOUND) message(FATAL_ERROR "Fail to find OpenSSL") # exit endif() -message(FATAL_ERROR, "Fail to find libcurl") include_directories(${CURL_INCLUDE_DIRS}) -enable_testing() +find_library(cpprest REQUIRED) +include_directories(${CPP_REST_INCLUDE_DIR}) + include_directories( ${PROJECT_SOURCE_DIR}/src - ${Boost_INCLUDE_DIRS} ) add_subdirectory(src) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2b348df..663f71f 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -4,7 +4,6 @@ set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}) add_executable(${PROJECT_NAME} main.cc) target_link_libraries(${PROJECT_NAME} lib-cocoatweet - ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY} OpenSSL::SSL OpenSSL::Crypto ${CURL_LIBRARIES} diff --git a/src/cocoatweet/CMakeLists.txt b/src/cocoatweet/CMakeLists.txt index 4505a85..c5bb89d 100644 --- a/src/cocoatweet/CMakeLists.txt +++ b/src/cocoatweet/CMakeLists.txt @@ -1,7 +1,4 @@ file(GLOB_RECURSE SOURCES ./*.cc) add_library(lib-cocoatweet ${SOURCES}) -target_link_libraries(lib-cocoatweet PUBLIC - Boost::boost -) target_include_directories(lib-cocoatweet PUBLIC ${PROJECT_SOURCE_DIR}/src) set_target_properties(lib-cocoatweet PROPERTIES OUTPUT_NAME cocoatweet) diff --git a/src/cocoatweet/api/status/update.cc b/src/cocoatweet/api/status/update.cc index adeec9d..0952bcf 100644 --- a/src/cocoatweet/api/status/update.cc +++ b/src/cocoatweet/api/status/update.cc @@ -21,15 +21,18 @@ void Update::status(const std::string _status) { void Update::process(std::weak_ptr _oauth, std::function _callback) { // エンドポイントへのパラメータにOAuthパラメータを付加して署名作成 - auto oauth = _oauth.lock(); - param_.merge(oauth->oauthParam()); - auto signature = oauth->signature(param_, "POST", url_); + auto oauth = _oauth.lock(); + auto oauthParam = oauth->oauthParam(); + auto sigingParam = oauthParam; + for (const auto [k, v] : param_) { + sigingParam.insert_or_assign(k, v); + } + + auto signature = oauth->signature(sigingParam, "POST", url_); // 作成した署名をエンドポイントへのパラメータ及びOAuthパラメータに登録 - param_.insert_or_assign("oauth_signature", signature["oauth_signature"]); - auto header = oauth->oauthParam(); std::cout << "signature : " << signature["oauth_signature"] << std::endl; - header.insert_or_assign("oauth_signature", signature["oauth_signature"]); + oauthParam.merge(signature); // リクエストボディの構築 std::string requestBody = ""; @@ -43,14 +46,13 @@ void Update::process(std::weak_ptr _oauth, requestBody = os.str(); requestBody.erase(requestBody.size() - std::char_traits::length("&")); } - requestBody += (std::string("&") + "oauth_signature=" + signature["oauth_signature"]); std::cout << "request Body -> " << requestBody << std::endl; // ヘッダの構築 - std::string oauthHeader = "Authorization: OAuth "; + std::string oauthHeader = "authorization: OAuth "; { std::vector tmp; - for (const auto& [key, value] : header) { + for (const auto& [key, value] : oauthParam) { tmp.push_back(key + "=" + value); } std::stringstream os; @@ -58,7 +60,6 @@ void Update::process(std::weak_ptr _oauth, oauthHeader += os.str(); oauthHeader.erase(oauthHeader.size() - std::char_traits::length(",")); } - //oauthHeader += (std::string(",") + "oauth_signature=" + signature["oauth_signature"]); std::cout << "OAuth Header -> " << oauthHeader << std::endl; // do post @@ -66,20 +67,23 @@ void Update::process(std::weak_ptr _oauth, CURLcode res; std::string rcv; curl = curl_easy_init(); + url_ = url_ + "?status=" + status_; std::cout << "URL : " << url_ << std::endl; if (curl) { curl_easy_setopt(curl, CURLOPT_URL, url_.c_str()); curl_easy_setopt(curl, CURLOPT_POST, 1); - curl_easy_setopt(curl, CURLOPT_POSTFIELDS, requestBody); - curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, requestBody.length()); + // curl_easy_setopt(curl, CURLOPT_POSTFIELDS, requestBody); + // curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, requestBody.length()); + curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, 0); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curlCallback_); curl_easy_setopt(curl, CURLOPT_WRITEDATA, (std::string*)&rcv); - curl_easy_setopt(curl, CURLOPT_PROXY, ""); + curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); // Headerを保持するcurl_slist*を初期化 struct curl_slist* headers = NULL; // Authorizationをヘッダに追加 headers = curl_slist_append(headers, oauthHeader.c_str()); - curl_easy_setopt(curl, CURLOPT_HEADER, headers); + // headers = curl_slist_append(headers, "Content-Type: application/x-www-form-urlencoded"); + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); res = curl_easy_perform(curl); curl_easy_cleanup(curl); } diff --git a/src/cocoatweet/oauth/oauth.cc b/src/cocoatweet/oauth/oauth.cc index cff6f74..2331b0c 100644 --- a/src/cocoatweet/oauth/oauth.cc +++ b/src/cocoatweet/oauth/oauth.cc @@ -26,7 +26,7 @@ std::map OAuth1::signature( std::vector tmp; for (const auto& [key, value] : _param) { tmp.push_back(key + "=" + value); - std::cout << (key + "=" + value) << std::endl; + std::cout << (key + "=" + value) << std::endl; } std::ostringstream os; std::copy(tmp.begin(), tmp.end(), std::ostream_iterator(os, "&")); @@ -34,14 +34,16 @@ std::map OAuth1::signature( query.erase(query.size() - std::char_traits::length("&")); auto significateKey = key().consumerSecret() + "&" + key().accessTokenSecret(); - auto significateBase = _method + "&" + CocoaTweet::Util::urlEncode(_url) + "&" + CocoaTweet::Util::urlEncode(query); - auto k64Sha1 = hmacSha1(significateKey, significateBase); + auto significateBase = _method + "&" + CocoaTweet::Util::urlEncode(_url) + "&" + + CocoaTweet::Util::urlEncode(query); + auto k64Sha1 = hmacSha1(significateKey, significateBase); std::cout << "significate key : " << significateKey << std::endl; std::cout << "significate base : " << significateBase << std::endl; std::cout << "hmac-sha1 : " << k64Sha1 << std::endl; - auto ret = std::map{{"oauth_signature", CocoaTweet::Util::urlEncode(k64Sha1)}}; + auto ret = std::map{ + {"oauth_signature", CocoaTweet::Util::urlEncode(k64Sha1)}}; return ret; } @@ -121,23 +123,22 @@ std::string OAuth1::hmacSha1(std::string _key, std::string _data) { HMAC(EVP_sha1(), reinterpret_cast(_key.c_str()), _key.length(), reinterpret_cast(_data.c_str()), _data.length(), result, &length); - auto sha1 = std::string(reinterpret_cast(result), length); + auto sha1 = std::string(reinterpret_cast(result), length); - - // base64 encodeもやっちゃえ日産 - BIO *encoder = BIO_new(BIO_f_base64()); - BIO *bmem = BIO_new(BIO_s_mem()); - encoder = BIO_push(encoder,bmem); + // base64 encodeもやっちゃえ日産 + BIO* encoder = BIO_new(BIO_f_base64()); + BIO* bmem = BIO_new(BIO_s_mem()); + encoder = BIO_push(encoder, bmem); BIO_write(encoder, sha1.c_str(), sha1.length()); BIO_flush(encoder); - BUF_MEM *bptr; - BIO_get_mem_ptr(encoder,&bptr); - - char *k64 = (char *)std::malloc(bptr->length); - std::memcpy(k64, bptr->data, bptr->length-1); - k64[bptr->length-1] = 0; - + BUF_MEM* bptr; + BIO_get_mem_ptr(encoder, &bptr); + + char* k64 = (char*)std::malloc(bptr->length); + std::memcpy(k64, bptr->data, bptr->length - 1); + k64[bptr->length - 1] = 0; + BIO_free_all(encoder); return static_cast(k64); diff --git a/src/cocoatweet/util/util.cc b/src/cocoatweet/util/util.cc index 40a0626..e8f777d 100644 --- a/src/cocoatweet/util/util.cc +++ b/src/cocoatweet/util/util.cc @@ -1,21 +1,22 @@ #include "cocoatweet/util/util.h" #include -namespace CocoaTweet::Util{ - std::string urlEncode(const std::string& _str){ - std::stringstream out; +namespace CocoaTweet::Util { +std::string urlEncode(const std::string& _str) { + std::stringstream out; - for(const auto c : _str){ - if(std::isalpha(c) || std::isdigit(c) || (c == '.' || (c == '_') || (c == '-' || (c == '~')))){ - out << c; - }else{ - out << '%' << std::hex << std::uppercase << static_cast(c); - } - } + for (const auto c : _str) { + if (std::isalpha(c) || std::isdigit(c) || + (c == '.' || (c == '_') || (c == '-' || (c == '~')))) { + out << c; + } else { + out << '%' << std::hex << std::uppercase << static_cast(c); + } + } - return out.str(); - } - - template - std::string join(const std::vector _vec){} + return out.str(); } + +template +std::string join(const std::vector _vec) {} +} // namespace CocoaTweet::Util diff --git a/src/cocoatweet/util/util.h b/src/cocoatweet/util/util.h index d39e28f..f8202fc 100644 --- a/src/cocoatweet/util/util.h +++ b/src/cocoatweet/util/util.h @@ -5,10 +5,10 @@ #include #include -namespace CocoaTweet::Util{ - std::string urlEncode(const std::string& _str); - template - std::string join(const std::vector _vec); -} +namespace CocoaTweet::Util { +std::string urlEncode(const std::string& _str); +template +std::string join(const std::vector _vec); +} // namespace CocoaTweet::Util #endif diff --git a/src/main.cc b/src/main.cc index 6fbe503..81ee57d 100644 --- a/src/main.cc +++ b/src/main.cc @@ -1,12 +1,12 @@ #include "cocoatweet/oauth/key.h" #include "cocoatweet/api/api.h" -auto main()->int{ - auto consumerKey = "JRKUmkKFWiC3f7K6msLKaNNuP"; - auto consumerSecret = "dTGI49MHRqa7XIFiPjwJR27vwolzsRaRXKA48iFlwAv4LK9Vlm"; - auto accessToken = "2224351076-uF2XTmYeDdAfIsixuvfrwt8puLiPuwGe4w7RM8I"; - auto accessTokenSecret = "dpCctbxzMjQ9AjZ6V7Fs6TIQlpPJo7JEkmjMfSO7QCEpW"; - CocoaTweet::OAuth::Key key(consumerKey, consumerSecret, accessToken, accessTokenSecret); - CocoaTweet::API::API api(key); - api.status().Update("CocoaTwitterAPI"); +auto main() -> int { + auto consumerKey = "JRKUmkKFWiC3f7K6msLKaNNuP"; + auto consumerSecret = "dTGI49MHRqa7XIFiPjwJR27vwolzsRaRXKA48iFlwAv4LK9Vlm"; + auto accessToken = "2224351076-uF2XTmYeDdAfIsixuvfrwt8puLiPuwGe4w7RM8I"; + auto accessTokenSecret = "dpCctbxzMjQ9AjZ6V7Fs6TIQlpPJo7JEkmjMfSO7QCEpW"; + CocoaTweet::OAuth::Key key(consumerKey, consumerSecret, accessToken, accessTokenSecret); + CocoaTweet::API::API api(key); + api.status().Update("tweet_from_Cocoa_Twitter_Library"); }