This repository has been archived on 2020-05-10. You can view files and clone it, but cannot push or open issues or pull requests.
mastodon-cpp/examples/example07_register_app.cpp

89 lines
2.2 KiB
C++

/* This file is part of mastodon-cpp.
* Register as an application.
*/
#include <iostream>
#include <vector>
#include <string>
#include <cstdint>
#include <sstream>
#ifdef MASTODON_CPP
#include "mastodon-cpp.hpp"
#else
#include <mastodon-cpp/mastodon-cpp.hpp>
#endif
using Mastodon::API;
int main(int argc, char *argv[])
{
if (argc < 2)
{
std::cerr << "usage: " << argv[0] << " <instance>\n";
return 1;
}
Mastodon::API masto(argv[1], "");
masto.set_useragent("mastodon-cpp-example/1.3.3.7");
std::string answer;
std::uint16_t ret;
std::string client_id, client_secret, url;
ret = masto.register_app1("test123",
"urn:ietf:wg:oauth:2.0:oob",
"read follow",
"",
client_id,
client_secret,
url);
if (ret == 0)
{
std::cout << "Visit " << url << " to get the code.\n";
std::string code;
std::cout << "Insert code: ";
std::cin >> code;
std::string access_token;
ret = masto.register_app2(client_id,
client_secret,
"urn:ietf:wg:oauth:2.0:oob",
code,
access_token);
if (ret == 0)
{
std::cout << "Success!\nAccess-token: " << access_token << '\n';
std::cout << "Testing access token...\n";
ret = masto.get(API::v1::accounts_verify_credentials, answer);
if (ret == 0)
{
std::cout << answer;
}
else
{
std::cerr << "Error code: " << ret << '\n';
return ret;
}
}
else
{
std::cerr << "Error code: " << ret << '\n';
return ret;
}
}
else if (ret == 13)
{
std::cerr << "The URL has permanently changed.\n" <<
"New URL: " << url << '\n';
return ret;
}
else
{
std::cerr << "Error code: " << ret << '\n';
return ret;
}
return 0;
}