2020-01-14 23:20:02 +01:00
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
< html xmlns = "http://www.w3.org/1999/xhtml" >
< head >
< meta http-equiv = "Content-Type" content = "text/xhtml;charset=UTF-8" / >
< meta http-equiv = "X-UA-Compatible" content = "IE=9" / >
< meta name = "generator" content = "Doxygen 1.8.16" / >
< meta name = "viewport" content = "width=device-width, initial-scale=1" / >
< title > mastodonpp: example08_obtain_token.cpp< / title >
< link href = "tabs.css" rel = "stylesheet" type = "text/css" / >
< script type = "text/javascript" src = "jquery.js" > < / script >
< script type = "text/javascript" src = "dynsections.js" > < / script >
< link href = "search/search.css" rel = "stylesheet" type = "text/css" / >
< script type = "text/javascript" src = "search/searchdata.js" > < / script >
< script type = "text/javascript" src = "search/search.js" > < / script >
< link href = "doxygen.css" rel = "stylesheet" type = "text/css" / >
< / head >
< body >
< div id = "top" > <!-- do not remove this div, it is closed by doxygen! -->
< div id = "titlearea" >
< table cellspacing = "0" cellpadding = "0" >
< tbody >
< tr style = "height: 56px;" >
< td id = "projectalign" style = "padding-left: 0.5em;" >
< div id = "projectname" > mastodonpp
2020-01-16 18:34:50 +01:00
  < span id = "projectnumber" > 0.4.0< / span >
