mastodonpp  0.5.5
C++ wrapper for the Mastodon and Pleroma APIs.
Public Member Functions | List of all members
mastodonpp::Instance::ObtainToken Class Reference

Simplifies obtaining an OAuth 2.0 Bearer Access Token. More...

#include <mastodonpp/instance.hpp>

Inheritance diagram for mastodonpp::Instance::ObtainToken:
Inheritance graph
[legend]
Collaboration diagram for mastodonpp::Instance::ObtainToken:
Collaboration graph
[legend]

Public Member Functions

 ObtainToken (Instance &instance)
 Constructor. More...
 
answer_type step_1 (string_view client_name, string_view scopes, string_view website)
 Creates an application via /api/v1/apps. More...
 
answer_type step_2 (string_view code)
 Creates a token via /oauth/token. More...
 
- Public Member Functions inherited from mastodonpp::CURLWrapper
 CURLWrapper ()
 Initializes curl and sets up connection. More...
 
 CURLWrapper (const CURLWrapper &)
 Copy constructor. Does the same as the Constructor. More...
 
 CURLWrapper (CURLWrapper &&other) noexcept=delete
 Move constructor. More...
 
virtual ~CURLWrapper () noexcept
 Cleans up curl and connection. More...
 
CURLWrapperoperator= (const CURLWrapper &other)=delete
 Copy assignment operator. More...
 
CURLWrapperoperator= (CURLWrapper &&other) noexcept=delete
 Move assignment operator. More...
 
CURL * get_curl_easy_handle ()
 Returns pointer to the CURL easy handle. More...
 
string escape_url (const string_view url) const
 URL encodes the given string. More...
 
string unescape_url (const string_view url) const
 URL decodes the given string. More...
 
void setup_connection_properties (string_view proxy, string_view access_token, string_view cainfo, string_view useragent)
 Set some properties of the connection. More...
 

Additional Inherited Members

- Protected Member Functions inherited from mastodonpp::CURLWrapper
answer_type make_request (const http_method &method, string uri, const parametermap &parameters)
 Make a HTTP request. More...
 
string & get_buffer ()
 Returns a reference to the buffer libcurl writes into. More...
 
void cancel_stream ()
 Cancel the stream. More...
 
virtual void set_proxy (string_view proxy)
 Set the proxy to use. More...
 
void set_access_token (string_view access_token)
 Set OAuth 2.0 Bearer Access Token. More...
 
virtual void set_cainfo (string_view path)
 Set path to Certificate Authority (CA) bundle. More...
 
virtual void set_useragent (string_view useragent)
 Sets the User-Agent. More...
 
- Protected Attributes inherited from mastodonpp::CURLWrapper
mutex _buffer_mutex
 Mutex for get_buffer a.k.a. _curl_buffer_body. More...
 

Detailed Description

Simplifies obtaining an OAuth 2.0 Bearer Access Token.

Example:

mastodonpp::Instance instance{"example.com", {}};
auto answer{token.step1("Good program", "read:blocks read:mutes", "")};
if (answer)
{
std::cout << "Please visit " << answer << "\nand paste the code: ";
std::string code;
std::cin >> code;
answer = access_token{token.step2(code)};
if (answer)
{
std::cout << "Success!\n";
}
}
Since
0.3.0

Constructor & Destructor Documentation

◆ ObtainToken()

mastodonpp::Instance::ObtainToken::ObtainToken ( Instance instance)
inlineexplicit

Constructor.

Since
0.3.0
257  : _instance{instance}
258  , _baseuri{instance.get_baseuri()}
259  {
260  _instance.copy_connection_properties(*this);
261  }

Member Function Documentation

◆ step_1()

answer_type mastodonpp::Instance::ObtainToken::step_1 ( string_view  client_name,
string_view  scopes,
string_view  website 
)

Creates an application via /api/v1/apps.

The body of the returned answer contains only the URI, not the whole JSON response.

Note that the required scopes may be different between Mastodon and other implementations, like Pleroma.

Parameters
client_nameThe name of your application.
scopesSpace separated list of scopes. Defaults to “read” if empty.
websiteThe URI to the homepage of your application. Can be an empty string.
Returns
The URI your user has to visit.
Since
0.3.0

◆ step_2()

answer_type mastodonpp::Instance::ObtainToken::step_2 ( string_view  code)

Creates a token via /oauth/token.

The body of the returned answer contains only the access token, not the whole JSON response.

The access token will be set in the Instance you initialized this ObtainToken with.

Parameters
codeThe authorization code you got from the user.
Returns
The access token.
Since
0.3.0

The documentation for this class was generated from the following file:
mastodonpp::Instance::ObtainToken
Simplifies obtaining an OAuth 2.0 Bearer Access Token.
Definition: instance.hpp:248
mastodonpp::Instance::copy_connection_properties
void copy_connection_properties(CURLWrapper &curlwrapper) const
Set the properties of the connection of the calling class up.
Definition: instance.hpp:90
mastodonpp::Instance
Holds the access data of an instance.
Definition: instance.hpp:48