Tistory remote blogging example based in CLI & PHP

CK CK 3788 
Created at
Updated at  
2,750 0 0

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:

  1. Access Token: You need to obtain an OAuth2 token by registering an application on Tistory.
    • Tistory OAuth2 Documentation: Tistory Open API.
  2. Blog Name: You need the name of the blog you want to post to (this is typically your Tistory username).

Steps:

  1. 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).
  2. 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:

  1. Access Token: You must replace abc123youraccesstokenxyz with the actual access token that you obtain via the OAuth2 process.
  2. Blog Name: Replace mytistoryblog with the actual blog name (this could be your Tistory username).
  3. 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:

  1. Request Authorization Code:

    https://www.tistory.com/oauth/authorize?client_id=your_client_id&redirect_uri=your_redirect_uri&response_type=code
    
  2. 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 

Tags Tistory CLI posting Tistory Open API Tistory automation Tistory command line Tistory curl posting Tistory remote blogging Facebook X
Comments 0
  1. Why SPF and DKIM Are Essential for Linux Email Servers?
    51
  2. Upgrading to GCC 4.9.4 in CentOS 7 requires installing a newer version manually
    588
  3. Force no caching in Apache by modifying the .htaccess file using appropriate headers
    2,691
  4. Beyond your conventional memory size with swap file instead of a partition
    1,147
  5. Gmail requires all senders to authenticate with either SPF or DKIM.
    684
  6. Bash script to install postfix and sasl for sending email
    1,202
  7. Guide for tmux shortcuts - split current pane, toggle pane
    1,313
  8. Generate Let’s Encrypt Wildcard SSL Certificate
    628
  9. Preventing Unauthorized Image Crawling by Bots: A Clever Discovery and How to Address It
    1,731
  10. Network diagnostic tool for IPv4/IPv6 - traceroute/traceroute6
    1,926
  11. chmod by type (directory and file permission)
    736
  12. Check network port is open or not on CentOS 7
    1,196
  13. Change MySQL port number for security purpose
    817
  14. CentOS warning: setlocale: LC_CTYPE: cannot change locale (UTF-8): No such file or directory on CentOS 7
    1,085
  15. Set up an SMTP server on CentOS 7
    730
  16. How to mount windows shared folder on Centos 6.8 - mount error 12 = Cannot allocate memory to the mount.cifs(8)
    922
  17. Configuring IP Address on Centos 6.7
    795
  18. Adding new cron job for automated processing on every 10 minutes on Centos 6.7
    1,336
  19. Set timezone and time on Centos 6.7
    545
  20. How to optimize MySQL automatically in CentOS Linux
    1,010
  21. File conversion to UTF in Schell Script
    1,026
  22. Server status checking by Shell Script
    1,521
Recently updated
  1. Google OAuth Token Expiration: Causes and Solutions
    31
  2. Why 100% SEO Guarantees Better User Engagement and How to Achieve It in Your Service
    57
  3. How do I create animated background for Google Meet?
    23,256
  4. MaxMind GeoLite2 vs. DB-IP Lite vs. ip-api.com: Choosing the Right Free IP Geolocation Solution
    273
  5. Meet Valkey: Why Redis Changed Its License, Why Valkey is Better, and How to Use It as a Drop-in Replacement
    105
  6. How Rogue Web Scrapers Are Stealing My Content and Destroying My Server
    170
  7. Array of arguments passed to script in PHP
    99
  8. Invalid Auction Storms in Ad Tech
    108
  9. Building MRC-Compliant Instrumentation in Ad Tech: Implementation Steps and Key Challenges
    209
  10. The Two Clocks of Ad Server Architecture
    78
  11. Frequency Cap Architecture Overview
    527
  12. Collecting Proof-of-Play from 100,000 Screens
    154
  13. Vector Search in Relational Databases (RDBs): Trends & Usage Environments
    304
  14. What is RedisVL?
    117
  15. Designing a Skill Registry Architecture: Minimize Token Usage and Maximize AI Performance
    247
  16. TPU & GPU Cluster Management Tools
    258
  17. The Concept of Scaffolding in AI Agents
    245
  18. Beyond Static Pipelines: Architecting LLM Multi-Agent Systems for Autonomous Workflow Orchestration
    599
  19. AI-DLC vs. AI-SDLC: Model-Centric vs. System-Centric AI Development
    269
  20. Fine-Tuning Open-Weight LLMs: Training, Licensing, and Deploying Custom Models with Ollama
    355
  21. Inside the Next-Generation Streaming Architecture: Ultra-Low Latency, SGAI, and AI Codecs
    207
  22. Machine Learning: Training vs. Ranking
    232
  23. The Continuous Learning Machine: Designing a Loop That Actually Turns
    229
  24. The Next-Gen Low-Latency Standards: LL-HLS & MoQ
    204
  25. Designing a Data Curation System Around a Vector Database
    230
  26. Extracting Context from Live Video: The Architecture Got Simpler When We Stopped Building Features
    264
  27. The New Ad Stack Behind Live Sports Streaming: Inside SGAI, EABN, and the Standards Race to Fix Live Monetization
    674
  28. Designing MLOps Architectures to Handle Seasonal Data Drift in EV Fleet Models
    201
  29. From 15 to 40+ FPS: Accelerating PyTorch Object Detection on Automotive ECUs
    233
  30. Understanding Data Lineage
    273