This commit is contained in:
keita
2021-10-07 20:55:23 +09:00
parent 5d32261b9e
commit 98ed362e12
11 changed files with 82 additions and 97 deletions
+1 -4
View File
@@ -40,7 +40,6 @@ void HttpGet::process(std::weak_ptr<CocoaTweet::OAuth::OAuth1> _oauth,
// // 作成した署名をエンドポイントへのパラメータ及びOAuthパラメータに登録 // // 作成した署名をエンドポイントへのパラメータ及びOAuthパラメータに登録
// oauthParam.merge(signature); // oauthParam.merge(signature);
// // ヘッダの構築 // // ヘッダの構築
// std::string oauthHeader = "authorization: OAuth "; // std::string oauthHeader = "authorization: OAuth ";
// { // {
@@ -126,9 +125,7 @@ std::cout << rcv << std::endl;
throw CocoaTweet::Exception::RateLimitException(message.get<std::string>().c_str()); throw CocoaTweet::Exception::RateLimitException(message.get<std::string>().c_str());
} else if (error.get<int>() == 186) { } else if (error.get<int>() == 186) {
throw CocoaTweet::Exception::TweetTooLongException(message.get<std::string>().c_str()); throw CocoaTweet::Exception::TweetTooLongException(message.get<std::string>().c_str());
} } else {
else{
} }
} }
-2
View File
@@ -30,6 +30,4 @@ const std::string& User::id() const{
return id_; return id_;
} }
} // namespace CocoaTweet::API::Model } // namespace CocoaTweet::API::Model
+1 -2
View File
@@ -39,7 +39,6 @@ public:
void bannerUrl(const std::string& _banner); void bannerUrl(const std::string& _banner);
void iconUrl(const std::string& _icon); void iconUrl(const std::string& _icon);
const std::string& id() const; const std::string& id() const;
const std::string& name() const; const std::string& name() const;
const std::string& screenName() const; const std::string& screenName() const;
@@ -73,6 +72,6 @@ private:
std::string bannerUrl_; std::string bannerUrl_;
std::string iconUrl_; std::string iconUrl_;
}; };
} } // namespace CocoaTweet::API::Model
#endif #endif
-4
View File
@@ -4,7 +4,6 @@
#include <string> #include <string>
#include <typeinfo> #include <typeinfo>
namespace CocoaTweet::OAuth { namespace CocoaTweet::OAuth {
Key Key::fromJsonFile(const std::string _jsonFile) { Key Key::fromJsonFile(const std::string _jsonFile) {
std::ifstream ifs(_jsonFile); std::ifstream ifs(_jsonFile);
@@ -16,17 +15,14 @@ Key Key::fromJsonFile(const std::string _jsonFile) {
key.consumerKey(j["consumer_key"].get<std::string>()); key.consumerKey(j["consumer_key"].get<std::string>());
} }
if (j.contains("consumer_secret")) { if (j.contains("consumer_secret")) {
key.consumerSecret(j["consumer_secret"].get<std::string>()); key.consumerSecret(j["consumer_secret"].get<std::string>());
} }
if (j.contains("access_token")) { if (j.contains("access_token")) {
key.accessToken(j["access_token"].get<std::string>()); key.accessToken(j["access_token"].get<std::string>());
} }
if (j.contains("access_token_secret")) { if (j.contains("access_token_secret")) {
key.accessTokenSecret(j["access_token_secret"].get<std::string>()); key.accessTokenSecret(j["access_token_secret"].get<std::string>());
} }
+2 -1
View File
@@ -20,7 +20,8 @@ public:
consumerSecret_(_consumerSecret), consumerSecret_(_consumerSecret),
accessToken_(_accessToken), accessToken_(_accessToken),
accessTokenSecret_(_accessTokenSecret) {} accessTokenSecret_(_accessTokenSecret) {}
Key(const std::string& _consumerKey, const std::string& _consumerSecret): consumerKey_(_consumerKey), consumerSecret_(_consumerSecret){} Key(const std::string& _consumerKey, const std::string& _consumerSecret)
: consumerKey_(_consumerKey), consumerSecret_(_consumerSecret) {}
void consumerKey(const std::string& _consumerKey) { void consumerKey(const std::string& _consumerKey) {
consumerKey_ = _consumerKey; consumerKey_ = _consumerKey;
+7 -7
View File
@@ -46,7 +46,9 @@ std::map<std::string, std::string> OAuth1::signature(
return ret; return ret;
} }
const std::string OAuth1::calculateAuthHeader(std::map<std::string, std::string> _bodyParam, const std::string& _method, const std::string& _url){ const std::string OAuth1::calculateAuthHeader(std::map<std::string, std::string> _bodyParam,
const std::string& _method,
const std::string& _url) {
if (authType_ == AuthType::Bearer) { if (authType_ == AuthType::Bearer) {
return "Authorization: Bearer " + key_.bearerToken(); return "Authorization: Bearer " + key_.bearerToken();
} }
@@ -75,13 +77,12 @@ const std::string OAuth1::calculateAuthHeader(std::map<std::string, std::string>
return oauthHeader; return oauthHeader;
} }
const std::string& OAuth1::generateBearerToken() { const std::string& OAuth1::generateBearerToken() {
auto signature = key_.consumerKey() + ":" + key_.consumerSecret(); auto signature = key_.consumerKey() + ":" + key_.consumerSecret();
auto k64Signature = base64(signature); auto k64Signature = base64(signature);
auto authHeader = std::string("Authorization: Basic ") + k64Signature; auto authHeader = std::string("Authorization: Basic ") + k64Signature;
auto contentType = std::string("Content-Type: application/x-www-form-urlencoded;charset=UTF-8"); 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 url = std::string("https://api.twitter.com/oauth2/token");
auto requestBody = std::string("grant_type=client_credentials"); auto requestBody = std::string("grant_type=client_credentials");
@@ -118,7 +119,6 @@ const std::string& OAuth1::generateBearerToken(){
exit(1); exit(1);
} }
auto j = nlohmann::json::parse(rcv); auto j = nlohmann::json::parse(rcv);
if ((responseCode / 100) == 4) { if ((responseCode / 100) == 4) {
auto error = j["errors"][0]["code"]; auto error = j["errors"][0]["code"];
@@ -128,14 +128,14 @@ const std::string& OAuth1::generateBearerToken(){
throw new CocoaTweet::Exception::Exception(j["error"]); throw new CocoaTweet::Exception::Exception(j["error"]);
} }
if (error.get<int>() == 44) { if (error.get<int>() == 44) {
throw CocoaTweet::Exception::InvalidParameterException(message.get<std::string>().c_str()); throw CocoaTweet::Exception::InvalidParameterException(
message.get<std::string>().c_str());
} }
} }
key_.bearerToken(j["access_token"]); key_.bearerToken(j["access_token"]);
authType_ = AuthType::Bearer; authType_ = AuthType::Bearer;
return key_.bearerToken(); return key_.bearerToken();
} }
const std::string OAuth1::nonce() const { const std::string OAuth1::nonce() const {
+3 -8
View File
@@ -9,11 +9,7 @@
namespace CocoaTweet::OAuth { namespace CocoaTweet::OAuth {
class OAuth1 { class OAuth1 {
public: public:
enum AuthType { OAuth, Bearer };
enum AuthType{
OAuth,
Bearer
};
OAuth1(); OAuth1();
OAuth1(const Key _key); OAuth1(const Key _key);
@@ -22,8 +18,8 @@ public:
const std::string& _url); const std::string& _url);
const std::string& generateBearerToken(); 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 calculateAuthHeader(std::map<std::string, std::string> _bodyParam,
const std::string& _method, const std::string& _url);
const std::string nonce() const; const std::string nonce() const;
const std::string timestamp() const; const std::string timestamp() const;
@@ -44,7 +40,6 @@ private:
_stream->append(_ptr, realsize); _stream->append(_ptr, realsize);
return realsize; return realsize;
} }
}; };
} // namespace CocoaTweet::OAuth } // namespace CocoaTweet::OAuth
-1
View File
@@ -11,7 +11,6 @@
#include <cocoatweet/exception/exception.h> #include <cocoatweet/exception/exception.h>
#include <cocoatweet/exception/rateLimitException.h> #include <cocoatweet/exception/rateLimitException.h>
bool starts_with(const std::string& s, const std::string& prefix) { bool starts_with(const std::string& s, const std::string& prefix) {
auto size = prefix.size(); auto size = prefix.size();
if (s.size() < size) return false; if (s.size() < size) return false;