如何使用c#在reddit.com上发布文本
本文关键字:布文本 文本 何使用 reddit com | 更新日期: 2023-09-27 18:03:28
我使用以下代码在reddit.com的文本帖子,但它给出了糟糕的验证码错误,我们如何获得验证码并在代码中传递它。我被卡在那里了,请帮帮我。
class Program
{
static void Main(string[] args)
{
Reddit reddit = null;
var authenticated = false;
while (!authenticated)
{
Console.Write("OAuth? (y/n) [n]: ");
var oaChoice = Console.ReadLine();
if (!string.IsNullOrEmpty(oaChoice) && oaChoice.ToLower()[0] == 'y')
{
Console.Write("OAuth token: ");
var token = "neud__ZOgyo52UsffzF0Z2WJnN0";
reddit = new Reddit("neud__ZOgyo52UsffzF0Z2WJnN0");//HsmHqXmvK3xciDt_FkPQJ-Klq-M
reddit.InitOrUpdateUser();
authenticated = reddit.User != null;
if (!authenticated)
Console.WriteLine("Invalid token");
}
else
{
Console.Write("Username: ");
var username = Console.ReadLine();
Console.Write("Password: ");
var password = ReadPassword();
try
{
Console.WriteLine("Logging in...");
reddit = new Reddit(username, password);
authenticated = reddit.User != null;
}
catch (AuthenticationException)
{
Console.WriteLine("Incorrect login.");
authenticated = false;
}
}
}
Console.Write("Create post? (y/n) [n]: ");
var choice = Console.ReadLine();
if (!string.IsNullOrEmpty(choice) && choice.ToLower()[0] == 'y')
{
Console.Write("Type a subreddit name: ");
var subname = Console.ReadLine();
var sub = reddit.GetSubreddit("/r/subreddit");
Console.WriteLine("Making test post");
var post = sub.SubmitTextPost("RedditSharp test", "This is a test post sent from RedditSharp");
Console.WriteLine("Submitted: {0}", post.Url);
}
else
{
Console.Write("Type a subreddit name: ");
var subname = Console.ReadLine();
var sub = reddit.GetSubreddit("/r/subreddit");
foreach (var post in sub.GetTop(FromTime.Week).Take(10))
Console.WriteLine("'"{0}'" by {1}", post.Title, post.Author);
}
Console.ReadKey(true);
}
public static string ReadPassword()
{
var passbits = new Stack<string>();
//keep reading
for (ConsoleKeyInfo cki = Console.ReadKey(true); cki.Key != ConsoleKey.Enter; cki = Console.ReadKey(true))
{
if (cki.Key == ConsoleKey.Backspace)
{
if (passbits.Count() > 0)
{
Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
Console.Write(" ");
Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
passbits.Pop();
}
}
else
{
Console.Write("*");
passbits.Push(cki.KeyChar.ToString());
}
}
string[] pass = passbits.ToArray();
Array.Reverse(pass);
Console.Write(Environment.NewLine);
return string.Join(string.Empty, pass);
}
}
我使用这个链接"https://www.reddit.com/dev/api/"作为参考。plzz帮助
我看了一下RedditSharp的GitHub页面,似乎没有办法用API的代码来解决验证码(这是你要求的正确吗?)要做到这一点,你必须研究用代码解决captcha,这对我来说太高级了。如果你不想让用户这样做,你可以研究一下像2Captcha这样的东西来解决这个问题。
如果不是……
在RedditSharp api中它有一个内置的ConsoleCaptchaSolver
using System;
namespace RedditSharp
{
public class ConsoleCaptchaSolver : ICaptchaSolver
{
public CaptchaResponse HandleCaptcha(Captcha captcha)
{
Console.WriteLine("Captcha required! The captcha ID is {0}", captcha.Id);
Console.WriteLine("You can find the captcha image at this url: {0}", captcha.Url);
Console.WriteLine("Please input your captcha response or empty string to cancel:");
var response = Console.ReadLine();
CaptchaResponse captchaResponse = new CaptchaResponse(string.IsNullOrEmpty(response) ? null : response);
return captchaResponse;
}
}
}
这将返回一个链接到一个captcha,你可以在web浏览器中查看,你只需要输入你认为的captcha是什么,并返回captcha响应与请求(如API中所述)。
var post = sub.SubmitTextPost("RedditSharp test", "This is a test post sent from RedditSharp");
是我相信你得到这个错误的地方。SubmitTextPost只是调用Submit方法并返回Post对象。但是要提交一个新的帖子,你需要填写一个captcha(这是必需的),但提交方法本身确实调用HandleCaptcha方法在ConsoleCaptchaSolver所以你应该看到captcha ID和URL能够看到并提供响应太。如果没有,我建议使用NuGet来确保你有最新的RedditSharper包。
编辑:我查看了RedditSharp的TestRedditSharp项目,并注释掉了一个不重要的部分,你可以在这里看到它https://gist.github.com/brian-heidrich/af61bef6a60438b2cfbf80e557d2ac5f
我能够使用该代码并使测试帖子/r/DTL https://www.reddit.com/r/DTL/comments/4yuu4f/redditsharp_test/