I used the code below to get an access taken for TickTick but the code is very general and can be altered to run this common process for all apps that use OAuth for issuing tokens. You’d just need to change the endpoints below that you see that start with ticktick.com to match your app.
Just get the initial info from the maker of your specific app.
I used PHP instead of Node because it seems simpler to get this little piece of information using PHP because there were no dependencies necessary, just this one file.
More info on this from TickTick’s Documentation
// Replace these values with your actual Client ID and Client Secret
$client_id = 'YOUR_CLIENT_ID';
$client_secret = 'YOUR_CLIENT_SECRET';
$redirect_uri = 'YOUR_REDIRECT_URI'; // Must match the registered Redirect URI
// Check if the user has been redirected back with an authorization code
if (isset($_GET['code'])) {
// Get the authorization code from the query parameters
$authorization_code = $_GET['code'];
// Set up the request to exchange the code for an access token
$token_url = 'https://ticktick.com/oauth/token';
$data = array(
'client_id' => $client_id,
'client_secret' => $client_secret,
'code' => $authorization_code,
'redirect_uri' => $redirect_uri,
'grant_type' => 'authorization_code'
);
// Initialize cURL session
$ch = curl_init($token_url);
// Set cURL options
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute cURL session and get the response
$response = curl_exec($ch);
// Check for cURL errors and handle the response
if ($response = false) {
echo 'cURL error: ' . curl_error($ch);
} else {
// Parse the JSON response
$token_data = json_decode($response, true);
if (isset($token_data['access_token'])) {
// Access token has been successfully obtained
$access_token = $token_data['access_token'];
// You can now use the access token for API requests
echo 'Access Token: ' . $access_token;
} else {
echo 'Failed to obtain access token.';
}
}
// Close the cURL session
curl_close($ch);
} else {
// Redirect the user to initiate the OAuth flow
$oauth_url = 'https://ticktick.com/oauth/authorize?' .
'client_id=' . $client_id .
'&redirect_uri=' . $redirect_uri .
'&response_type=code';
header('Location: ' . $oauth_url);