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)
    
  • 相关阅读:
    HBase学习笔记-基础(一)
    系统架构师之旅(一)——软件架构设计:程序员向架构师转型必备总结
    高效程序员的特征:聪明,懒惰
    TOMCAT源码分析(启动框架)
    Java线程池的原理及几类线程池的介绍
    Java 并发:Executors 和线程池
    Memcache基础教程
    发邮件
    图片验证码
    上传图片添加水印
  • 原文地址:https://www.cnblogs.com/cmt/p/13191411.html
Copyright © 2011-2022 走看看