调用博客园 open api 的客户端示例代码

以下是调用博客园 open api 获取 access token 的客户端控制台程序示例代码,通过命令行参数传递 client id 与 client secret 。

C# 版

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;

namespace CSharpClient
{
    class Program
    {
        static async Task Main(string[] args)
        {
            var clientId = args[0];
            var clientSecret = args[1];

            var host = new HostBuilder()
               .ConfigureServices((context, services) =>
               {
                   services.AddHttpClient();
               })
               .Build();

            using (var scope = host.Services.CreateScope())
            {
                var httpClient = scope.ServiceProvider.GetRequiredService<IHttpClientFactory>().CreateClient();

                var data = new FormUrlEncodedContent(new Dictionary<string, string>
                {
                    ["client_id"] = clientId,
                    ["client_secret"] = clientSecret,
                    ["grant_type"] = "client_credentials"
                });

                var response = await httpClient.PostAsync("https://api.cnblogs.com/token", data);
                Console.WriteLine(response.StatusCode);
                Console.WriteLine(await response.Content.ReadAsStringAsync());
            }
        }
    }
}

Python 版

import sys
import requests

if __name__ == "__main__":
    clientId = sys.argv[1]
    clientSecret = sys.argv[2]

    response = requests.post("https://api.cnblogs.com/token", data={
        "client_id": clientId,
        "client_secret": clientSecret,
        "grant_type": "client_credentials"
    })

    print(response)
    print(response.content)
posted @   博客园团队  阅读(1253)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
阅读排行:
· 2分钟学会 DeepSeek API,竟然比官方更好用!
· .NET 使用 DeepSeek R1 开发智能 AI 客户端
· autohue.js:让你的图片和背景融为一体,绝了!
· 10亿数据,如何做迁移?
· 推荐几款开源且免费的 .NET MAUI 组件库
历史上的今天:
2018-06-25 上周热点回顾(6.18-6.24)
2015-06-25 云计算之路-阿里云上:9:55-10:08因流量攻击被进黑洞,造成主站不能正常访问
2012-06-25 [功能改进]Live Writer发博支持“建分类、加标签、写摘要”
2012-06-25 上周热点回顾(6.18-6.24)
2010-06-25 【调查】说说你对技术培训年会的看法
点击右上角即可分享
微信分享提示