csharp Azure功能发布到Blogger

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp Azure功能发布到Blogger相关的知识,希望对你有一定的参考价值。

private static readonly HttpClient _bloggerHttpClient = new HttpClient();
private static async Task<PublishResult> PublishToBlogger(PlaylistItem latestVideo, TraceWriter log)
{
    try
    {
        // Récupération du token
        _bloggerHttpClient.DefaultRequestHeaders.Authorization = null;
        var content = new FormUrlEncodedContent(new List<KeyValuePair<string, string>>
        {
            new KeyValuePair<string, string>("client_id", Environment.GetEnvironmentVariable("GoogleApiClientId")),
            new KeyValuePair<string, string>("client_secret", Environment.GetEnvironmentVariable("GoogleApiClientSecret")),
            new KeyValuePair<string, string>("refresh_token", Environment.GetEnvironmentVariable("GoogleApiRefreshToken")),
            new KeyValuePair<string, string>("grant_type", "refresh_token"),
        });
        var tokenResponse = await _bloggerHttpClient.PostAsync("https://www.googleapis.com/oauth2/v4/token", content);
        if (!tokenResponse.IsSuccessStatusCode)
        {
            log.Error("Récupération du token Blogger en échec");

            return PublishResult.Error();
        }

        var jsonResponse = await tokenResponse.Content.ReadAsStringAsync();
        if (string.IsNullOrEmpty(jsonResponse))
        {
            log.Error("Récupération de token Blogger vide");

            return PublishResult.Error();
        }

        AccessToken token;
        try
        {
            token = JsonConvert.DeserializeObject<AccessToken>(jsonResponse);
        }
        catch(Exception e)
        {
            log.Error("Echec de désérialisatin du token Blogger\n"+e.Message);

            return PublishResult.Error();
        }

        _bloggerHttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.Token);

        log.Info("Lancement de la publication sur Blogger");
        var post = new
        {
            kind = "blogger#post",
            title = latestVideo.Snippet.Title,
            content = ComputeBloggerContent(latestVideo) // Encapsule le champ description dans une structure HTML adéquate au site et embed la vidéo
        };

        var postResponse = await _bloggerHttpClient
            .PostAsJsonAsync(
                $"https://www.googleapis.com/blogger/v3/blogs/{Environment.GetEnvironmentVariable("GoogleApiBloggerId")}/posts/",
                post
            )
        ;

        if (postResponse.IsSuccessStatusCode)
        {
            var response = await postResponse.Content.ReadAsStringAsync();
            log.Info($"Publication Blogger faite !\n{response}");

            return PublishResult.Success();
        }

        log.Error(
            "Erreur de publication sur FB\n" +
            $"\tCode: {postResponse.StatusCode}\n" +
            $"\tReason: {postResponse.ReasonPhrase}\n"
        );

        try
        {
            var response = await postResponse.Content.ReadAsStringAsync();
            log.Error("ContentResponse" + response);
        }
        catch
        {
            // Be quiet...
        }
    }
    catch(Exception e)
    {
        log.Error("Exception sur la publication Blogger\n"+e.Message);
    }

    return PublishResult.Error();
}

以上是关于csharp Azure功能发布到Blogger的主要内容,如果未能解决你的问题,请参考以下文章

csharp Azure功能

csharp Azure功能Youtube列表API示例

csharp 上传到azure.cs

HttpClient PostAsync 到 Blogger API

csharp 将Webhooks添加到Azure Data Factory

csharp 通过.net WebAPI将Azure存储专用容器blob下载/流式传输到AngularJS