如何使用 REST "GET" 请求 Confluence 页面内容 API

How to do "GET" request for Confluence page content with REST API

我是 C# 的初学者,也是第一次使用 REST API。
我想使用带有 HTTP 触发器的 Azure 函数通过 REST API“GET”从 Confluence 页面获取内容。
我在 local.settings.json 文件中指定了 ConfluenceUserConfluenceAPIConfluenceDomain

代码 (Visual Studio 2019):

using Microsoft.Extensions.Configuration;
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

namespace HttpClientEx
{
    class Program
    {
        private static async Task Main(string[] args)
        {
            var confBuild = new ConfigurationBuilder()
                .AddJsonFile("local.settings.json")
                .AddEnvironmentVariables()
                .Build();

            //Basic Authentication
            var user = Encoding.UTF8.GetBytes(confBuild["ConfluenceUser"]);
            var api = Encoding.UTF8.GetBytes(confBuild["ConfluenceAPI"]);
            var domain = confBuild["ConfluenceDomain"];

            string basic_auth = Convert.ToBase64String(user) + ":" + Convert.ToBase64String(api);

            string pageid = "1234567";
            string page = "https://" + domain + "/wiki/rest/api/content/" + pageid;

        }
        private static async Task<string> getPageContent(string domain, int pageid, string basic_auth)
        {
            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", basic_auth);

                var response = await client.GetAsync(String.Format("https://" + domain + "/wiki/rest/api/content/" + pageid.ToString() + "?expand=space,metadata.labels"));
                var result = await response.Content.ReadAsStringAsync();

                return result;
            }
        }
    }
}

我的问题:
我是否正确执行了 GET 请求?
如何查看从 Confluence 页面获取的内容?

如果想在Azure函数中调用restAPI,请参考以下步骤

  1. 安装Azure Functions Core Tools

  2. 创建 Azure 函数

#create project
func init LocalFunctionProj --dotnet
cd LocalFunctionProj
#create httptrigger function
func new --name Http --template "HTTP trigger" --authlevel "anonymous"
  1. 更新项目

一个。将以下设置添加到 local.settings.json

{
    "IsEncrypted": false,
    "Values": {
        "AzureWebJobsStorage": "UseDevelopmentStorage=true",
        "FUNCTIONS_WORKER_RUNTIME": "dotnet",
        "ConfluenceUser":"",
        "ConfluenceAPI":"",
        "ConfluenceDomain":""
    }
}

b。更新 Http.cs

中的代码
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;

namespace LocalFunctionProj
{
    
    public static class Http
    {
        private static HttpClient httpClient = new HttpClient();
        [FunctionName("Http")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
            ILogger log, ExecutionContext context)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            var confBuild = new ConfigurationBuilder()
                .SetBasePath(context.FunctionAppDirectory)
                .AddJsonFile("local.settings.json")
                .AddEnvironmentVariables()
                .Build();

            //Basic Authentication
            var user =confBuild["ConfluenceUser"];
            var api = confBuild["ConfluenceAPI"];
            var domain = confBuild["ConfluenceDomain"];
              httpClient.DefaultRequestHeaders.Authorization= new AuthenticationHeaderValue("Basic",
                Convert.ToBase64String(
                    ASCIIEncoding.ASCII.GetBytes(
                        string.Format("{0}:{1}", user, api))));
            object  body = null;
            using (HttpResponseMessage response = await httpClient.GetAsync(
                        $"https://{domain}/wiki/rest/api/content/163979"))
            {
                response.EnsureSuccessStatusCode();
                string responseBody = await response.Content.ReadAsStringAsync();
               
                body = JsonConvert.DeserializeObject(responseBody);
            }
            return new OkObjectResult(body);
        }
    }
}

  1. 测试

使用以下命令运行项目

func start