OAuth2が出来るようになったよ。ブランチの目的からめちゃくちゃズレてるね
This commit is contained in:
@@ -9,6 +9,10 @@ API::API(CocoaTweet::OAuth::Key _key) {
|
||||
directMessage_ = DirectMessages::DirectMessage(oauth_);
|
||||
}
|
||||
|
||||
const std::string& API::generateBearerToken() const{
|
||||
return oauth_->generateBearerToken();
|
||||
}
|
||||
|
||||
Statuses::Status API::status() const {
|
||||
return status_;
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@ public:
|
||||
Medias::Media media() const;
|
||||
|
||||
DirectMessages::DirectMessage directMessage() const;
|
||||
const std::string& generateBearerToken() const;
|
||||
|
||||
private:
|
||||
Statuses::Status status_;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#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>
|
||||
@@ -125,6 +126,8 @@ void HttpPost::process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth,
|
||||
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) {
|
||||
|
||||
@@ -2,8 +2,6 @@
|
||||
#include <cocoatweet/api/model/mediaStore.h>
|
||||
#include <cocoatweet/exception/unsupportedMediaTypeException.h>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
|
||||
namespace CocoaTweet::API::Medias {
|
||||
const std::map<std::string, std::string> Upload::mimeType = {{".jpg", "image/jpeg"},
|
||||
{".jpeg", "image/jpeg"},
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
namespace CocoaTweet::API::Model {
|
||||
Tweet Tweet::parse(const std::string& _json) {
|
||||
auto j = nlohmann::json::parse(_json);
|
||||
auto j = nlohmann::json::parse(_json);
|
||||
Tweet tweet;
|
||||
|
||||
tweet.id(j["id_str"]);
|
||||
@@ -11,6 +11,8 @@ Tweet Tweet::parse(const std::string& _json) {
|
||||
tweet.text(j["text"]);
|
||||
tweet.source(j["source"]);
|
||||
|
||||
tweet.user(CocoaTweet::API::Model::User(j["user"].dump()));
|
||||
|
||||
return tweet;
|
||||
}
|
||||
|
||||
@@ -29,16 +31,27 @@ void Tweet::source(const std::string _source) {
|
||||
source_ = _source;
|
||||
}
|
||||
|
||||
void Tweet::user(const CocoaTweet::API::Model::User _user) {
|
||||
user_ = _user;
|
||||
}
|
||||
|
||||
const std::string Tweet::id() const {
|
||||
return id_;
|
||||
}
|
||||
|
||||
const std::string Tweet::createdAt() const {
|
||||
return createdAt_;
|
||||
}
|
||||
|
||||
const std::string Tweet::text() const {
|
||||
return text_;
|
||||
}
|
||||
|
||||
const std::string Tweet::source() const {
|
||||
return source_;
|
||||
}
|
||||
|
||||
const CocoaTweet::API::Model::User Tweet::user() const {
|
||||
return user_;
|
||||
}
|
||||
} // namespace CocoaTweet::API::Model
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#ifndef COCOATWEET_API_MODEL_TWEET_H_
|
||||
#define COCOATWEET_API_MODEL_TWEET_H_
|
||||
|
||||
#include <cocoatweet/api/model/user.h>
|
||||
#include <string>
|
||||
|
||||
namespace CocoaTweet::API::Model {
|
||||
@@ -43,6 +44,8 @@ public:
|
||||
/// @param[out] none
|
||||
void source(const std::string _source);
|
||||
|
||||
void user(const CocoaTweet::API::Model::User _user);
|
||||
|
||||
/// @brief get tweet id
|
||||
/// @param[in] none
|
||||
/// @param[out] const std::string : tweet id
|
||||
@@ -63,11 +66,14 @@ public:
|
||||
/// @param[out] const std::string : source information
|
||||
const std::string source() const;
|
||||
|
||||
const CocoaTweet::API::Model::User user() const;
|
||||
|
||||
private:
|
||||
std::string id_;
|
||||
std::string createdAt_;
|
||||
std::string text_;
|
||||
std::string source_;
|
||||
CocoaTweet::API::Model::User user_;
|
||||
};
|
||||
} // namespace CocoaTweet::API::Model
|
||||
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
#include <cocoatweet/api/model/user.h>
|
||||
#include "nlohmann/json.hpp"
|
||||
|
||||
namespace CocoaTweet::API::Model {
|
||||
CocoaTweet::API::Model::User User::parse(const std::string& _json) {
|
||||
auto j = nlohmann::json::parse(_json);
|
||||
User user;
|
||||
|
||||
user.id(j["id_str"]);
|
||||
return user;
|
||||
}
|
||||
|
||||
void User::id(const std::string& _id){
|
||||
id_ = _id;
|
||||
}
|
||||
|
||||
void User::name(const std::string& _name){
|
||||
name_ = _name;
|
||||
}
|
||||
|
||||
void User::screenName(const std::string& _screen){
|
||||
screenName_ = _screen;
|
||||
}
|
||||
|
||||
void User::location(const std::string& _location){
|
||||
location_ = _location;
|
||||
}
|
||||
|
||||
const std::string& User::id() const{
|
||||
return id_;
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // namespace CocoaTweet::API::Model
|
||||
@@ -0,0 +1,78 @@
|
||||
#ifndef COCOATWEET_API_MODEL_USER_H_
|
||||
#define COCOATWEET_API_MODEL_USER_H_
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace CocoaTweet::API::Model {
|
||||
|
||||
/// @brief data class for tweet object
|
||||
class User final {
|
||||
public:
|
||||
/// @brief constructor
|
||||
User() = default;
|
||||
|
||||
/// @brief copy constructor
|
||||
User(const User&) = default;
|
||||
|
||||
/// @brief constructor for create object from json response
|
||||
/// @param[in] const std::string& _json : received content from twitter endpoint
|
||||
User(const std::string& _json) : User(User::parse(_json)) {}
|
||||
|
||||
/// @brief response parser for user object
|
||||
/// @param[in] const std::string& _json : received content from twitter endpoint
|
||||
/// @param[out] CocoaTweet::API::Model::User
|
||||
static User parse(const std::string& _json);
|
||||
|
||||
void id(const std::string& _id);
|
||||
void name(const std::string& _name);
|
||||
void screenName(const std::string& _screen);
|
||||
void location(const std::string& _location);
|
||||
void url(const std::string& _url);
|
||||
void description(const std::string& _description);
|
||||
void protectedUser(const bool _protected);
|
||||
void follower(const long _follower);
|
||||
void follow(const long _follow);
|
||||
void listed(const long _listed);
|
||||
void favorite(const long _favorite);
|
||||
void tweet(const long _tweet);
|
||||
void createdAt(const std::string& _created);
|
||||
void bannerUrl(const std::string& _banner);
|
||||
void iconUrl(const std::string& _icon);
|
||||
|
||||
|
||||
const std::string& id() const;
|
||||
const std::string& name() const;
|
||||
const std::string& screenName() const;
|
||||
const std::string& location() const;
|
||||
const std::string& url() const;
|
||||
const std::string& description() const;
|
||||
bool protectedUser() const;
|
||||
long follower() const;
|
||||
long follow() const;
|
||||
long listed() const;
|
||||
long favorite() const;
|
||||
long tweet() const;
|
||||
const std::string& created() const;
|
||||
const std::string& banner() const;
|
||||
const std::string& icon() const;
|
||||
|
||||
private:
|
||||
std::string id_;
|
||||
std::string name_;
|
||||
std::string screenName_;
|
||||
std::string location_;
|
||||
std::string url_;
|
||||
std::string description_;
|
||||
bool protectedUser_;
|
||||
long follower_;
|
||||
long follow_;
|
||||
long listed_;
|
||||
long favorite_;
|
||||
long tweet_;
|
||||
std::string createdAt_;
|
||||
std::string bannerUrl_;
|
||||
std::string iconUrl_;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -1,5 +1,4 @@
|
||||
#include <cocoatweet/api/status/retweet.h>
|
||||
#include <iostream>
|
||||
|
||||
namespace CocoaTweet::API::Statuses {
|
||||
Retweet::Retweet() {}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
#include <cocoatweet/api/status/unretweet.h>
|
||||
#include <iostream>
|
||||
|
||||
namespace CocoaTweet::API::Statuses {
|
||||
Unretweet::Unretweet() {}
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
#include <cocoatweet/exception/exception.h>
|
||||
|
||||
namespace CocoaTweet::Exception {
|
||||
class AuthenticateException final : Exception {
|
||||
class AuthenticateException final : public Exception {
|
||||
using Exception::Exception;
|
||||
};
|
||||
} // namespace CocoaTweet::Exception
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
#ifndef COCOATWEET_EXCEPTION_INVAILDPARAMETEREXCEPTION_H_
|
||||
#define COCOATWEET_EXCEPTION_INVAILDPARAMETEREXCEPTION_H_
|
||||
|
||||
#include <cocoatweet/exception/exception.h>
|
||||
|
||||
namespace CocoaTweet::Exception {
|
||||
class InvalidParameterException final : public Exception {
|
||||
using Exception::Exception;
|
||||
};
|
||||
} // namespace CocoaTweet::Exception
|
||||
|
||||
#endif
|
||||
@@ -4,7 +4,7 @@
|
||||
#include <cocoatweet/exception/exception.h>
|
||||
|
||||
namespace CocoaTweet::Exception {
|
||||
class RateLimitException final : Exception {
|
||||
class RateLimitException final : public Exception {
|
||||
using Exception::Exception;
|
||||
};
|
||||
} // namespace CocoaTweet::Exception
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
#ifndef COCOATWEET_EXCEPTION_TOKENINVALIDEXCEPTION_H_
|
||||
#define COCOATWEET_EXCEPTION_TOKENINVALIDEXCEPTION_H_
|
||||
|
||||
#include <cocoatweet/exception/exception.h>
|
||||
|
||||
namespace CocoaTweet::Exception {
|
||||
class TokenInvalidException final : public Exception {
|
||||
using Exception::Exception;
|
||||
};
|
||||
} // namespace CocoaTweet::Exception
|
||||
|
||||
#endif
|
||||
@@ -4,7 +4,7 @@
|
||||
#include <cocoatweet/exception/exception.h>
|
||||
|
||||
namespace CocoaTweet::Exception {
|
||||
class TweetDuplicateException final : Exception {
|
||||
class TweetDuplicateException final : public Exception {
|
||||
using Exception::Exception;
|
||||
};
|
||||
} // namespace CocoaTweet::Exception
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
#include <cocoatweet/exception/exception.h>
|
||||
|
||||
namespace CocoaTweet::Exception {
|
||||
class TweetNotFoundException final : Exception {
|
||||
class TweetNotFoundException final : public Exception {
|
||||
using Exception::Exception;
|
||||
};
|
||||
} // namespace CocoaTweet::Exception
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
#include <cocoatweet/exception/exception.h>
|
||||
|
||||
namespace CocoaTweet::Exception {
|
||||
class TweetTooLongException final : Exception {
|
||||
class TweetTooLongException final : public Exception {
|
||||
using Exception::Exception;
|
||||
};
|
||||
} // namespace CocoaTweet::Exception
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
#include <cocoatweet/exception/exception.h>
|
||||
|
||||
namespace CocoaTweet::Exception {
|
||||
class UnsupportedMediaTypeException final : Exception {
|
||||
class UnsupportedMediaTypeException final : public Exception {
|
||||
using Exception::Exception;
|
||||
};
|
||||
} // namespace CocoaTweet::Exception
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
#include "nlohmann/json.hpp"
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <typeinfo>
|
||||
|
||||
|
||||
namespace CocoaTweet::OAuth {
|
||||
Key Key::fromJsonFile(const std::string _jsonFile) {
|
||||
@@ -9,7 +11,26 @@ Key Key::fromJsonFile(const std::string _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"]);
|
||||
Key key;
|
||||
if(j.contains("consumer_key")){
|
||||
key.consumerKey(j["consumer_key"].get<std::string>());
|
||||
}
|
||||
|
||||
|
||||
if(j.contains("consumer_secret")){
|
||||
key.consumerSecret(j["consumer_secret"].get<std::string>());
|
||||
}
|
||||
|
||||
|
||||
if(j.contains("access_token")){
|
||||
key.accessToken(j["access_token"].get<std::string>());
|
||||
}
|
||||
|
||||
|
||||
if(j.contains("access_token_secret")){
|
||||
key.accessTokenSecret(j["access_token_secret"].get<std::string>());
|
||||
}
|
||||
|
||||
return key;
|
||||
}
|
||||
} // namespace CocoaTweet::OAuth
|
||||
|
||||
@@ -6,10 +6,11 @@
|
||||
|
||||
namespace CocoaTweet::OAuth {
|
||||
class Key {
|
||||
const std::string consumerKey_;
|
||||
const std::string consumerSecret_;
|
||||
const std::string accessToken_;
|
||||
const std::string accessTokenSecret_;
|
||||
std::string consumerKey_;
|
||||
std::string consumerSecret_;
|
||||
std::string accessToken_;
|
||||
std::string accessTokenSecret_;
|
||||
std::string bearerToken_;
|
||||
|
||||
public:
|
||||
Key() : consumerKey_(""), consumerSecret_(""), accessToken_(""), accessTokenSecret_("") {}
|
||||
@@ -19,6 +20,27 @@ public:
|
||||
consumerSecret_(_consumerSecret),
|
||||
accessToken_(_accessToken),
|
||||
accessTokenSecret_(_accessTokenSecret) {}
|
||||
Key(const std::string& _consumerKey, const std::string& _consumerSecret): consumerKey_(_consumerKey), consumerSecret_(_consumerSecret){}
|
||||
|
||||
void consumerKey(const std::string& _consumerKey){
|
||||
consumerKey_ = _consumerKey;
|
||||
}
|
||||
|
||||
void consumerSecret(const std::string& _consumerSecret){
|
||||
consumerSecret_ = _consumerSecret;
|
||||
}
|
||||
|
||||
void accessToken(const std::string& _accessToken){
|
||||
accessToken_ = _accessToken;
|
||||
}
|
||||
|
||||
void accessTokenSecret(const std::string& _accessTokenSecret){
|
||||
accessTokenSecret_ = _accessTokenSecret;
|
||||
}
|
||||
|
||||
void bearerToken(const std::string& _bearer){
|
||||
bearerToken_ = _bearer;
|
||||
}
|
||||
const std::string& consumerKey() const {
|
||||
return consumerKey_;
|
||||
}
|
||||
@@ -31,6 +53,11 @@ public:
|
||||
const std::string& accessTokenSecret() const {
|
||||
return accessTokenSecret_;
|
||||
}
|
||||
|
||||
const std::string& bearerToken() const{
|
||||
return bearerToken_;
|
||||
}
|
||||
|
||||
std::map<std::string, std::string> noSecret() const {
|
||||
return std::map<std::string, std::string>{{"oauth_consumer_key", consumerKey_},
|
||||
{"oauth_token", accessToken_}};
|
||||
|
||||
@@ -7,17 +7,25 @@
|
||||
#include <string>
|
||||
#include <cstring>
|
||||
#include <iterator>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
#include <cocoatweet/exception/invalidParameterException.h>
|
||||
|
||||
extern "C" {
|
||||
#include <openssl/hmac.h>
|
||||
#include <openssl/sha.h>
|
||||
#include <openssl/buffer.h>
|
||||
#include <curl/curl.h>
|
||||
}
|
||||
|
||||
namespace CocoaTweet::OAuth {
|
||||
OAuth1::OAuth1() {}
|
||||
#ifndef NDEBUG
|
||||
#include <iostream>
|
||||
#endif
|
||||
|
||||
OAuth1::OAuth1(const Key _key) : key_(_key) {}
|
||||
namespace CocoaTweet::OAuth {
|
||||
OAuth1::OAuth1(): authType_(AuthType::OAuth) {}
|
||||
|
||||
OAuth1::OAuth1(const Key _key) : key_(_key), authType_(AuthType::OAuth) {}
|
||||
|
||||
std::map<std::string, std::string> OAuth1::signature(
|
||||
const std::map<std::string, std::string>& _param, const std::string& _method,
|
||||
@@ -38,6 +46,98 @@ std::map<std::string, std::string> OAuth1::signature(
|
||||
return ret;
|
||||
}
|
||||
|
||||
const std::string OAuth1::calculateAuthHeader(std::map<std::string, std::string> _bodyParam, const std::string& _method, const std::string& _url){
|
||||
if(authType_ == AuthType::Bearer){
|
||||
return "Authorization: Bearer " + key_.bearerToken();
|
||||
}
|
||||
|
||||
auto authParam = oauthParam();
|
||||
auto sigingParam = authParam;
|
||||
if (!_bodyParam.empty()) {
|
||||
for (const auto [k, v] : _bodyParam) {
|
||||
sigingParam.insert_or_assign(k, v);
|
||||
}
|
||||
}
|
||||
|
||||
auto sign = signature(sigingParam, _method, _url);
|
||||
|
||||
authParam.merge(sign);
|
||||
// ヘッダの構築
|
||||
std::string oauthHeader = "authorization: OAuth ";
|
||||
{
|
||||
std::vector<std::string> tmp;
|
||||
for (const auto& [key, value] : authParam) {
|
||||
tmp.push_back(key + "=" + CocoaTweet::Util::urlEncode(value));
|
||||
}
|
||||
oauthHeader += CocoaTweet::Util::join(tmp, ",");
|
||||
}
|
||||
|
||||
return oauthHeader;
|
||||
}
|
||||
|
||||
|
||||
|
||||
const std::string& OAuth1::generateBearerToken(){
|
||||
auto signature = key_.consumerKey() + ":" + key_.consumerSecret();
|
||||
auto k64Signature = base64(signature);
|
||||
auto authHeader = std::string("Authorization: Basic ") + k64Signature;
|
||||
auto contentType = std::string("Content-Type: application/x-www-form-urlencoded;charset=UTF-8");
|
||||
auto url = std::string("https://api.twitter.com/oauth2/token");
|
||||
auto requestBody = std::string("grant_type=client_credentials");
|
||||
|
||||
// do post
|
||||
CURL* curl;
|
||||
CURLcode res;
|
||||
std::string rcv;
|
||||
long responseCode;
|
||||
curl = curl_easy_init();
|
||||
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_SSL_VERIFYPEER, 0L);
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curlCallback_);
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (std::string*)&rcv);
|
||||
#ifndef NDEBUG
|
||||
std::cout << "requestBody : " << requestBody << std::endl;
|
||||
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
|
||||
#endif
|
||||
// Headerを保持するcurl_slist*を初期化
|
||||
struct curl_slist* headers = NULL;
|
||||
// Authorizationをヘッダに追加
|
||||
headers = curl_slist_append(headers, authHeader.c_str());
|
||||
headers = curl_slist_append(headers, contentType.c_str());
|
||||
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
|
||||
res = curl_easy_perform(curl);
|
||||
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &responseCode);
|
||||
curl_easy_cleanup(curl);
|
||||
}
|
||||
if (res != CURLE_OK) {
|
||||
throw std::runtime_error(std::string("INTERNAL ERROR : curl(") + std::to_string(res) + ")");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
||||
auto j = nlohmann::json::parse(rcv);
|
||||
if ((responseCode / 100) == 4) {
|
||||
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>() == 44) {
|
||||
throw CocoaTweet::Exception::InvalidParameterException(message.get<std::string>().c_str());
|
||||
}
|
||||
}
|
||||
|
||||
key_.bearerToken(j["access_token"]);
|
||||
authType_ = AuthType::Bearer;
|
||||
return key_.bearerToken();
|
||||
|
||||
}
|
||||
|
||||
const std::string OAuth1::nonce() const {
|
||||
std::random_device engine;
|
||||
std::string nonceTable = "abcdefghijklmnopqrstuvwxyz0123456789";
|
||||
|
||||
@@ -9,11 +9,22 @@
|
||||
namespace CocoaTweet::OAuth {
|
||||
class OAuth1 {
|
||||
public:
|
||||
|
||||
enum AuthType{
|
||||
OAuth,
|
||||
Bearer
|
||||
};
|
||||
|
||||
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& generateBearerToken();
|
||||
const std::string calculateAuthHeader(std::map<std::string, std::string> _bodyParam, const std::string& _method, const std::string& _url);
|
||||
|
||||
|
||||
const std::string nonce() const;
|
||||
const std::string timestamp() const;
|
||||
const std::string method() const;
|
||||
@@ -24,9 +35,16 @@ public:
|
||||
const std::string base64(const std::string& _raw);
|
||||
|
||||
private:
|
||||
AuthType authType_;
|
||||
Key key_;
|
||||
const std::string SIGNATURE_METHOD_ = "HMAC-SHA1";
|
||||
const std::string OAUTH_VERSION_ = "1.0";
|
||||
static size_t curlCallback_(char* _ptr, size_t _size, size_t _nmemb, std::string* _stream) {
|
||||
int realsize = _size * _nmemb;
|
||||
_stream->append(_ptr, realsize);
|
||||
return realsize;
|
||||
}
|
||||
|
||||
};
|
||||
} // namespace CocoaTweet::OAuth
|
||||
|
||||
|
||||
@@ -32,4 +32,5 @@ std::string join(const std::vector<std::string> _vec, const std::string& _delim)
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
} // namespace CocoaTweet::Util
|
||||
|
||||
+22
-2
@@ -3,6 +3,20 @@
|
||||
#include <cocoatweet/api/directMessage/new.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <algorithm>
|
||||
#include <unistd.h>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
#include <cocoatweet/exception/exception.h>
|
||||
#include <cocoatweet/exception/rateLimitException.h>
|
||||
|
||||
|
||||
bool starts_with(const std::string& s, const std::string& prefix) {
|
||||
auto size = prefix.size();
|
||||
if (s.size() < size) return false;
|
||||
return std::equal(std::begin(prefix), std::end(prefix), std::begin(s));
|
||||
}
|
||||
|
||||
auto main() -> int {
|
||||
// Generate Key object
|
||||
@@ -16,14 +30,20 @@ auto main() -> int {
|
||||
// accessTokenSecret);
|
||||
|
||||
// also can generate Key object from JSON file
|
||||
// CocoaTweet::OAuth::Key key = CocoaTweet::OAuth::Key::fromJsonFile("api_key.json");
|
||||
CocoaTweet::OAuth::Key key = CocoaTweet::OAuth::Key::fromJsonFile("api_key.json");
|
||||
// auto oauth = CocoaTweet::OAuth::OAuth1(key);
|
||||
// oauth.GenerateBearerToken();
|
||||
|
||||
// std::cout << "sdfgwregfresgfresdwefgweragregreagretgreawgrt#$QTWREATGREWTGF$ERTF";
|
||||
// Generate API Entry object using Key object
|
||||
// CocoaTweet::API::API api(key);
|
||||
CocoaTweet::API::API api(key);
|
||||
// std::cout << api.generateBearerToken() << std::endl;
|
||||
|
||||
// Now, you can use a twitter api
|
||||
// auto status = api.status().update("Hello Twitter World via Cocoa Twitter Library");
|
||||
// api.favorite().create(status.id());
|
||||
// api.favorite().destroy(status.id());
|
||||
// api.status().destroy(status.id());
|
||||
auto timeline = api.status().userTimeline("milkcocoa0902");
|
||||
std::cout << timeline[0].user().id();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user