Merge pull request #1 from milkcocoa0902/develop
merge branch develop into master
This commit is contained in:
@@ -0,0 +1,11 @@
|
|||||||
|
Language: Cpp
|
||||||
|
BasedOnStyle: Google
|
||||||
|
AccessModifierOffset: -2
|
||||||
|
AlignConsecutiveAssignments: true
|
||||||
|
AllowShortFunctionsOnASingleLine: Empty
|
||||||
|
ColumnLimit: 96
|
||||||
|
Cpp11BracedListStyle: true
|
||||||
|
DerivePointerAlignment: false
|
||||||
|
SortIncludes: false
|
||||||
|
SpacesBeforeTrailingComments: 1
|
||||||
|
Standard: Cpp11
|
||||||
@@ -1,2 +1,3 @@
|
|||||||
build/
|
build/
|
||||||
*.swp
|
*.swp
|
||||||
|
src/main.cc
|
||||||
|
|||||||
@@ -0,0 +1,76 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.10)
|
||||||
|
|
||||||
|
project(CocoaTweet CXX C)
|
||||||
|
|
||||||
|
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")
|
||||||
|
|
||||||
|
# Disable in-source build
|
||||||
|
set(CMAKE_DISABLE_IN_SOURCE_BUILD ON)
|
||||||
|
set(CMAKE_DISABLE_SOURCE_CHANGES ON)
|
||||||
|
|
||||||
|
# Set default build type
|
||||||
|
if(NOT CMAKE_BUILD_TYPE)
|
||||||
|
set(CMAKE_BUILD_TYPE Release)
|
||||||
|
endif()
|
||||||
|
message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
|
||||||
|
|
||||||
|
# Set compiler flags
|
||||||
|
set(CMAKE_CXX_EXTENSIONS OFF)
|
||||||
|
set(CMAKE_CXX_STANDARD 17)
|
||||||
|
set(CXX_STANDARD_REQUIRED ON)
|
||||||
|
|
||||||
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -pedantic")
|
||||||
|
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g -DDEBUG")
|
||||||
|
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O2 -march=native -DNDEBUG")
|
||||||
|
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-O1,--sort-common,--as-needed,-z,relro")
|
||||||
|
|
||||||
|
if(CMAKE_GENERATOR STREQUAL "Ninja")
|
||||||
|
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
|
||||||
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fdiagnostics-color=always")
|
||||||
|
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
|
||||||
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fcolor-diagnostics")
|
||||||
|
endif()
|
||||||
|
endif()
|
||||||
|
|
||||||
|
# Sanitizers
|
||||||
|
if(ENABLE_SANITIZERS)
|
||||||
|
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
|
||||||
|
message(STATUS "Enabling GCC's address sanitizer")
|
||||||
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address")
|
||||||
|
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
|
||||||
|
message(STATUS "Enabling Clang's address sanitizer")
|
||||||
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fno-omit-frame-pointer")
|
||||||
|
endif()
|
||||||
|
endif()
|
||||||
|
|
||||||
|
# Code coverage
|
||||||
|
if(ENABLE_CODE_COVERAGE)
|
||||||
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
|
||||||
|
endif()
|
||||||
|
|
||||||
|
# Required libraries
|
||||||
|
find_package(PkgConfig REQUIRED)
|
||||||
|
find_package(OpenSSL REQUIRED)
|
||||||
|
if(NOT OPENSSL_FOUND)
|
||||||
|
message(FATAL_ERROR "Fail to find OpenSSL") # exit
|
||||||
|
endif()
|
||||||
|
message(STATUS "OPENSSL_INCLUDE_DIR: ${OPENSSL_INCLUDE_DIR}")
|
||||||
|
include_directories(${OPENSSL_INCLUDE_DIR})
|
||||||
|
|
||||||
|
find_package(CURL REQUIRED)
|
||||||
|
if(NOT CURL_FOUND)
|
||||||
|
message(FATAL_ERROR "Fail to find OpenSSL") # exit
|
||||||
|
endif()
|
||||||
|
include_directories(${CURL_INCLUDE_DIRS})
|
||||||
|
|
||||||
|
find_library(cpprest REQUIRED)
|
||||||
|
include_directories(${CPP_REST_INCLUDE_DIR})
|
||||||
|
|
||||||
|
|
||||||
|
include_directories(
|
||||||
|
${PROJECT_SOURCE_DIR}/src
|
||||||
|
${PROJECT_SOURCE_DIR}/third
|
||||||
|
)
|
||||||
|
|
||||||
|
add_subdirectory(src)
|
||||||
|
#add_subdirectory(test)
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
# CocoaTweet
|
||||||
|
This is a library for using Twitter API from C++
|
||||||
|
|
||||||
|
# Dependency
|
||||||
|
- libcurl
|
||||||
|
- libssl
|
||||||
|
|
||||||
|
# Features
|
||||||
|
- statuses/update
|
||||||
|
|
||||||
|
Now, only post a tweet.
|
||||||
|
|
||||||
|
# How
|
||||||
|
## API Key Registration
|
||||||
|
### 1.Write Key into code
|
||||||
|
write api key into code and create Key object use it.
|
||||||
|
```
|
||||||
|
#include "cocoatweet/oauth/key.h"
|
||||||
|
|
||||||
|
auto consumerKey = "your consumer key";
|
||||||
|
auto consumerSecret = "your consumer secret";
|
||||||
|
auto accessToken = "your access token";
|
||||||
|
auto accessTokenSecret = "your access token secret";
|
||||||
|
|
||||||
|
CocoaTweet::OAuth::Key key(consumerKey, consumerSecret, accessToken, accessTokenSecret);
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### 2. Load Key from JSON file
|
||||||
|
prepare file which written 'api-key' with json format.
|
||||||
|
```
|
||||||
|
{
|
||||||
|
"consumer_key" : "your consumer key",
|
||||||
|
"consumer_secret" : "your consumer secret",
|
||||||
|
"access_token" : "your access token",
|
||||||
|
"access_token_secret" : "your access token secret"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
then you can load api key from json file.
|
||||||
|
```
|
||||||
|
#include "cocoatweet/oauth/key.h"
|
||||||
|
|
||||||
|
CocoaTweet::OAuth::Key key = CocoaTweet::OAuth::Key::fromJsonFile("api_key.json");
|
||||||
|
```
|
||||||
|
|
||||||
|
## Generate API object
|
||||||
|
generating API object with Key.
|
||||||
|
this object is API entry point.
|
||||||
|
|
||||||
|
```
|
||||||
|
#include "cocoatweet/api/api.h"
|
||||||
|
|
||||||
|
CocoaTweet::API::API api(key);
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
## Post Tweet
|
||||||
|
post tweet
|
||||||
|
```
|
||||||
|
api.status().Update("Hello, World!!\nTweet from Cocoa Twitter Library");
|
||||||
|
```
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
add_subdirectory(cocoatweet)
|
||||||
|
|
||||||
|
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR})
|
||||||
|
add_executable(${PROJECT_NAME} main.cc)
|
||||||
|
target_link_libraries(${PROJECT_NAME}
|
||||||
|
lib-cocoatweet
|
||||||
|
OpenSSL::SSL
|
||||||
|
OpenSSL::Crypto
|
||||||
|
${CURL_LIBRARIES}
|
||||||
|
)
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
file(GLOB_RECURSE SOURCES ./*.cc)
|
||||||
|
add_library(lib-cocoatweet ${SOURCES})
|
||||||
|
target_include_directories(lib-cocoatweet PUBLIC ${PROJECT_SOURCE_DIR}/src)
|
||||||
|
set_target_properties(lib-cocoatweet PROPERTIES OUTPUT_NAME cocoatweet)
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
#include <cocoatweet/api/api.h>
|
||||||
|
|
||||||
|
namespace CocoaTweet::API {
|
||||||
|
API::API(CocoaTweet::OAuth::Key _key) {
|
||||||
|
oauth_ = std::make_shared<CocoaTweet::OAuth::OAuth1>(_key);
|
||||||
|
status_ = Statuses::Status(oauth_);
|
||||||
|
}
|
||||||
|
|
||||||
|
Statuses::Status API::status() const {
|
||||||
|
return status_;
|
||||||
|
}
|
||||||
|
} // namespace CocoaTweet::API
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
#ifndef COCOATWEET_API_H_
|
||||||
|
#define COCOATWEET_API_H_
|
||||||
|
|
||||||
|
#include "cocoatweet/api/status/status.h"
|
||||||
|
#include "cocoatweet/oauth/oauth.h"
|
||||||
|
|
||||||
|
namespace CocoaTweet::API {
|
||||||
|
class API {
|
||||||
|
public:
|
||||||
|
API(CocoaTweet::OAuth::Key _key);
|
||||||
|
Statuses::Status status() const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
Statuses::Status status_;
|
||||||
|
std::shared_ptr<CocoaTweet::OAuth::OAuth1> oauth_;
|
||||||
|
};
|
||||||
|
} // namespace CocoaTweet::API
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
#ifndef COCOATWEET_API_GROUPINTERFACE_H_
|
||||||
|
#define COCOATWEET_API_GROUPINTERFACE_H_
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
#include "cocoatweet/oauth/oauth.h"
|
||||||
|
|
||||||
|
namespace CocoaTweet::API {
|
||||||
|
class groupInterface {
|
||||||
|
protected:
|
||||||
|
std::weak_ptr<CocoaTweet::OAuth::OAuth1> oauth_;
|
||||||
|
};
|
||||||
|
} // namespace CocoaTweet::API
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,97 @@
|
|||||||
|
#include <cocoatweet/api/interface/postInterface.h>
|
||||||
|
#include "cocoatweet/util/util.h"
|
||||||
|
#include <iterator>
|
||||||
|
#include <memory>
|
||||||
|
#include <vector>
|
||||||
|
#include <sstream>
|
||||||
|
#include <iostream>
|
||||||
|
extern "C" {
|
||||||
|
#include <curl/curl.h>
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace CocoaTweet::API::Interface {
|
||||||
|
size_t postInterface::curlCallback_(char* _ptr, size_t _size, size_t _nmemb,
|
||||||
|
std::string* _stream) {
|
||||||
|
int realsize = _size * _nmemb;
|
||||||
|
_stream->append(_ptr, realsize);
|
||||||
|
return realsize;
|
||||||
|
}
|
||||||
|
|
||||||
|
void postInterface::process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth,
|
||||||
|
std::function<void(std::string)> _callback) {
|
||||||
|
// エンドポイントへのパラメータにOAuthパラメータを付加して署名作成
|
||||||
|
auto oauth = _oauth.lock();
|
||||||
|
auto oauthParam = oauth->oauthParam();
|
||||||
|
auto sigingParam = oauthParam;
|
||||||
|
for (const auto [k, v] : bodyParam_) {
|
||||||
|
sigingParam.insert_or_assign(k, v);
|
||||||
|
}
|
||||||
|
|
||||||
|
auto signature = oauth->signature(sigingParam, "POST", url_);
|
||||||
|
|
||||||
|
// 作成した署名をエンドポイントへのパラメータ及びOAuthパラメータに登録
|
||||||
|
std::cout << "signature : " << signature["oauth_signature"] << std::endl;
|
||||||
|
oauthParam.merge(signature);
|
||||||
|
|
||||||
|
// リクエストボディの構築
|
||||||
|
std::string requestBody = "";
|
||||||
|
{
|
||||||
|
std::vector<std::string> tmp;
|
||||||
|
for (const auto& [key, value] : bodyParam_) {
|
||||||
|
tmp.push_back(key + "=" + value);
|
||||||
|
}
|
||||||
|
std::stringstream os;
|
||||||
|
std::copy(tmp.begin(), tmp.end(), std::ostream_iterator<std::string>(os, "&"));
|
||||||
|
requestBody = os.str();
|
||||||
|
requestBody.erase(requestBody.size() - std::char_traits<char>::length("&"));
|
||||||
|
}
|
||||||
|
std::cout << "request Body -> " << requestBody << std::endl;
|
||||||
|
|
||||||
|
// ヘッダの構築
|
||||||
|
std::string oauthHeader = "authorization: OAuth ";
|
||||||
|
{
|
||||||
|
std::vector<std::string> tmp;
|
||||||
|
for (const auto& [key, value] : oauthParam) {
|
||||||
|
tmp.push_back(key + "=" + CocoaTweet::Util::urlEncode(value));
|
||||||
|
}
|
||||||
|
std::stringstream os;
|
||||||
|
std::copy(tmp.begin(), tmp.end(), std::ostream_iterator<std::string>(os, ","));
|
||||||
|
oauthHeader += os.str();
|
||||||
|
oauthHeader.erase(oauthHeader.size() - std::char_traits<char>::length(","));
|
||||||
|
}
|
||||||
|
std::cout << "OAuth Header -> " << oauthHeader << std::endl;
|
||||||
|
|
||||||
|
// do post
|
||||||
|
CURL* curl;
|
||||||
|
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.c_str());
|
||||||
|
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, requestBody.length());
|
||||||
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curlCallback_);
|
||||||
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (std::string*)&rcv);
|
||||||
|
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
|
||||||
|
// Headerを保持するcurl_slist*を初期化
|
||||||
|
struct curl_slist* headers = NULL;
|
||||||
|
// Authorizationをヘッダに追加
|
||||||
|
headers = curl_slist_append(headers, oauthHeader.c_str());
|
||||||
|
// 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);
|
||||||
|
}
|
||||||
|
if (res != CURLE_OK) {
|
||||||
|
std::cout << "curl error : " << res << std::endl;
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_callback) {
|
||||||
|
_callback(rcv);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} // namespace CocoaTweet::API::Interface
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
#ifndef COCOATWEET_API_INTERFACE_POSTINTERFACE_H_
|
||||||
|
#define COCOATWEET_API_INTERFACE_POSTINTERFACE_H_
|
||||||
|
|
||||||
|
#include <functional>
|
||||||
|
#include "cocoatweet/oauth/oauth.h"
|
||||||
|
|
||||||
|
namespace CocoaTweet::API::Interface {
|
||||||
|
class postInterface {
|
||||||
|
public:
|
||||||
|
void process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth,
|
||||||
|
std::function<void(std::string)> _callback);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
std::weak_ptr<CocoaTweet::OAuth::OAuth1> oauth_;
|
||||||
|
std::map<std::string, std::string> bodyParam_;
|
||||||
|
std::string url_;
|
||||||
|
static size_t curlCallback_(char* _ptr, size_t _size, size_t _nmemb, std::string* _stream);
|
||||||
|
};
|
||||||
|
} // namespace CocoaTweet::API::Interface
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
#include "cocoatweet/api/status/status.h"
|
||||||
|
#include "cocoatweet/api/status/update.h"
|
||||||
|
|
||||||
|
namespace CocoaTweet::API::Statuses {
|
||||||
|
Status::Status(std::shared_ptr<CocoaTweet::OAuth::OAuth1> _oauth) {
|
||||||
|
oauth_ = _oauth;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Status::Update(const std::string& _status) const {
|
||||||
|
CocoaTweet::API::Statuses::Update update;
|
||||||
|
update.status(_status);
|
||||||
|
update.process(oauth_, [](std::string _rcv) { std::cout << _rcv << std::endl; });
|
||||||
|
}
|
||||||
|
} // namespace CocoaTweet::API::Statuses
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
#ifndef COCOATWEET_API_STATUSED_H_
|
||||||
|
#define COCOATWEET_API_STATUSED_H_
|
||||||
|
|
||||||
|
#include "cocoatweet/api/interface/groupInterface.h"
|
||||||
|
#include "cocoatweet/oauth/oauth.h"
|
||||||
|
|
||||||
|
namespace CocoaTweet::API::Statuses {
|
||||||
|
class Status : public groupInterface {
|
||||||
|
public:
|
||||||
|
Status() = default;
|
||||||
|
Status(std::shared_ptr<CocoaTweet::OAuth::OAuth1> _oauth);
|
||||||
|
void Update(const std::string& _status) const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
};
|
||||||
|
} // namespace CocoaTweet::API::Statuses
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
#include "cocoatweet/api/status/update.h"
|
||||||
|
|
||||||
|
namespace CocoaTweet::API::Statuses {
|
||||||
|
Update::Update() {
|
||||||
|
url_ = "https://api.twitter.com/1.1/statuses/update.json";
|
||||||
|
}
|
||||||
|
void Update::status(const std::string _status) {
|
||||||
|
status_ = _status;
|
||||||
|
bodyParam_.insert_or_assign("status", status_);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace CocoaTweet::API::Statuses
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
#ifndef COCOATWEET_API_STATUSES_UPDATE_H_
|
||||||
|
#define COCOATWEET_API_STATUSES_UPDATE_H_
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
#include "cocoatweet/api/interface/postInterface.h"
|
||||||
|
//#include "cocoatweet/oauth/oauth.h"
|
||||||
|
|
||||||
|
namespace CocoaTweet::API::Statuses {
|
||||||
|
class Update : public CocoaTweet::API::Interface::postInterface {
|
||||||
|
public:
|
||||||
|
Update();
|
||||||
|
void status(const std::string _status);
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::string status_;
|
||||||
|
};
|
||||||
|
} // namespace CocoaTweet::API::Statuses
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
#include "cocoatweet/oauth/key.h"
|
||||||
|
#include "nlohmann/json.hpp"
|
||||||
|
#include <fstream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace CocoaTweet::OAuth {
|
||||||
|
Key Key::fromJsonFile(const std::string _jsonFile) {
|
||||||
|
std::ifstream ifs(_jsonFile);
|
||||||
|
std::string str((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
|
||||||
|
|
||||||
|
auto j = nlohmann::json::parse(str);
|
||||||
|
return Key(j["consumer_key"], j["consumer_secret"], j["access_token"],
|
||||||
|
j["access_token_secret"]);
|
||||||
|
}
|
||||||
|
} // namespace CocoaTweet::OAuth
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
#ifndef COCOATWEET_OAUTH_KEY_H_
|
||||||
|
#define COCOATWEET_OAUTH_KEY_H_
|
||||||
|
|
||||||
|
#include <map>
|
||||||
|
|
||||||
|
namespace CocoaTweet::OAuth {
|
||||||
|
class Key {
|
||||||
|
const std::string consumerKey_;
|
||||||
|
const std::string consumerSecret_;
|
||||||
|
const std::string accessToken_;
|
||||||
|
const std::string accessTokenSecret_;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Key() = default;
|
||||||
|
Key(const std::string& _consumerKey, const std::string& _consumerSecret,
|
||||||
|
const std::string& _accessToken, const std::string& _accessTokenSecret)
|
||||||
|
: consumerKey_(_consumerKey),
|
||||||
|
consumerSecret_(_consumerSecret),
|
||||||
|
accessToken_(_accessToken),
|
||||||
|
accessTokenSecret_(_accessTokenSecret) {}
|
||||||
|
const std::string& consumerKey() const {
|
||||||
|
return consumerKey_;
|
||||||
|
}
|
||||||
|
const std::string& consumerSecret() const {
|
||||||
|
return consumerSecret_;
|
||||||
|
}
|
||||||
|
const std::string& accessToken() const {
|
||||||
|
return accessToken_;
|
||||||
|
}
|
||||||
|
const std::string& accessTokenSecret() const {
|
||||||
|
return accessTokenSecret_;
|
||||||
|
}
|
||||||
|
std::map<std::string, std::string> noSecret() const {
|
||||||
|
return std::map<std::string, std::string>{{"oauth_consumer_key", consumerKey_},
|
||||||
|
{"oauth_token", accessToken_}};
|
||||||
|
}
|
||||||
|
const std::map<std::string, std::string> secret() const {
|
||||||
|
return std::map<std::string, std::string>{{"oauth_consumer_key", consumerSecret_},
|
||||||
|
{"oauth_token", accessTokenSecret_}};
|
||||||
|
}
|
||||||
|
|
||||||
|
static Key fromJsonFile(const std::string _jsonFile);
|
||||||
|
};
|
||||||
|
} // namespace CocoaTweet::OAuth
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,146 @@
|
|||||||
|
#include "oauth.h"
|
||||||
|
#include "cocoatweet/util/util.h"
|
||||||
|
#include <random>
|
||||||
|
#include <ctime>
|
||||||
|
#include <bitset>
|
||||||
|
#include <sstream>
|
||||||
|
#include <string>
|
||||||
|
#include <cstring>
|
||||||
|
#include <iterator>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
|
#include <openssl/hmac.h>
|
||||||
|
#include <openssl/sha.h>
|
||||||
|
#include <openssl/buffer.h>
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace CocoaTweet::OAuth {
|
||||||
|
OAuth1::OAuth1() {}
|
||||||
|
|
||||||
|
OAuth1::OAuth1(const Key _key) : key_(_key) {}
|
||||||
|
|
||||||
|
std::map<std::string, std::string> OAuth1::signature(
|
||||||
|
const std::map<std::string, std::string>& _param, const std::string& _method,
|
||||||
|
const std::string& _url) {
|
||||||
|
std::vector<std::string> tmp;
|
||||||
|
for (const auto& [key, value] : _param) {
|
||||||
|
tmp.push_back(key + "=" + value);
|
||||||
|
std::cout << (key + "=" + value) << std::endl;
|
||||||
|
}
|
||||||
|
std::ostringstream os;
|
||||||
|
std::copy(tmp.begin(), tmp.end(), std::ostream_iterator<std::string>(os, "&"));
|
||||||
|
std::string query = os.str();
|
||||||
|
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);
|
||||||
|
|
||||||
|
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", k64Sha1}};
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::string OAuth1::nonce() const {
|
||||||
|
std::random_device engine;
|
||||||
|
std::string nonceTable = "abcdefghijklmnopqrstuvwxyz0123456789";
|
||||||
|
std::uniform_int_distribution<std::size_t> dist(0, nonceTable.length() - 1);
|
||||||
|
std::string nonce;
|
||||||
|
|
||||||
|
for (auto i = 0; i < 32; ++i) {
|
||||||
|
nonce += nonceTable[dist(engine)];
|
||||||
|
}
|
||||||
|
|
||||||
|
return nonce;
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::string OAuth1::timestamp() const {
|
||||||
|
return std::to_string(time(nullptr));
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::string OAuth1::method() const {
|
||||||
|
return SIGNATURE_METHOD_;
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::string OAuth1::version() const {
|
||||||
|
return OAUTH_VERSION_;
|
||||||
|
}
|
||||||
|
|
||||||
|
const Key OAuth1::key() const {
|
||||||
|
return key_;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::map<std::string, std::string> OAuth1::oauthParam() const {
|
||||||
|
auto tmp = std::map<std::string, std::string>{{"oauth_nonce", nonce()},
|
||||||
|
{"oauth_signature_method", method()},
|
||||||
|
{"oauth_timestamp", timestamp()},
|
||||||
|
{"oauth_version", version()}};
|
||||||
|
tmp.merge(key().noSecret());
|
||||||
|
|
||||||
|
return tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::string OAuth1::base64(const std::string& _raw) {
|
||||||
|
auto base64Table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||||
|
std::stringstream ss;
|
||||||
|
for (auto r : _raw) {
|
||||||
|
ss << std::bitset<8>(r);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_raw.length() % 3 == 1) {
|
||||||
|
ss << "0000";
|
||||||
|
} else if (_raw.length() % 3 == 2) {
|
||||||
|
ss << "00";
|
||||||
|
}
|
||||||
|
|
||||||
|
auto bin = ss.str();
|
||||||
|
std::string base64 = "";
|
||||||
|
for (auto i = 0; i < bin.length() / 6; i++) {
|
||||||
|
base64 += base64Table[std::stoi(bin.substr(i * 6, 6), nullptr, 2)];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (base64.length() % 4 == 3) {
|
||||||
|
base64 += "=";
|
||||||
|
} else if (base64.length() % 4 == 2) {
|
||||||
|
base64 += "==";
|
||||||
|
} else if (base64.length() % 4 == 1) {
|
||||||
|
base64 += "===";
|
||||||
|
}
|
||||||
|
|
||||||
|
return base64;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string OAuth1::hmacSha1(std::string _key, std::string _data) {
|
||||||
|
unsigned char result[255];
|
||||||
|
unsigned int length = 255;
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
|
||||||
|
BIO_free_all(encoder);
|
||||||
|
|
||||||
|
return static_cast<std::string>(k64);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace CocoaTweet::OAuth
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
#ifndef COCOATWEET_OAUTH_OAUTH_H_
|
||||||
|
#define COCOATWEET_OAUTH_OAUTH_H_
|
||||||
|
|
||||||
|
#include <map>
|
||||||
|
#include <memory>
|
||||||
|
#include "key.h"
|
||||||
|
|
||||||
|
namespace CocoaTweet::OAuth {
|
||||||
|
class OAuth1 {
|
||||||
|
public:
|
||||||
|
OAuth1();
|
||||||
|
OAuth1(const Key _key);
|
||||||
|
std::map<std::string, std::string> signature(const std::map<std::string, std::string>& _param,
|
||||||
|
const std::string& _method,
|
||||||
|
const std::string& _url);
|
||||||
|
const std::string nonce() const;
|
||||||
|
const std::string timestamp() const;
|
||||||
|
const std::string method() const;
|
||||||
|
const std::string version() const;
|
||||||
|
const Key key() const;
|
||||||
|
std::map<std::string, std::string> oauthParam() const;
|
||||||
|
std::string hmacSha1(std::string _key, std::string _data);
|
||||||
|
const std::string base64(const std::string& _raw);
|
||||||
|
|
||||||
|
private:
|
||||||
|
Key key_;
|
||||||
|
const std::string SIGNATURE_METHOD_ = "HMAC-SHA1";
|
||||||
|
const std::string OAUTH_VERSION_ = "1.0";
|
||||||
|
};
|
||||||
|
} // namespace CocoaTweet::OAuth
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
#include "cocoatweet/util/util.h"
|
||||||
|
#include <cctype>
|
||||||
|
#include <iomanip>
|
||||||
|
|
||||||
|
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::setw(2) << std::setfill('0') << std::hex << std::uppercase
|
||||||
|
<< (0xFF & static_cast<int>(c));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return out.str();
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
std::string join(const std::vector<T> _vec) {}
|
||||||
|
} // namespace CocoaTweet::Util
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
#ifndef COCOATWEET_UTIL_UTIL_H_
|
||||||
|
#define COCOATWEET_UTIL_UTIL_H_
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
#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
|
||||||
|
|
||||||
|
#endif
|
||||||
+21
@@ -0,0 +1,21 @@
|
|||||||
|
#include "cocoatweet/oauth/key.h"
|
||||||
|
#include "cocoatweet/api/api.h"
|
||||||
|
|
||||||
|
auto main() -> int {
|
||||||
|
// キーオブジェクトを作成
|
||||||
|
// auto consumerKey = "your consumer key";
|
||||||
|
// auto consumerSecret = "your consumer secret";
|
||||||
|
// auto accessToken = "your access token";
|
||||||
|
// auto accessTokenSecret = "your access token secret";
|
||||||
|
// CocoaTweet::OAuth::Key key = CocoaTweet::OAuth::Key(consumerKey, consumerSecret,
|
||||||
|
// accessToken, accessTokenSecret);
|
||||||
|
|
||||||
|
// jsonファイルから各種キーを読み込むことも可能
|
||||||
|
CocoaTweet::OAuth::Key key = CocoaTweet::OAuth::Key::fromJsonFile("apikey.json");
|
||||||
|
|
||||||
|
// 作成したキーオブジェクトを用いてAPIを立ち上げる.
|
||||||
|
// 内部的にはキーオブジェクトを使用してOAuth認証機を立ち上げている.
|
||||||
|
CocoaTweet::API::API api(key);
|
||||||
|
|
||||||
|
api.status().Update("Hello Twitter World from Cocoa Twitter Library");
|
||||||
|
}
|
||||||
+25447
File diff suppressed because it is too large
Load Diff
Executable
+8
@@ -0,0 +1,8 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
. "$(dirname "$0")/isTopLevel.sh"
|
||||||
|
|
||||||
|
for f in $(git ls-files | grep -E '.*\.(cc|h)$'); do
|
||||||
|
echo "formatting ${f}"
|
||||||
|
clang-format -i "${f}"
|
||||||
|
done
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
if ! git rev-parse --is-inside-work-tree > /dev/null 2>&1; then
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ "$(pwd)" != "$(git rev-parse --show-toplevel)" ]; then
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
Executable
+14
@@ -0,0 +1,14 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
. "$(dirname "$0")/../isTopLevel.sh"
|
||||||
|
|
||||||
|
RETVAL=0
|
||||||
|
|
||||||
|
for f in $(git ls-files | grep -E '.*\.(cc|h)$'); do
|
||||||
|
if ! clang-format "${f}" | diff "${f}" - > /dev/null 2>&1; then
|
||||||
|
echo "Error: Invalid code formatting in ${f}" >&2
|
||||||
|
RETVAL=1
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
exit $RETVAL
|
||||||
Executable
+25
@@ -0,0 +1,25 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
. "$(dirname "$0")/../isTopLevel.sh"
|
||||||
|
|
||||||
|
RETVAL=0
|
||||||
|
|
||||||
|
for f in $(git ls-files | grep -E '^src\/.*\.h$'); do
|
||||||
|
# ファイルパスから正しいインクルードガードの文字列を生成する
|
||||||
|
s1="$(echo "$f" | sed -r 's/^src\///; s/[\/\.-]+/_/g; s/^.*$/\U&/')_"
|
||||||
|
|
||||||
|
# ファイルからインクルードガードを読み込む
|
||||||
|
s2=$(grep -Pzo '#ifndef\s+\K\b(\w+)\b(?=\s+#define\s+\b\1\b)' "$f" | tr '\0' '\n')
|
||||||
|
|
||||||
|
if [ -z "$s2" ]; then
|
||||||
|
echo "error: $f: include guard is missing or incorrect" >&2
|
||||||
|
echo " ($s1)" >&2
|
||||||
|
RETVAL=1
|
||||||
|
elif [ "$s2" != "$s1" ]; then
|
||||||
|
echo "error: $f: include guard is incorrect" >&2
|
||||||
|
echo " ($s2 => $s1)" >&2
|
||||||
|
RETVAL=1
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
exit $RETVAL
|
||||||
Reference in New Issue
Block a user