+3
-4
@@ -49,7 +49,6 @@ if(ENABLE_CODE_COVERAGE)
|
|||||||
endif()
|
endif()
|
||||||
|
|
||||||
# Required libraries
|
# Required libraries
|
||||||
find_package(Boost 1.61.0 COMPONENTS unit_test_framework REQUIRED)
|
|
||||||
find_package(PkgConfig REQUIRED)
|
find_package(PkgConfig REQUIRED)
|
||||||
find_package(OpenSSL REQUIRED)
|
find_package(OpenSSL REQUIRED)
|
||||||
if(NOT OPENSSL_FOUND)
|
if(NOT OPENSSL_FOUND)
|
||||||
@@ -62,14 +61,14 @@ find_package(CURL REQUIRED)
|
|||||||
if(NOT CURL_FOUND)
|
if(NOT CURL_FOUND)
|
||||||
message(FATAL_ERROR "Fail to find OpenSSL") # exit
|
message(FATAL_ERROR "Fail to find OpenSSL") # exit
|
||||||
endif()
|
endif()
|
||||||
message(FATAL_ERROR, "Fail to find libcurl")
|
|
||||||
include_directories(${CURL_INCLUDE_DIRS})
|
include_directories(${CURL_INCLUDE_DIRS})
|
||||||
|
|
||||||
enable_testing()
|
find_library(cpprest REQUIRED)
|
||||||
|
include_directories(${CPP_REST_INCLUDE_DIR})
|
||||||
|
|
||||||
|
|
||||||
include_directories(
|
include_directories(
|
||||||
${PROJECT_SOURCE_DIR}/src
|
${PROJECT_SOURCE_DIR}/src
|
||||||
${Boost_INCLUDE_DIRS}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
add_subdirectory(src)
|
add_subdirectory(src)
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR})
|
|||||||
add_executable(${PROJECT_NAME} main.cc)
|
add_executable(${PROJECT_NAME} main.cc)
|
||||||
target_link_libraries(${PROJECT_NAME}
|
target_link_libraries(${PROJECT_NAME}
|
||||||
lib-cocoatweet
|
lib-cocoatweet
|
||||||
${Boost_UNIT_TEST_FRAMEWORK_LIBRARY}
|
|
||||||
OpenSSL::SSL
|
OpenSSL::SSL
|
||||||
OpenSSL::Crypto
|
OpenSSL::Crypto
|
||||||
${CURL_LIBRARIES}
|
${CURL_LIBRARIES}
|
||||||
|
|||||||
@@ -1,7 +1,4 @@
|
|||||||
file(GLOB_RECURSE SOURCES ./*.cc)
|
file(GLOB_RECURSE SOURCES ./*.cc)
|
||||||
add_library(lib-cocoatweet ${SOURCES})
|
add_library(lib-cocoatweet ${SOURCES})
|
||||||
target_link_libraries(lib-cocoatweet PUBLIC
|
|
||||||
Boost::boost
|
|
||||||
)
|
|
||||||
target_include_directories(lib-cocoatweet PUBLIC ${PROJECT_SOURCE_DIR}/src)
|
target_include_directories(lib-cocoatweet PUBLIC ${PROJECT_SOURCE_DIR}/src)
|
||||||
set_target_properties(lib-cocoatweet PROPERTIES OUTPUT_NAME cocoatweet)
|
set_target_properties(lib-cocoatweet PROPERTIES OUTPUT_NAME cocoatweet)
|
||||||
|
|||||||
@@ -22,14 +22,17 @@ void Update::process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth,
|
|||||||
std::function<void(std::string)> _callback) {
|
std::function<void(std::string)> _callback) {
|
||||||
// エンドポイントへのパラメータにOAuthパラメータを付加して署名作成
|
// エンドポイントへのパラメータにOAuthパラメータを付加して署名作成
|
||||||
auto oauth = _oauth.lock();
|
auto oauth = _oauth.lock();
|
||||||
param_.merge(oauth->oauthParam());
|
auto oauthParam = oauth->oauthParam();
|
||||||
auto signature = oauth->signature(param_, "POST", url_);
|
auto sigingParam = oauthParam;
|
||||||
|
for (const auto [k, v] : param_) {
|
||||||
|
sigingParam.insert_or_assign(k, v);
|
||||||
|
}
|
||||||
|
|
||||||
|
auto signature = oauth->signature(sigingParam, "POST", url_);
|
||||||
|
|
||||||
// 作成した署名をエンドポイントへのパラメータ及びOAuthパラメータに登録
|
// 作成した署名をエンドポイントへのパラメータ及びOAuthパラメータに登録
|
||||||
param_.insert_or_assign("oauth_signature", signature["oauth_signature"]);
|
|
||||||
auto header = oauth->oauthParam();
|
|
||||||
std::cout << "signature : " << signature["oauth_signature"] << std::endl;
|
std::cout << "signature : " << signature["oauth_signature"] << std::endl;
|
||||||
header.insert_or_assign("oauth_signature", signature["oauth_signature"]);
|
oauthParam.merge(signature);
|
||||||
|
|
||||||
// リクエストボディの構築
|
// リクエストボディの構築
|
||||||
std::string requestBody = "";
|
std::string requestBody = "";
|
||||||
@@ -43,14 +46,13 @@ void Update::process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth,
|
|||||||
requestBody = os.str();
|
requestBody = os.str();
|
||||||
requestBody.erase(requestBody.size() - std::char_traits<char>::length("&"));
|
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::cout << "request Body -> " << requestBody << std::endl;
|
||||||
|
|
||||||
// ヘッダの構築
|
// ヘッダの構築
|
||||||
std::string oauthHeader = "Authorization: OAuth ";
|
std::string oauthHeader = "authorization: OAuth ";
|
||||||
{
|
{
|
||||||
std::vector<std::string> tmp;
|
std::vector<std::string> tmp;
|
||||||
for (const auto& [key, value] : header) {
|
for (const auto& [key, value] : oauthParam) {
|
||||||
tmp.push_back(key + "=" + value);
|
tmp.push_back(key + "=" + value);
|
||||||
}
|
}
|
||||||
std::stringstream os;
|
std::stringstream os;
|
||||||
@@ -58,7 +60,6 @@ void Update::process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth,
|
|||||||
oauthHeader += os.str();
|
oauthHeader += os.str();
|
||||||
oauthHeader.erase(oauthHeader.size() - std::char_traits<char>::length(","));
|
oauthHeader.erase(oauthHeader.size() - std::char_traits<char>::length(","));
|
||||||
}
|
}
|
||||||
//oauthHeader += (std::string(",") + "oauth_signature=" + signature["oauth_signature"]);
|
|
||||||
std::cout << "OAuth Header -> " << oauthHeader << std::endl;
|
std::cout << "OAuth Header -> " << oauthHeader << std::endl;
|
||||||
|
|
||||||
// do post
|
// do post
|
||||||
@@ -66,20 +67,23 @@ void Update::process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth,
|
|||||||
CURLcode res;
|
CURLcode res;
|
||||||
std::string rcv;
|
std::string rcv;
|
||||||
curl = curl_easy_init();
|
curl = curl_easy_init();
|
||||||
|
url_ = url_ + "?status=" + status_;
|
||||||
std::cout << "URL : " << url_ << std::endl;
|
std::cout << "URL : " << url_ << std::endl;
|
||||||
if (curl) {
|
if (curl) {
|
||||||
curl_easy_setopt(curl, CURLOPT_URL, url_.c_str());
|
curl_easy_setopt(curl, CURLOPT_URL, url_.c_str());
|
||||||
curl_easy_setopt(curl, CURLOPT_POST, 1);
|
curl_easy_setopt(curl, CURLOPT_POST, 1);
|
||||||
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, requestBody);
|
// curl_easy_setopt(curl, CURLOPT_POSTFIELDS, requestBody);
|
||||||
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, requestBody.length());
|
// 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_WRITEFUNCTION, curlCallback_);
|
||||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (std::string*)&rcv);
|
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*を初期化
|
// Headerを保持するcurl_slist*を初期化
|
||||||
struct curl_slist* headers = NULL;
|
struct curl_slist* headers = NULL;
|
||||||
// Authorizationをヘッダに追加
|
// Authorizationをヘッダに追加
|
||||||
headers = curl_slist_append(headers, oauthHeader.c_str());
|
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);
|
res = curl_easy_perform(curl);
|
||||||
curl_easy_cleanup(curl);
|
curl_easy_cleanup(curl);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,14 +34,16 @@ std::map<std::string, std::string> OAuth1::signature(
|
|||||||
query.erase(query.size() - std::char_traits<char>::length("&"));
|
query.erase(query.size() - std::char_traits<char>::length("&"));
|
||||||
|
|
||||||
auto significateKey = key().consumerSecret() + "&" + key().accessTokenSecret();
|
auto significateKey = key().consumerSecret() + "&" + key().accessTokenSecret();
|
||||||
auto significateBase = _method + "&" + CocoaTweet::Util::urlEncode(_url) + "&" + CocoaTweet::Util::urlEncode(query);
|
auto significateBase = _method + "&" + CocoaTweet::Util::urlEncode(_url) + "&" +
|
||||||
|
CocoaTweet::Util::urlEncode(query);
|
||||||
auto k64Sha1 = hmacSha1(significateKey, significateBase);
|
auto k64Sha1 = hmacSha1(significateKey, significateBase);
|
||||||
|
|
||||||
std::cout << "significate key : " << significateKey << std::endl;
|
std::cout << "significate key : " << significateKey << std::endl;
|
||||||
std::cout << "significate base : " << significateBase << std::endl;
|
std::cout << "significate base : " << significateBase << std::endl;
|
||||||
std::cout << "hmac-sha1 : " << k64Sha1 << 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;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -123,20 +125,19 @@ std::string OAuth1::hmacSha1(std::string _key, std::string _data) {
|
|||||||
|
|
||||||
auto sha1 = std::string(reinterpret_cast<char*>(result), length);
|
auto sha1 = std::string(reinterpret_cast<char*>(result), length);
|
||||||
|
|
||||||
|
|
||||||
// base64 encodeもやっちゃえ日産
|
// base64 encodeもやっちゃえ日産
|
||||||
BIO *encoder = BIO_new(BIO_f_base64());
|
BIO* encoder = BIO_new(BIO_f_base64());
|
||||||
BIO *bmem = BIO_new(BIO_s_mem());
|
BIO* bmem = BIO_new(BIO_s_mem());
|
||||||
encoder = BIO_push(encoder,bmem);
|
encoder = BIO_push(encoder, bmem);
|
||||||
BIO_write(encoder, sha1.c_str(), sha1.length());
|
BIO_write(encoder, sha1.c_str(), sha1.length());
|
||||||
BIO_flush(encoder);
|
BIO_flush(encoder);
|
||||||
|
|
||||||
BUF_MEM *bptr;
|
BUF_MEM* bptr;
|
||||||
BIO_get_mem_ptr(encoder,&bptr);
|
BIO_get_mem_ptr(encoder, &bptr);
|
||||||
|
|
||||||
char *k64 = (char *)std::malloc(bptr->length);
|
char* k64 = (char*)std::malloc(bptr->length);
|
||||||
std::memcpy(k64, bptr->data, bptr->length-1);
|
std::memcpy(k64, bptr->data, bptr->length - 1);
|
||||||
k64[bptr->length-1] = 0;
|
k64[bptr->length - 1] = 0;
|
||||||
|
|
||||||
BIO_free_all(encoder);
|
BIO_free_all(encoder);
|
||||||
|
|
||||||
|
|||||||
@@ -1,21 +1,22 @@
|
|||||||
#include "cocoatweet/util/util.h"
|
#include "cocoatweet/util/util.h"
|
||||||
#include <cctype>
|
#include <cctype>
|
||||||
|
|
||||||
namespace CocoaTweet::Util{
|
namespace CocoaTweet::Util {
|
||||||
std::string urlEncode(const std::string& _str){
|
std::string urlEncode(const std::string& _str) {
|
||||||
std::stringstream out;
|
std::stringstream out;
|
||||||
|
|
||||||
for(const auto c : _str){
|
for (const auto c : _str) {
|
||||||
if(std::isalpha(c) || std::isdigit(c) || (c == '.' || (c == '_') || (c == '-' || (c == '~')))){
|
if (std::isalpha(c) || std::isdigit(c) ||
|
||||||
|
(c == '.' || (c == '_') || (c == '-' || (c == '~')))) {
|
||||||
out << c;
|
out << c;
|
||||||
}else{
|
} else {
|
||||||
out << '%' << std::hex << std::uppercase << static_cast<int>(c);
|
out << '%' << std::hex << std::uppercase << static_cast<int>(c);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return out.str();
|
return out.str();
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
std::string join(const std::vector<T> _vec){}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
std::string join(const std::vector<T> _vec) {}
|
||||||
|
} // namespace CocoaTweet::Util
|
||||||
|
|||||||
@@ -5,10 +5,10 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
namespace CocoaTweet::Util{
|
namespace CocoaTweet::Util {
|
||||||
std::string urlEncode(const std::string& _str);
|
std::string urlEncode(const std::string& _str);
|
||||||
template<typename T>
|
template <typename T>
|
||||||
std::string join(const std::vector<T> _vec);
|
std::string join(const std::vector<T> _vec);
|
||||||
}
|
} // namespace CocoaTweet::Util
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
+2
-2
@@ -1,12 +1,12 @@
|
|||||||
#include "cocoatweet/oauth/key.h"
|
#include "cocoatweet/oauth/key.h"
|
||||||
#include "cocoatweet/api/api.h"
|
#include "cocoatweet/api/api.h"
|
||||||
|
|
||||||
auto main()->int{
|
auto main() -> int {
|
||||||
auto consumerKey = "JRKUmkKFWiC3f7K6msLKaNNuP";
|
auto consumerKey = "JRKUmkKFWiC3f7K6msLKaNNuP";
|
||||||
auto consumerSecret = "dTGI49MHRqa7XIFiPjwJR27vwolzsRaRXKA48iFlwAv4LK9Vlm";
|
auto consumerSecret = "dTGI49MHRqa7XIFiPjwJR27vwolzsRaRXKA48iFlwAv4LK9Vlm";
|
||||||
auto accessToken = "2224351076-uF2XTmYeDdAfIsixuvfrwt8puLiPuwGe4w7RM8I";
|
auto accessToken = "2224351076-uF2XTmYeDdAfIsixuvfrwt8puLiPuwGe4w7RM8I";
|
||||||
auto accessTokenSecret = "dpCctbxzMjQ9AjZ6V7Fs6TIQlpPJo7JEkmjMfSO7QCEpW";
|
auto accessTokenSecret = "dpCctbxzMjQ9AjZ6V7Fs6TIQlpPJo7JEkmjMfSO7QCEpW";
|
||||||
CocoaTweet::OAuth::Key key(consumerKey, consumerSecret, accessToken, accessTokenSecret);
|
CocoaTweet::OAuth::Key key(consumerKey, consumerSecret, accessToken, accessTokenSecret);
|
||||||
CocoaTweet::API::API api(key);
|
CocoaTweet::API::API api(key);
|
||||||
api.status().Update("CocoaTwitterAPI");
|
api.status().Update("tweet_from_Cocoa_Twitter_Library");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user