You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在C#中调用Tumblr API发起POST/GET请求?代码调用失败求助

解决Tumblr API获取关注者的OAuth认证问题及垃圾账号识别方案

核心问题分析

你当前的API调用失败,本质是因为Tumblr的/followers接口属于受保护资源,必须通过OAuth 1.0a认证才能访问,匿名请求会被服务器直接拒绝。


现代C# OAuth 1.0a实现示例

我们可以借助OAuth NuGet包快速实现签名认证,步骤如下:

  1. 准备凭证:先在Tumblr开发者平台创建应用,获取以下4个关键凭证:

    • Consumer Key
    • Consumer Secret
    • Access Token(需为目标博客账号授权生成)
    • Access Token Secret
  2. 安装依赖:通过NuGet安装OAuth包

    Install-Package OAuth
    
  3. 完整代码实现

    using OAuth;
    using System;
    using System.Net.Http;
    using System.Threading.Tasks;
    using System.Text.Json;
    
    namespace FukenSpamms
    {
        class Program
        {
            // 替换为你的实际凭证
            private const string ConsumerKey = "YOUR_CONSUMER_KEY";
            private const string ConsumerSecret = "YOUR_CONSUMER_SECRET";
            private const string AccessToken = "YOUR_ACCESS_TOKEN";
            private const string AccessTokenSecret = "YOUR_ACCESS_TOKEN_SECRET";
            private const string BlogId = "your-blog.tumblr.com"; // 或t:xxx格式的博客ID
    
            private static readonly HttpClient client = new HttpClient();
    
            static async Task Main(string[] args)
            {
                Console.WriteLine("开始获取关注者列表...");
    
                // 构造OAuth请求签名
                var oauthRequest = new OAuthRequest
                {
                    Method = "GET",
                    Type = OAuthRequestType.ProtectedResource,
                    ConsumerKey = ConsumerKey,
                    ConsumerSecret = ConsumerSecret,
                    Token = AccessToken,
                    TokenSecret = AccessTokenSecret,
                    RequestUrl = $"https://api.tumblr.com/v2/blog/{BlogId}/followers",
                    Version = "1.0a"
                };
    
                // 添加认证头到HttpClient
                client.DefaultRequestHeaders.Add("Authorization", oauthRequest.GetAuthorizationHeader());
    
                // 发送请求并处理响应
                var response = await client.GetAsync(oauthRequest.RequestUrl);
                response.EnsureSuccessStatusCode();
                var responseJson = await response.Content.ReadAsStringAsync();
    
                Console.WriteLine("API返回结果:");
                Console.WriteLine(responseJson);
    
                // 识别垃圾账号
                DetectSpamAccounts(responseJson);
            }
    
            private static void DetectSpamAccounts(string responseJson)
            {
                Console.WriteLine("\n开始识别垃圾账号:");
                using var doc = JsonDocument.Parse(responseJson);
                var followers = doc.RootElement
                                    .GetProperty("response")
                                    .GetProperty("users")
                                    .EnumerateArray();
    
                foreach (var user in followers)
                {
                    bool isSpam = false;
    
                    // 检查是否使用默认头像(Tumblr默认头像URL包含"default_avatar"关键词)
                    var avatarUrl = user.GetProperty("avatar")[0].GetProperty("url").GetString();
                    if (avatarUrl.Contains("default_avatar"))
                    {
                        isSpam = true;
                    }
    
                    // 检查是否无有效博客标题
                    var blogTitle = user.GetProperty("name").GetString();
                    if (string.IsNullOrEmpty(blogTitle) || blogTitle.Equals("Untitled", StringComparison.OrdinalIgnoreCase))
                    {
                        isSpam = true;
                    }
    
                    if (isSpam)
                    {
                        Console.WriteLine($"垃圾账号:{blogTitle}(ID:{user.GetProperty("id").GetString()})");
                    }
                }
            }
        }
    }
    

关键注意事项

  • 若需获取大量关注者,需处理API分页参数(offset和limit),Tumblr API单次最多返回20条数据。
  • 垃圾账号判断规则可按需扩展,比如添加“无发布内容”“注册时间过短”等维度。
  • 凭证建议通过配置文件或环境变量注入,避免硬编码到代码中。

内容的提问来源于stack exchange,提问作者HelpMeTumblPlease

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 02:37:44