C++ wrapper for the Mastodon API
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

example07_register_app.cpp 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* This file is part of mastodon-cpp.
  2. * Register as an application.
  3. */
  4. #include <iostream>
  5. #include <vector>
  6. #include <string>
  7. #include <cstdint>
  8. #include <sstream>
  9. #ifdef MASTODON_CPP
  10. #include "mastodon-cpp.hpp"
  11. #else
  12. #include <mastodon-cpp/mastodon-cpp.hpp>
  13. #endif
  14. using Mastodon::API;
  15. int main(int argc, char *argv[])
  16. {
  17. if (argc < 2)
  18. {
  19. std::cerr << "usage: " << argv[0] << " <instance>\n";
  20. return 1;
  21. }
  22. Mastodon::API masto(argv[1], "");
  23. masto.set_useragent("mastodon-cpp-example/1.3.3.7");
  24. std::string answer;
  25. std::uint16_t ret;
  26. std::string client_id, client_secret, url;
  27. ret = masto.register_app1("test123",
  28. "urn:ietf:wg:oauth:2.0:oob",
  29. "read follow",
  30. "",
  31. client_id,
  32. client_secret,
  33. url);
  34. if (ret == 0)
  35. {
  36. std::cout << "Visit " << url << " to get the code.\n";
  37. std::string code;
  38. std::cout << "Insert code: ";
  39. std::cin >> code;
  40. std::string access_token;
  41. ret = masto.register_app2(client_id,
  42. client_secret,
  43. "urn:ietf:wg:oauth:2.0:oob",
  44. code,
  45. access_token);
  46. if (ret == 0)
  47. {
  48. std::cout << "Success!\nAccess-token: " << access_token << '\n';
  49. std::cout << "Testing access token...\n";
  50. ret = masto.get(API::v1::accounts_verify_credentials, answer);
  51. if (ret == 0)
  52. {
  53. std::cout << answer;
  54. }
  55. else
  56. {
  57. std::cerr << "Error code: " << ret << '\n';
  58. return ret;
  59. }
  60. }
  61. else
  62. {
  63. std::cerr << "Error code: " << ret << '\n';
  64. return ret;
  65. }
  66. }
  67. else if (ret == 13)
  68. {
  69. std::cerr << "The URL has permanently changed.\n" <<
  70. "New URL: " << url << '\n';
  71. return ret;
  72. }
  73. else
  74. {
  75. std::cerr << "Error code: " << ret << '\n';
  76. return ret;
  77. }
  78. return 0;
  79. }