format
This commit is contained in:
@@ -4,7 +4,6 @@
|
||||
#include <string>
|
||||
#include <typeinfo>
|
||||
|
||||
|
||||
namespace CocoaTweet::OAuth {
|
||||
Key Key::fromJsonFile(const std::string _jsonFile) {
|
||||
std::ifstream ifs(_jsonFile);
|
||||
@@ -12,25 +11,22 @@ Key Key::fromJsonFile(const std::string _jsonFile) {
|
||||
|
||||
auto j = nlohmann::json::parse(str);
|
||||
Key key;
|
||||
if(j.contains("consumer_key")){
|
||||
if (j.contains("consumer_key")) {
|
||||
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>());
|
||||
}
|
||||
|
||||
|
||||
if(j.contains("access_token")){
|
||||
if (j.contains("access_token")) {
|
||||
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>());
|
||||
}
|
||||
|
||||
return key;
|
||||
return key;
|
||||
}
|
||||
} // namespace CocoaTweet::OAuth
|
||||
|
||||
@@ -20,25 +20,26 @@ public:
|
||||
consumerSecret_(_consumerSecret),
|
||||
accessToken_(_accessToken),
|
||||
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;
|
||||
}
|
||||
|
||||
void consumerSecret(const std::string& _consumerSecret){
|
||||
void consumerSecret(const std::string& _consumerSecret) {
|
||||
consumerSecret_ = _consumerSecret;
|
||||
}
|
||||
|
||||
void accessToken(const std::string& _accessToken){
|
||||
void accessToken(const std::string& _accessToken) {
|
||||
accessToken_ = _accessToken;
|
||||
}
|
||||
|
||||
void accessTokenSecret(const std::string& _accessTokenSecret){
|
||||
void accessTokenSecret(const std::string& _accessTokenSecret) {
|
||||
accessTokenSecret_ = _accessTokenSecret;
|
||||
}
|
||||
|
||||
void bearerToken(const std::string& _bearer){
|
||||
void bearerToken(const std::string& _bearer) {
|
||||
bearerToken_ = _bearer;
|
||||
}
|
||||
const std::string& consumerKey() const {
|
||||
@@ -54,7 +55,7 @@ public:
|
||||
return accessTokenSecret_;
|
||||
}
|
||||
|
||||
const std::string& bearerToken() const{
|
||||
const std::string& bearerToken() const {
|
||||
return bearerToken_;
|
||||
}
|
||||
|
||||
|
||||
@@ -23,7 +23,7 @@ extern "C" {
|
||||
#endif
|
||||
|
||||
namespace CocoaTweet::OAuth {
|
||||
OAuth1::OAuth1(): authType_(AuthType::OAuth) {}
|
||||
OAuth1::OAuth1() : authType_(AuthType::OAuth) {}
|
||||
|
||||
OAuth1::OAuth1(const Key _key) : key_(_key), authType_(AuthType::OAuth) {}
|
||||
|
||||
@@ -46,12 +46,14 @@ 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){
|
||||
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 authParam = oauthParam();
|
||||
auto sigingParam = authParam;
|
||||
if (!_bodyParam.empty()) {
|
||||
for (const auto [k, v] : _bodyParam) {
|
||||
@@ -62,7 +64,7 @@ const std::string OAuth1::calculateAuthHeader(std::map<std::string, std::string>
|
||||
auto sign = signature(sigingParam, _method, _url);
|
||||
|
||||
authParam.merge(sign);
|
||||
// ヘッダの構築
|
||||
// ヘッダの構築
|
||||
std::string oauthHeader = "authorization: OAuth ";
|
||||
{
|
||||
std::vector<std::string> tmp;
|
||||
@@ -75,14 +77,13 @@ const std::string OAuth1::calculateAuthHeader(std::map<std::string, std::string>
|
||||
return oauthHeader;
|
||||
}
|
||||
|
||||
|
||||
|
||||
const std::string& OAuth1::generateBearerToken(){
|
||||
auto signature = key_.consumerKey() + ":" + key_.consumerSecret();
|
||||
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 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
|
||||
@@ -118,8 +119,7 @@ const std::string& OAuth1::generateBearerToken(){
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
||||
auto j = nlohmann::json::parse(rcv);
|
||||
auto j = nlohmann::json::parse(rcv);
|
||||
if ((responseCode / 100) == 4) {
|
||||
auto error = j["errors"][0]["code"];
|
||||
auto message = j["errors"][0]["message"];
|
||||
@@ -128,14 +128,14 @@ const std::string& OAuth1::generateBearerToken(){
|
||||
throw new CocoaTweet::Exception::Exception(j["error"]);
|
||||
}
|
||||
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"]);
|
||||
authType_ = AuthType::Bearer;
|
||||
return key_.bearerToken();
|
||||
|
||||
}
|
||||
|
||||
const std::string OAuth1::nonce() const {
|
||||
|
||||
@@ -9,21 +9,17 @@
|
||||
namespace CocoaTweet::OAuth {
|
||||
class OAuth1 {
|
||||
public:
|
||||
|
||||
enum AuthType{
|
||||
OAuth,
|
||||
Bearer
|
||||
};
|
||||
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& 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;
|
||||
@@ -44,7 +40,6 @@ private:
|
||||
_stream->append(_ptr, realsize);
|
||||
return realsize;
|
||||
}
|
||||
|
||||
};
|
||||
} // namespace CocoaTweet::OAuth
|
||||
|
||||
|
||||
Reference in New Issue
Block a user