Kurlyk
Toggle main menu visibility
Loading...
Searching...
No Matches
openrouter_oauth_pkce_example.cpp
Go to the documentation of this file.
1
#define KURLYK_AUTO_INIT 0
2
#include <
kurlyk.hpp
>
3
#include <iostream>
4
5
int
main
() {
6
kurlyk::init
(
true
);
7
try
{
8
kurlyk::OAuthConfig
config;
9
config.
client_id
=
"your_openrouter_client_id"
;
10
config.
authorization_endpoint
=
"https://openrouter.ai/oauth/authorize"
;
11
config.
token_endpoint
=
"https://openrouter.ai/oauth/token"
;
12
config.
redirect_uri
=
"http://127.0.0.1:8080/callback"
;
13
config.
scope
=
"read"
;
14
config.
use_pkce
=
true
;
15
16
kurlyk::http::auth::OAuthPkceClient
oauth(config);
17
18
std::string auth_url = oauth.
build_authorization_url
();
19
KURLYK_PRINT
<<
"Open this URL in a browser:"
<< std::endl;
20
KURLYK_PRINT
<< auth_url << std::endl;
21
KURLYK_PRINT
<<
"State: "
<< oauth.
state
() << std::endl;
22
23
// In a real application, after the user is redirected back,
24
// extract the "code" and "state" from the redirect URL and call exchange_code:
25
//
26
// std::string code = receive_code_from_redirect();
27
// std::string returned_state = receive_state_from_redirect();
28
//
29
// if (!oauth.validate_state(returned_state)) {
30
// KURLYK_PRINT << "State mismatch!" << std::endl;
31
// return 1;
32
// }
33
//
34
// kurlyk::AuthResult result;
35
// if (oauth.exchange_code(code, result)) {
36
// KURLYK_PRINT << "Access token: " << result.token.access_token << std::endl;
37
// } else {
38
// KURLYK_PRINT << "Error: " << result.error_message << std::endl;
39
// }
40
41
}
catch
(
const
std::exception& e) {
42
KURLYK_PRINT
<<
"Error: "
<< e.what() << std::endl;
43
}
44
45
kurlyk::deinit
();
46
return
0;
47
}
kurlyk::http::auth::OAuthPkceClient
Implements the OAuth2 Authorization Code flow with optional PKCE.
Definition
OAuthPkceClient.hpp:31
kurlyk::http::auth::OAuthPkceClient::build_authorization_url
std::string build_authorization_url()
Builds the full authorization URL with query parameters.
Definition
OAuthPkceClient.hpp:52
kurlyk::http::auth::OAuthPkceClient::state
const std::string & state() const
Returns the state parameter used in the last authorization request.
Definition
OAuthPkceClient.hpp:232
kurlyk.hpp
Main header file for the Kurlyk library, providing HTTP and WebSocket support.
kurlyk::init
void init(const bool use_async=true)
Initializes the Kurlyk library, setting up necessary managers and the network worker.
Definition
runtime.hpp:13
kurlyk::deinit
void deinit()
Deinitializes the Kurlyk library, stopping async processing or cleaning up synchronous state.
Definition
runtime.hpp:26
main
int main()
Definition
openrouter_oauth_pkce_example.cpp:5
KURLYK_PRINT
#define KURLYK_PRINT
Definition
print_utils.hpp:8
kurlyk::OAuthConfig
Stores client configuration for an OAuth2 Authorization Code + PKCE flow.
Definition
OAuthConfig.hpp:18
kurlyk::OAuthConfig::redirect_uri
std::string redirect_uri
Registered redirect URI.
Definition
OAuthConfig.hpp:23
kurlyk::OAuthConfig::client_id
std::string client_id
OAuth2 client identifier.
Definition
OAuthConfig.hpp:19
kurlyk::OAuthConfig::use_pkce
bool use_pkce
Whether to use PKCE (RFC 7636). Default true.
Definition
OAuthConfig.hpp:25
kurlyk::OAuthConfig::authorization_endpoint
std::string authorization_endpoint
Authorization server URL.
Definition
OAuthConfig.hpp:21
kurlyk::OAuthConfig::token_endpoint
std::string token_endpoint
Token exchange URL.
Definition
OAuthConfig.hpp:22
kurlyk::OAuthConfig::scope
std::string scope
Space-separated requested scopes.
Definition
OAuthConfig.hpp:24
examples
openrouter_oauth_pkce_example.cpp
Generated by
1.17.0