ベアラトークンのモデル作成。その他

This commit is contained in:
keita
2022-04-23 20:56:50 +09:00
parent 5eef4a64e6
commit 9f2c61b7e0
10 changed files with 136 additions and 16 deletions
+14
View File
@@ -0,0 +1,14 @@
#include "bearerToken.h"
namespace CocoaTweet::API::Model {
BearerToken::BearerToken() : token_("") {}
BearerToken::BearerToken(const std::string _token)
: token_(_token) {}
const std::string& BearerToken::token() const {
return token_;
}
void BearerToken::token(const std::string& _token) {
token_ = _token;
}
} // namespace CocoaTweet::API::Model
+17
View File
@@ -0,0 +1,17 @@
#ifndef COCOATWEET_API_MODEL_BEARERTOKEN_H_
#define COCOATWEET_API_MODEL_BEARERTOKEN_H_
#include <string>
namespace CocoaTweet::API::Model {
class BearerToken {
std::string token_;
public:
BearerToken();
BearerToken(const std::string _token);
const std::string& token() const;
void token(const std::string& _token);
};
} // namespace CocoaTweet::API::Model
#endif
+10 -1
View File
@@ -2,6 +2,7 @@
#include <cocoatweet/util/util.h>
#include <iostream>
#include <cocoatweet/authentication/oauth.h>
namespace CocoaTweet::API::OAuth1 {
AccessToken::AccessToken() {
contentType_ = "application/x-www-form-urlencoded";
@@ -18,8 +19,16 @@ void AccessToken::oauthToken(const CocoaTweet::API::Model::OAuthToken _token) {
const CocoaTweet::API::Model::OAuthToken AccessToken::process(
std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth) {
auto key = oauth_.lock()->key();
key.authType(CocoaTweet::Authentication::Key::AUTH_TYPE::OAUTH10A);
key.accessToken(oauthToken_.oauthToken());
key.accessTokenSecret(oauthToken_.oauthTokenSecret());
auto oauth = std::make_shared<CocoaTweet::Authentication::OAuth1>(key);
CocoaTweet::API::Model::OAuthToken oauthToken;
HttpPost::process(_oauth, [&oauthToken](const std::string& _rcv) {
HttpPost::process(oauth, [&oauthToken](const std::string& _rcv) {
auto mp = CocoaTweet::Util::parse(_rcv, '&', '=');
if (mp.count("oauth_token")) {
oauthToken.oauthToken(mp.at("oauth_token"));
+10 -10
View File
@@ -8,13 +8,13 @@ OAuth::OAuth(std::shared_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oau
CocoaTweet::API::Model::OAuthToken OAuth::requestToken(
const std::string& _oauthCallback) const {
auto key = oauth_.lock()->key();
key.authType(CocoaTweet::Authentication::Key::AUTH_TYPE::OAUTH10A);
auto oauth = std::make_shared<CocoaTweet::Authentication::OAuth1>(key);
// auto key = oauth_.lock()->key();
// key.authType(CocoaTweet::Authentication::Key::AUTH_TYPE::OAUTH10A);
// auto oauth = std::make_shared<CocoaTweet::Authentication::OAuth1>(key);
CocoaTweet::API::OAuth1::RequestToken requestToken;
requestToken.oauthCallback(_oauthCallback);
return requestToken.process(oauth);
return requestToken.process(oauth_);
}
const std::string OAuth::authorize(const CocoaTweet::API::Model::OAuthToken _oauthToken) const {
@@ -25,13 +25,13 @@ const std::string OAuth::authorize(const CocoaTweet::API::Model::OAuthToken _oau
const CocoaTweet::API::Model::OAuthToken OAuth::accessToken(
const CocoaTweet::API::Model::OAuthToken _oauthToken, const std::string _verifier) const {
auto key = oauth_.lock()->key();
key.authType(CocoaTweet::Authentication::Key::AUTH_TYPE::OAUTH10A);
key.accessToken(_oauthToken.oauthToken());
key.accessTokenSecret(_oauthToken.oauthTokenSecret());
auto oauth = std::make_shared<CocoaTweet::Authentication::OAuth1>(key);
// auto key = oauth_.lock()->key();
// key.authType(CocoaTweet::Authentication::Key::AUTH_TYPE::OAUTH10A);
// key.accessToken(_oauthToken.oauthToken());
// key.accessTokenSecret(_oauthToken.oauthTokenSecret());
// auto oauth = std::make_shared<CocoaTweet::Authentication::OAuth1>(key);
CocoaTweet::API::OAuth1::AccessToken accessToken;
accessToken.oauthVerifier(_verifier);
return accessToken.process(oauth);
return accessToken.process(oauth_);
}
} // namespace CocoaTweet::API::OAuth1
+6 -1
View File
@@ -1,6 +1,7 @@
#include <cocoatweet/api/oauth1/requestToken.h>
#include <cocoatweet/util/util.h>
#include <iostream>
#include <cocoatweet/authentication/oauth.h>
namespace CocoaTweet::API::OAuth1 {
RequestToken::RequestToken() {
@@ -14,8 +15,12 @@ void RequestToken::oauthCallback(const std::string& _oauthCallback) {
CocoaTweet::API::Model::OAuthToken RequestToken::process(
std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth) {
auto key = oauth_.lock()->key();
key.authType(CocoaTweet::Authentication::Key::AUTH_TYPE::OAUTH10A);
auto oauth = std::make_shared<CocoaTweet::Authentication::OAuth1>(key);
CocoaTweet::API::Model::OAuthToken oauthToken;
HttpPost::process(_oauth, [&oauthToken](const std::string& _rcv) {
HttpPost::process(oauth, [&oauthToken](const std::string& _rcv) {
auto mp = CocoaTweet::Util::parse(_rcv, '&', '=');
if (mp.count("oauth_token")) {
oauthToken.oauthToken(mp.at("oauth_token"));
@@ -0,0 +1,30 @@
#include <cocoatweet/api/oauth2/invalidateToken.h>
#include "nlohmann/json.hpp"
namespace CocoaTweet::API::OAuth2 {
InvalidateToken::InvalidateToken() {
contentType_ = "application/x-www-form-urlencoded";
url_ = "https://api.twitter.com/oauth2/invalidate_token";
}
void InvalidateToken::accessToken(const std::string _bearer){
// bodyParam_.insert_or_assign("access_token", _bearer);
bearer_ = _bearer;
}
const CocoaTweet::API::Model::BearerToken InvalidateToken::process(
std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth) {
auto org = url_;
auto url = url_ + "?access_token=" + bearer_;
url_ = url;
auto basic = std::make_shared<CocoaTweet::Authentication::Basic>(_oauth.lock()->key());
CocoaTweet::API::Model::BearerToken bearer;
HttpPost::process(basic, [&bearer](const std::string& _rcv) {
auto j = nlohmann::json::parse(_rcv);
bearer.token(j["access_token"]);
});
url_ = org;
return bearer;
}
} // namespace CocoaTweet::API::OAuth2
@@ -1,6 +1,22 @@
#ifndef COCOATWEET_API_OAUTH2_INVALIDATETOKEN_H_
#define COCOATWEET_API_OAUTH2_INVALIDATETOKEN_H_
#include <cocoatweet/api/model/bearerToken.h>
#include <cocoatweet/api/interface/httpPost.h>
#include <cocoatweet/authentication/basic.h>
namespace CocoaTweet::API::OAuth2{
class InvalidateToken : public CocoaTweet::API::Interface::HttpPost {
public:
InvalidateToken();
void accessToken(const std::string _bearer);
const CocoaTweet::API::Model::BearerToken process(
std::weak_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth);
private:
std::string bearer_;
};
}
#endif
+12 -3
View File
@@ -7,10 +7,19 @@ OAuth2::OAuth2(std::shared_ptr<CocoaTweet::Authentication::AuthenticatorBase> _o
}
const std::string OAuth2::token() const {
auto key = oauth_.lock()->key();
auto oauth = std::make_shared<CocoaTweet::Authentication::Basic>(key);
// auto key = oauth_.lock()->key();
// auto oauth = std::make_shared<CocoaTweet::Authentication::Basic>(key);
CocoaTweet::API::OAuth2::Token token;
return token.process(oauth);
return token.process(oauth_);
}
const CocoaTweet::API::Model::BearerToken OAuth2::invalidateToken(const std::string& _bearer) const {
// auto key = oauth_.lock()->key();
// auto oauth = std::make_shared<CocoaTweet::Authentication::Basic>(key);
CocoaTweet::API::OAuth2::InvalidateToken invalidateToken;
invalidateToken.accessToken(_bearer);
return invalidateToken.process(oauth_);
}
} // namespace CocoaTweet::API::OAuth2
+4
View File
@@ -2,8 +2,10 @@
#define COCOATWEET_API_OAUTH2_OAUTH2_H_
#include "cocoatweet/api/interface/groupInterface.h"
#include <cocoatweet/api/oauth2/invalidateToken.h>
#include <cocoatweet/api/oauth2/token.h>
#include <cocoatweet/api/model/oauthToken.h>
#include <cocoatweet/api/model/bearerToken.h>
#include <vector>
#include <utility>
@@ -20,6 +22,8 @@ public:
OAuth2(std::shared_ptr<CocoaTweet::Authentication::AuthenticatorBase> _oauth);
const std::string token() const;
const CocoaTweet::API::Model::BearerToken invalidateToken(const std::string& _bearer) const;
};
} // namespace CocoaTweet::API::OAuth2
+17 -1
View File
@@ -8,6 +8,8 @@
#include <cocoatweet/api/oauth2/token.h>
#include <cocoatweet/authentication/oauth.h>
#include <cocoatweet/util/util.h>
auto main() -> int {
// Generate Key object
// auto consumerKey = "your consumer key";
@@ -35,6 +37,20 @@ auto main() -> int {
auto user = api.user().show("milkcocoa9692");
std::cout << user.id() << std::endl;
std::cout << "consumerKey : " << key.consumerKey() << std::endl;
std::cout << "consumerSecret : " << key.consumerSecret() << std::endl;
std::cout << "bearer : " << key.bearerToken() << std::endl;
std::cout << "bearer : " << CocoaTweet::Util::urlDecode(key.bearerToken()) << std::endl;
auto newBearerToken = api.oauth2().invalidateToken(key.bearerToken());
key.bearerToken(newBearerToken.token());
api.swapKey(key);
user = api.user().show("milkcocoa9692");
std::cout << user.id() << std::endl;
// api.directMessage().messageCreate(
// user.id(),
// "これはクソみたいなスパムDMです。\nCocoaTwitterLibraryで、user/"
@@ -47,7 +63,7 @@ auto main() -> int {
// api.generateBearerToken();
// Now, you can use a twitter api
auto status = api.status().update("Hello Twitter World via Cocoa Twitter Library!!");
// auto status = api.status().update("Hello Twitter World via Cocoa Twitter Library!!");
// std::cout << status.id() << std::endl;
// api.favorite().create(status.id());
// api.favorite().destroy(status.id());