+3
-4
@@ -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)
|
||||
|
||||
@@ -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}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -21,15 +21,18 @@ void Update::status(const std::string _status) {
|
||||
void Update::process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth,
|
||||
std::function<void(std::string)> _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<CocoaTweet::OAuth::OAuth1> _oauth,
|
||||
requestBody = os.str();
|
||||
requestBody.erase(requestBody.size() - std::char_traits<char>::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<std::string> 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<CocoaTweet::OAuth::OAuth1> _oauth,
|
||||
oauthHeader += os.str();
|
||||
oauthHeader.erase(oauthHeader.size() - std::char_traits<char>::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<CocoaTweet::OAuth::OAuth1> _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);
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ std::map<std::string, std::string> OAuth1::signature(
|
||||
std::vector<std::string> 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<std::string>(os, "&"));
|
||||
@@ -34,14 +34,16 @@ std::map<std::string, std::string> OAuth1::signature(
|
||||
query.erase(query.size() - std::char_traits<char>::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<std::string, std::string>{{"oauth_signature", CocoaTweet::Util::urlEncode(k64Sha1)}};
|
||||
auto ret = std::map<std::string, std::string>{
|
||||
{"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<const unsigned char*>(_key.c_str()), _key.length(),
|
||||
reinterpret_cast<const unsigned char*>(_data.c_str()), _data.length(), result, &length);
|
||||
|
||||
auto sha1 = std::string(reinterpret_cast<char*>(result), length);
|
||||
auto sha1 = std::string(reinterpret_cast<char*>(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<std::string>(k64);
|
||||
|
||||
+16
-15
@@ -1,21 +1,22 @@
|
||||
#include "cocoatweet/util/util.h"
|
||||
#include <cctype>
|
||||
|
||||
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<int>(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<int>(c);
|
||||
}
|
||||
}
|
||||
|
||||
return out.str();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
std::string join(const std::vector<T> _vec){}
|
||||
return out.str();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
std::string join(const std::vector<T> _vec) {}
|
||||
} // namespace CocoaTweet::Util
|
||||
|
||||
@@ -5,10 +5,10 @@
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
|
||||
namespace CocoaTweet::Util{
|
||||
std::string urlEncode(const std::string& _str);
|
||||
template<typename T>
|
||||
std::string join(const std::vector<T> _vec);
|
||||
}
|
||||
namespace CocoaTweet::Util {
|
||||
std::string urlEncode(const std::string& _str);
|
||||
template <typename T>
|
||||
std::string join(const std::vector<T> _vec);
|
||||
} // namespace CocoaTweet::Util
|
||||
|
||||
#endif
|
||||
|
||||
+8
-8
@@ -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");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user