調用博客園 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)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM