Kurlyk
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
5int main() {
6 kurlyk::init(true);
7 try {
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
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
46 return 0;
47}
Implements the OAuth2 Authorization Code flow with optional PKCE.
std::string build_authorization_url()
Builds the full authorization URL with query parameters.
const std::string & state() const
Returns the state parameter used in the last authorization request.
Main header file for the Kurlyk library, providing HTTP and WebSocket support.
void init(const bool use_async=true)
Initializes the Kurlyk library, setting up necessary managers and the network worker.
Definition runtime.hpp:13
void deinit()
Deinitializes the Kurlyk library, stopping async processing or cleaning up synchronous state.
Definition runtime.hpp:26
#define KURLYK_PRINT
Stores client configuration for an OAuth2 Authorization Code + PKCE flow.
std::string redirect_uri
Registered redirect URI.
std::string client_id
OAuth2 client identifier.
bool use_pkce
Whether to use PKCE (RFC 7636). Default true.
std::string authorization_endpoint
Authorization server URL.
std::string token_endpoint
Token exchange URL.
std::string scope
Space-separated requested scopes.