| To post remotely to Tistory (a popular blogging platform in Korea) using the CLI and curl, you need to make use of the Tistory Open API. Below is an example showing how to create a post using curl in the command line. Prerequisites: - Access Token: You need to obtain an OAuth2 token by registering an application on Tistory.
- Tistory OAuth2 Documentation: Tistory Open API.
- Blog Name: You need the name of the blog you want to post to (this is typically your Tistory username).
Steps: - Obtain Access Token:
- You must first register an application in Tistory (on the Tistory API page) to obtain your client ID and client secret.
- After that, you'll need to perform an OAuth2 flow to get an access token. (Refer to Tistory's documentation for full details).
- Create a Post via cURL: Once you have the access token, you can use the
curl command to post content to your Tistory blog.
Example: Posting via CLI with curl curl -X POST "https://www.tistory.com/apis/post/write" \
-d "access_token=your_access_token" \
-d "output=json" \
-d "blogName=your_blog_name" \
-d "title=Post from CLI" \
-d "content=This is the content of the post created using curl in the CLI." \
-d "visibility=3"
Explanation: access_token: The OAuth2 token you obtained from Tistory.output=json: This tells Tistory to return the response in JSON format.blogName: The name of the Tistory blog (typically your username).title: The title of the post.content: The content/body of the post. It can include HTML.visibility=3: This controls the visibility of the post:0 - Secret (private).1 - Protect (password-protected).3 - Public.
Example Command with Real Values: curl -X POST "https://www.tistory.com/apis/post/write" \
-d "access_token=abc123youraccesstokenxyz" \
-d "output=json" \
-d "blogName=mytistoryblog" \
-d "title=My First Remote Post" \
-d "content=<p>This is a remote post created from the Linux CLI using curl.</p>" \
-d "visibility=3"
Notes: - Access Token: You must replace
abc123youraccesstokenxyz with the actual access token that you obtain via the OAuth2 process. - Blog Name: Replace
mytistoryblog with the actual blog name (this could be your Tistory username). - Visibility: You can control the visibility of the post using the
visibility parameter (public, private, or password-protected).
After executing this command, your post should appear on your Tistory blog. Getting the Access Token: To get the access token, follow Tistory's OAuth2 process: Request Authorization Code: https://www.tistory.com/oauth/authorize?client_id=your_client_id&redirect_uri=your_redirect_uri&response_type=code
Exchange Authorization Code for Access Token: curl -X POST "https://www.tistory.com/oauth/access_token" \
-d "client_id=your_client_id" \
-d "client_secret=your_client_secret" \
-d "redirect_uri=your_redirect_uri" \
-d "code=authorization_code" \
-d "grant_type=authorization_code"
This will return an access token, which can then be used for API requests like the post/write example above. For more detailed instructions, you can check the official Tistory API documentation here. Below is another example based in PHP <?php
// Set the access token and blog name
$access_token = 'your_access_token';
$blog_name = 'your_blog_name';
// Set the API endpoint URL
$url = 'https://www.tistory.com/apis/post/write';
// Set up the post data
$data = array(
'access_token' => $access_token,
'output' => 'json', // We want a JSON response
'blogName' => $blog_name,
'title' => 'My Remote PHP Post',
'content' => '<p>This is a post created using PHP and cURL.</p>',
'visibility' => 3 // 3 = public, 0 = private
);
// Initialize a cURL session
$ch = curl_init();
// Set the cURL options
curl_setopt($ch, CURLOPT_URL, $url); // Set the API URL
curl_setopt($ch, CURLOPT_POST, true); // Use POST method
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); // Attach the post data
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return the response as a string
// Execute the request and capture the response
$response = curl_exec($ch);
// Check for errors
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
} else {
// Decode the JSON response
$response_data = json_decode($response, true);
// Print the response (or handle it as needed)
if (isset($response_data['tistory']['status']) && $response_data['tistory']['status'] == '200') {
echo 'Post created successfully! Post ID: ' . $response_data['tistory']['postId'];
} else {
echo 'Error: ' . $response;
}
}
// Close the cURL session
curl_close($ch);
?>
For your information, Tistory decided stop providing OpenAPI service from Feb 2024. You can see more detail at https://notice.tistory.com/2664 |