2020-01-14 23:20:02 +01:00
< / div >
< / td >
< / tr >
< / tbody >
< / table >
< / div >
<!-- end header part -->
<!-- Generated by Doxygen 1.8.16 -->
< script type = "text/javascript" >
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3& dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "search",false,'Search');
/* @license-end */
< / script >
< script type = "text/javascript" src = "menudata.js" > < / script >
< script type = "text/javascript" src = "menu.js" > < / script >
< script type = "text/javascript" >
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3& dn=gpl-2.0.txt GPL-v2 */
$(function() {
initMenu('',true,false,'search.php','Search');
$(document).ready(function() { init_search(); });
});
/* @license-end */< / script >
< div id = "main-nav" > < / div >
< / div > <!-- top -->
<!-- window showing the filter options -->
< div id = "MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
< / div >
<!-- iframe showing the search results (closed by default) -->
< div id = "MSearchResultsWindow" >
< iframe src = "javascript:void(0)" frameborder = "0"
name="MSearchResults" id="MSearchResults">
< / iframe >
< / div >
< div class = "header" >
< div class = "headertitle" >
< div class = "title" > example08_obtain_token.cpp< / div > < / div >
< / div > <!-- header -->
< div class = "contents" >
< div class = "fragment" > < div class = "line" > < span class = "comment" > /* This file is part of mastodonpp.< / span > < / div >
< div class = "line" > < span class = "comment" > * Copyright © 2020 tastytea < tastytea@tastytea.de> < / span > < / div >
< div class = "line" > < span class = "comment" > *< / span > < / div >
< div class = "line" > < span class = "comment" > * Permission to use, copy, modify, and/or distribute this software for any< / span > < / div >
< div class = "line" > < span class = "comment" > * purpose with or without fee is hereby granted.< / span > < / div >
< div class = "line" > < span class = "comment" > *< / span > < / div >
< div class = "line" > < span class = "comment" > * THE SOFTWARE IS PROVIDED " AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES< / span > < / div >
< div class = "line" > < span class = "comment" > * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF< / span > < / div >
< div class = "line" > < span class = "comment" > * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY< / span > < / div >
< div class = "line" > < span class = "comment" > * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES< / span > < / div >
< div class = "line" > < span class = "comment" > * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION< / span > < / div >
< div class = "line" > < span class = "comment" > * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN< / span > < / div >
< div class = "line" > < span class = "comment" > * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.< / span > < / div >
< div class = "line" > < span class = "comment" > */< / span > < / div >
< div class = "line" > < / div >
< div class = "line" > < span class = "comment" > // Obtain an access token and verify that it works.< / span > < / div >
< div class = "line" > < / div >
< div class = "line" > < span class = "preprocessor" > #include " mastodonpp.hpp" < / span > < / div >
< div class = "line" > < / div >
< div class = "line" > < span class = "preprocessor" > #include < cstdlib> < / span > < / div >
< div class = "line" > < span class = "preprocessor" > #include < iostream> < / span > < / div >
< div class = "line" > < span class = "preprocessor" > #include < string> < / span > < / div >
< div class = "line" > < span class = "preprocessor" > #include < string_view> < / span > < / div >
< div class = "line" > < span class = "preprocessor" > #include < vector> < / span > < / div >
< div class = "line" > < / div >
< div class = "line" > < span class = "keyword" > namespace < / span > masto = < a class = "code" href = "namespacemastodonpp.html" > mastodonpp< / a > ;< / div >
< div class = "line" > < span class = "keyword" > using< / span > std::exit;< / div >
< div class = "line" > < span class = "keyword" > using< / span > std::cout;< / div >
< div class = "line" > < span class = "keyword" > using< / span > std::cerr;< / div >
< div class = "line" > < span class = "keyword" > using< / span > std::endl;< / div >
< div class = "line" > < span class = "keyword" > using< / span > std::cin;< / div >
< div class = "line" > < span class = "keyword" > using< / span > std::string;< / div >
< div class = "line" > < span class = "keyword" > using< / span > std::to_string;< / div >
< div class = "line" > < span class = "keyword" > using< / span > std::string_view;< / div >
< div class = "line" > < span class = "keyword" > using< / span > std::vector;< / div >
< div class = "line" > < / div >
< div class = "line" > < span class = "keywordtype" > void< / span > handle_error(< span class = "keyword" > const< / span > masto::answer_type & answer);< / div >
< div class = "line" > < / div >
< div class = "line" > < span class = "keywordtype" > int< / span > main(< span class = "keywordtype" > int< / span > argc, < span class = "keywordtype" > char< / span > *argv[])< / div >
< div class = "line" > {< / div >
< div class = "line" > < span class = "keyword" > const< / span > vector< string_view> args(argv, argv + argc);< / div >
< div class = "line" > < span class = "keywordflow" > if< / span > (args.size() < = 1)< / div >
< div class = "line" > {< / div >
< div class = "line" > cerr < < < span class = "stringliteral" > " Usage: " < / span > < < args[0] < < < span class = "stringliteral" > " < instance hostname> \n" < / span > ;< / div >
< div class = "line" > < span class = "keywordflow" > return< / span > 1;< / div >
< div class = "line" > }< / div >
< div class = "line" > < / div >
< div class = "line" > < span class = "keywordflow" > try< / span > < / div >
< div class = "line" > {< / div >
< div class = "line" > < span class = "comment" > // Initialize Instance and Instance::ObtainToken.< / span > < / div >
< div class = "line" > masto::Instance instance{args[1], {}};< / div >
< div class = "line" > masto::Instance::ObtainToken token{instance};< / div >
< div class = "line" > < / div >
< div class = "line" > < span class = "comment" > // Create an “Application” (/api/v1/apps),< / span > < / div >
< div class = "line" > < span class = "comment" > // and get URI for the authorization code (/oauth/authorize).< / span > < / div >
< div class = "line" > < span class = "keyword" > auto< / span > answer{token.step_1(< span class = "stringliteral" > " Testclient" < / span > , < span class = "stringliteral" > " read:blocks read:mutes" < / span > ,< / div >
2020-01-14 23:28:44 +01:00
< div class = "line" > < span class = "stringliteral" > " https://example.com/" < / span > )};< / div >
2020-01-14 23:20:02 +01:00
< div class = "line" > < span class = "keywordflow" > if< / span > (!answer)< / div >
< div class = "line" > {< / div >
< div class = "line" > handle_error(answer);< / div >
< div class = "line" > }< / div >
< div class = "line" > < / div >
< div class = "line" > cout < < < span class = "stringliteral" > " Please visit " < / span > < < answer < < < span class = "stringliteral" > " \nand paste the code here: " < / span > ;< / div >
< div class = "line" > < span class = "keywordtype" > string< / span > code;< / div >
< div class = "line" > cin > > code;< / div >
< div class = "line" > < / div >
< div class = "line" > < span class = "comment" > // Obtain the token (/oauth/token).< / span > < / div >
< div class = "line" > answer = token.step_2(code);< / div >
< div class = "line" > < span class = "keywordflow" > if< / span > (!answer)< / div >
< div class = "line" > {< / div >
< div class = "line" > handle_error(answer);< / div >
< div class = "line" > }< / div >
< div class = "line" > < / div >
< div class = "line" > cout < < < span class = "stringliteral" > " Your access token is: " < / span > < < answer < < endl;< / div >
< div class = "line" > < / div >
< div class = "line" > < span class = "comment" > // Test if the token works.< / span > < / div >
< div class = "line" > masto::Connection connection{instance};< / div >
< div class = "line" > answer = connection.get(masto::API::v1::apps_verify_credentials);< / div >
< div class = "line" > < span class = "keywordflow" > if< / span > (!answer)< / div >
< div class = "line" > {< / div >
< div class = "line" > handle_error(answer);< / div >
< div class = "line" > }< / div >
< div class = "line" > < / div >
< div class = "line" > cout < < answer < < endl;< / div >
< div class = "line" > }< / div >
< div class = "line" > < span class = "keywordflow" > catch< / span > (< span class = "keyword" > const< / span > masto::CURLException & e)< / div >
< div class = "line" > {< / div >
< div class = "line" > < span class = "comment" > // Only libcurl errors that are not network errors will be thrown.< / span > < / div >
< div class = "line" > < span class = "comment" > // There went probably something wrong with the initialization.< / span > < / div >
< div class = "line" > cerr < < e.what() < < endl;< / div >
< div class = "line" > }< / div >
< div class = "line" > < / div >
< div class = "line" > < span class = "keywordflow" > return< / span > 0;< / div >
< div class = "line" > }< / div >
< div class = "line" > < / div >
< div class = "line" > < span class = "keywordtype" > void< / span > handle_error(< span class = "keyword" > const< / span > masto::answer_type & answer)< / div >
< div class = "line" > {< / div >
< div class = "line" > < span class = "keywordflow" > if< / span > (answer.curl_error_code == 0)< / div >
< div class = "line" > {< / div >
< div class = "line" > < span class = "comment" > // If it is no libcurl error, it must be an HTTP error.< / span > < / div >
< div class = "line" > cerr < < < span class = "stringliteral" > " HTTP status: " < / span > < < answer.http_status < < endl;< / div >
< div class = "line" > }< / div >
< div class = "line" > < span class = "keywordflow" > else< / span > < / div >
< div class = "line" > {< / div >
< div class = "line" > < span class = "comment" > // Network errors like “Couldn' t resolve host.”.< / span > < / div >
< div class = "line" > cerr < < < span class = "stringliteral" > " libcurl error " < / span > < < to_string(answer.curl_error_code)< / div >
< div class = "line" > < < < span class = "stringliteral" > " : " < / span > < < answer.error_message < < endl;< / div >
< div class = "line" > }< / div >
< div class = "line" > < / div >
< div class = "line" > exit(1);< / div >
< div class = "line" > }< / div >
< / div > <!-- fragment --> < / div > <!-- contents -->
< div class = "ttc" id = "anamespacemastodonpp_html" > < div class = "ttname" > < a href = "namespacemastodonpp.html" > mastodonpp< / a > < / div > < div class = "ttdoc" > C++ wrapper for the Mastodon API.< / div > < div class = "ttdef" > < b > Definition:< / b > api.cpp:19< / div > < / div >
<!-- start footer part -->
< hr class = "footer" / > < address class = "footer" > < small >
Generated by   < a href = "http://www.doxygen.org/index.html" >
< img class = "footer" src = "doxygen.png" alt = "doxygen" / >
< / a > 1.8.16
< / small > < / address >
< / body >
< / html >