zoukankan      html  css  js  c++  java
  • 调用博客园 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)
    
  • 相关阅读:
    TCP/IP协议栈之QEMU
    FreeRTOS-Plus-CLI中添加一个自己的命令行
    FreeRTOS A57
    log日志库
    函数解读:ioremap / ioremap_nocache / ioremap_wc / ioremap_wt
    Makefile 使用小结
    41. 缺失的第一个正数(First Missing Positive)
    42. 接雨水(Trapping Rain Water)
    关于C++内联和静态成员函数的问题
    C++11 线程并发问题
  • 原文地址:https://www.cnblogs.com/cmt/p/13191411.html
Copyright © 2011-2022 走看看